Generating Stepped Motion

by Ivan Hamilton 7/28/2012 6:13:00 PM

The standard parallel port "pulsing" setup in Mach3 uses a set "kernel speed" (25-100kHz) and this is the rate at which it decides to output a step pulse (or not). It is therefore also the maximum pulsing rate. The implementation details are specific to Mach3, but how would you determine an "even" pulsing pattern?

Let's rephrase our problem: How do you evenly take Y steps over X periods (or, determine which points in a 2 dimensional raster should be plotted in order to form a close approximation of a straight line between two given points). Bresenham's line algorithm is a way to achieve this.

At the start, we're already 1/2 way to crossing into the next Y position. For each increment in X, we move Y/X closer. Once we cross into the next Y position, we're another 1 away from the next crossing.

This could be expressed as:

var accumulator = 0.5;
loop (X times)
	accumulator = accumulator + Y/X;
	if (accumulator >= 1)
		accumulator = accumulator - 1;
		//step!

We can eliminate the fractional 1/X & 0.5 and use only integer math by multiplying everything by 2X.

var accumulator = X;
loop (X times)
	accumulator = accumulator + 2Y;
	if (accumulator >= 2X)
		accumulator = accumulator - 2X;
		//step!

We can also reduce calculation load by precalculating and storing 2Y & 2X.

This algorithm gives nice even pulse patterns. An example of 0 to a maximum of 10 pulses is shown below:

             1
    1234567890
 0  ..........
 1  ....X.....
 2  ..X....X..
 3  .X..X...X.
 4  .X.X..X.X.
 5  X.X.X.X.X.
 6  X.X.XX.X.X
 7  X.XXX.XX.X
 8  XX.XXXX.XX
 9  XXXXX.XXXX
10  XXXXXXXXXX 

The integer maths also makes this suitable for microcontrollers.

Below is an example of 3 & 7 (yellow top & blue bottom) pulses per 10 periods being generated by an AVR micro.

When we produce a continuous stream of these pulses, the pattern repeats (prior & next pattern shaded below)

 

So, how much processing power is consumed by this? I'm using a 256 cycle ring-buffer to precalulate 4 output channels, and inserting a 10ms delay when the buffer fills. To help measure calculation duration, I toggled a line (yellow) high during buffer re-fill. One of the output channels is display below (it's 25kHz signal is not decernable at this resolution).

 
Of a 16.80ms cycle, 10.40ms is "available" for other tasks. Pattern generation currently consumes ~40% of "available" CPU time. I say "available", since the CPU is constantly interrupted for short periods to update the output.

Currently rated 1.5 by 641 people

  • Currently 1.48362/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

CNC | Electronics

Overcoming NAT dropouts with BitTorrent & TP-Link TD-W8960N

by Ivan Hamilton 7/5/2012 9:46:00 PM

ADSL Modem/Routers like the TP-Link TD-W8960N must maintain connection information between internet (WAN) and local (LAN) machines.

I'd been having issues with connections "freezing" after a minute or so:

  • MS-Outlook failing to send/receive 
  • MS-Access connections to a remote SQL database would give errors
  • Our VOIP phone would fail to ring on incoming calls
  • RDP connections

I could re-establish the connection, and everything would be fine... until some period of inactivity. This smelt like a network/firewall issue, and the only thing in common was our TP-Link TD-W8960N.

Many ADSL Modem/Routers run a cut-down version of Linux, and the TP-Link TD-W8960N is no exception. Handily, it provides telnet console access and includes BusyBox with sh. NAT services are provided by netfilter's iptables.

You can see the active & maximum number of connections tracked using:

cat /proc/sys/net/netfilter/nf_conntrack_count;cat /proc/sys/net/netfilter/nf_conntrack_max

For me, this returned 2024 & 2024. The router was tracking the maximum number of connections it could. This isn't as much of a problem as it sounds, since once two-way communication happens, connections are marked as ASSURED, and are retained over other connections when the table fills. This stops "active" connections being dropped to preserve connections that were never actually established.

The actual connections can be seen with:

cat /proc/net/nf_conntrack

When I did this I got quite the surprise - ALL of my connections were ASSURED. This means my Outlook, Access, VOIP, RDP connections were eligible to get dropped - and were after enough inactivity. Why? What were the other two thousand ASSURED connections? And why were they all in the TIME_WAIT state?

They were the continual stream of incoming (and quickly closed) BitTorrent connections. But why were they still being tracked?

When a TCP connection is closed down, the connection enters the TIME_WAIT state (which is per default set to 2 minutes). This is used so that all packets that have gotten out of order can still get through, even after the connection has already closed. This is used as a kind of buffer time so that packets that have gotten stuck in one or another congested router can still get to the other end. I was hosting torrents with DHT, and could get 15 connections a second (held for 2 minutes=1800).

2 minutes is complete overkill. If you don't see a packet within a couple of seconds, it's not coming. Also, the "connection" limit of 2024 is far too small (and the TD-W8960N appears to have plenty of free memory to store more connections).

Changing it... 

Luckily, it's very very easy to change. Telnet into your TD-W8960N, enter your username & password (default: admin/admin) and then issue the following commands:

echo "5" > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_time_wait
echo "16360" > /proc/sys/net/netfilter/nf_conntrack_max
That's it. Until the router restarts...

Making the change stick... 

I couldn't find a way to issue custom commands to the router on startup. So, instead I looked for a way to automate issuing the commands. What I needed was a simple tool that took text and sent it over the telnet session.

Enter netcat (or nc).

Get a native windows copy at http://www.rodneybeede.com/Compile_Netcat_on_Windows_using_MinGW.html and extract into a directory (such as "c:\Program Files\nc\") 

I created a script file (conntrack.txt) looking something like this:

admin
yourpassword
echo "5" > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_time_wait
echo "16360" > /proc/sys/net/netfilter/nf_conntrack_max
cat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_time_wait
cat /proc/sys/net/netfilter/nf_conntrack_max
cat /proc/sys/net/netfilter/nf_conntrack_count
exit

And then created a windows task scheduled to run every 15 minutes (Starting in the nc directory "c:\Program Files\nc\")

c:\windows\system32\cmd.exe /c "nc 192.168.1.1 23 -i 1 -w 2 < conntrack.txt > conntrack.out.txt"

And that's it.

Every 15 minutes, it logs into the router and fixes the NAT settings. If the router restarts, it'll be fixed within 15 minutes.

 

Currently rated 2.1 by 10 people

  • Currently 2.1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Powered by BlogEngine.NET 1.3.1.30
Original theme by Mads Kristensen

About the author

Name of author Ivan Hamilton
"My inner nerd can beat up your inner nerd."

E-mail me Send mail

Adsense

Calendar

<<  September 2017  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678

View posts in large calendar

Recent comments

Tags

None

    Entropy

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2017

    Sign in