Wednesday 12 January 2011

Time Control

Parallax Continuous Rotation Servos
Just to have an idea what sort of things we are looking at to start with from the manual, I'll occasionally post some of the code the exercise is asking me to perform.

The exercise I just finished is early in chapter 2, and is getting the concept of control flow and timing using the BOE and the BASIC Stamp microcontroller. Here is the code:

' {$STAMP BS2}
' {$PBASIC 2.5}

DEBUG "Start Timer..."

PAUSE 1000
DEBUG CR, "One second elapsed"

PAUSE 2000
DEBUG CR, "Three seconds elapsed"

DEBUG CR, "Done"

END
 As you can see, there isn't anything terribly magical about this. The programming language used is a specialised form of the BASIC language. Specifically, it is known as Parallax Beginners All-purpose Simple Instruction Code. Compared to Java, Python, Ruby and the .NET programming languages, BASIC and its variants operates at a significantly lower level.

The reason time control is important, is because when we are operating the servos (pictured) we are essentially sending digital signals to an analogue device. Because we are operating at such a low level, we can't simply give the device a goal and expect it to get on with it, we actually have to teach it how to use itself. When we want these servos to actually start moving, what we really need to be doing is telling the microcontroller how long to supply electricity to the servo in order to make it function. When electricity is applied to the servo, the control horn rotates.

In order to precisely control the servo, we need to have some fairly fine control over how long we apply power to it, right?

In the program listing above, the numbers are referring to milliseconds, or thousandths of a second. 1000 milliseconds = 1 second.

The word DEBUG is simply an instruction to the microcontroller to send the text in quotes back to the terminal, the CR simply represents a carriage return.

The statements with a single quote in front of them are compiler directives. All I know at this stage is, those two are necessary.

The program above, when run, produces the following output:
Start Timer...
One second elapsed
Three seconds elapsed
Done
Pretty much as expected, so far so good.

Gets more interesting when you consider that while a thousanth of a second seems like it passes pretty quickly, two millionths of a second is the base time granularity of the BASIC Stamp 2 (BS2).

Pretty well blew me away when I found that out.

I mean, we all know computers are quick, but have you ever stopped to consider exactly how quick? Divide a second by one thousand, you have a millisecond. Divide that millisecond by a thousand, you have a microsecond, also referred to as μs. Thats not "us" with an unusual accent.

According to that bastion of incontrovertible truth, Wikipedia, it takes 5.4 microseconds for light to travel 1 mile in a vacuum. In addition, the length of a day increases by 15 microseconds due to the tidal pull of the moon, but 2.68 microseconds was subtracted from the length of the day as a result of the 2004 Indian Ocean earthquake.

Who measures these things?

In any case, two of those microseconds is theoretically the shortest period of time I can apply power to those servos. In reality, I dont think anything would move in 2 microseconds, but when I actually get to that part of the book, I'll have a go and let you know.

No comments:

Post a Comment