Society of Robots - Robot Forum
Software => Software => Topic started by: 555 timer chip guy on March 31, 2007, 09:32:15 AM
-
I am trying to control a servo with a BS1 microcontroller but I can't figure out how to express the value 1.5 or any other decimals for that mater dose any one know how to express a decimal in PBASIC.
-
whats your code look like so far?
look into 'float', which is a variable type for storing decimal numbers. its been almost a decade since i programmed in PBASIC, but i dont remember ever having a problem with decimals when using them . . . in theory it should just work . . .
-
I've never worked with PBASIC, but one way you could consider would be to use fixed-point math.
For example, you can store numbers like 1.5 and 2.4 as 15 and 24 respectively. This way, you can do
regular math on these "integers", and to get the digits and decimal parts, you can divide by 10 and mod by 10 respectively. Of course, if you needed more precision, you could use larger numbers like 100 or 1000, but you get the idea.
Hope this helps!
-
A 'float' character stores decimals. I don't know what it looks like in PBASIC though.
If anything, think of it a different way. 1.5 is also 3/2 Sometimes it can be a matter of modifying what you learned. If you do division, make sure you store it in an appropriate integer.
Hope this helps
-
thanks very much for the help that helped a lot . :D
-
so tell me if I have this right:
if I wont to pause for 1.5 ms I could use this command PAUSE 2/3 ???
-
I have no clue. Try looking for information on the web first.
If not, give the program a try like that and see if you get any errors, or if the program doesn't like to your wishes.
Maybe, if the programming language supports it, try () to encase the number.
Or perhaps, if you use this number once, and if the programming language supports it, try defining a variable at the beginning to be 3/2 or 1.5. Then you can just say PAUSE (Insert Variable Here)
A few ideas that may work.
-
Actually, from what I know, most languages will treat 3 / 2 as an integer division, and return an integer, so 3/2 = 1 in this case.
If you need to do a pause for fractions of a second, but the function like pause only accepts seconds, then it is unlikely that it'll work. Instead, there might be another function that will take in milliseconds or some smaller division.
Again, I'm not familiar with PBASIC, but I'm guessing through my experience with other languages, and playing with QBasic a long time ago (no idea if it's related to PBASIC though).
Another suggestion would be to try google for "pause fraction of second" or something like that.
-
so tell me if I have this right:
if I wont to pause for 1.5 ms I could use this command PAUSE 2/3 ???
Since the units are milliseconds, there is no way to do anything other than whole milliseconds with PAUSE.
If you're trying to control an I/O pin for a specific length of time, you could use PULSOUT. Otherwise, you need to use a better microcontroller that has higher timing resolution.
- Jon
-
thank you for you help
-
When I do my servos, I dont actually pause in milliseconds. I actually pause in units of computational cycles.
For example, if I know my microcontroller takes 1.5ms to run a while loop 300 times, then I just do a while loop 300 times for my servo :P
300 is a non-decimal number, too 8)
-
For example, if I know my microcontroller takes 1.5ms to run a while loop 300 times, then I just do a while loop 300 times for my servo :P
Hmm.. how do you know the microcontroller takes 1.5ms to run the loop 300 times? By calculating the number of clock cycles in the loop and comparing it to the processor speed?
-
the easiest method is just to use an oscope - send out a high-low pulse from a DO after the while loop is done, and measure the time between pulses.
when the time reaches about 1.5ms, your servo should be centered.
i dont bother calculating or counting anything cause its too dependent on hardware (the timing crystal, voltages, etc.)
-
the easiest method is just to use an oscope - send out a high-low pulse from a DO after the while loop is done, and measure the time between pulses.
when the time reaches about 1.5ms, your servo should be centered.
i dont bother calculating or counting anything cause its too dependent on hardware (the timing crystal, voltages, etc.)
That's really smart, thanks! ;D There's so much more about electronics and stuff about robotics that I need to learn :P
-
thanks :D
-
I never thought of doing it that way. I used to do BASIC and everything was in milliseconds and very limited.
Now I just need an oscilliscope :(
-
I started off with a basic stamp II microcontroller and programmed it in PBASIC. From my experience, PBASIC doesnt like decimal numbers. All numbers must be in integer format. If u really want to work with decimal numbers, try PICmicros. If you still want to play with the stamp, then try the parallax forums. They provide excellent support for their products there.