Servo driver.

shamu

Member
Hi all.

I'm trying to control the position of a r/c servo using a variable resistor and an 18M2, my code looks like this:

Symbol SERVO1 =c.0

main:
readadc c.1, b1
pulsout SERVO1,b1
goto main


Generally it appears to work but the servo is hunting and jittering quit a bit, anybody any ideas where I'm going wrong?

Thanks.
 

Jamster

Senior Member
Pulseout is not what you wan't, it will reset every time it loops to main.

Try Servo. (exact same syntax)
 

shamu

Member
Think I have the general idea now, the pot returns a value between 0 & 255, this needs turning in a 75 - 225 range for the servo. I can handle the maths but am I correct in thinking the pic's can't cope with brackets?
My current program looks like this:

Symbol SERVO1 =b.1

servo SERVO1,b.1

main:

readadc c.1, b1

let w1 = b1/255 *150 +75

servopos b.1, w1

goto main


The variable w1 doesn't appear to work either, any ideas??

Thanks.
 

Andrew Cowan

Senior Member
PICAXE chips do integer maths only, so b1/255 will always return 0 or 1.

The solution is to use:
let w1 = b1 * 150 / 255 + 75

A
 

westaust55

Moderator
@shamu,

Furthermore, the SERVOPOS command only uses a byte variable (or constant) for the data with a value from 0 to 255.

Also, even when the result is to be placed into a byte variable, the PICAXE does the maths internally at 16-bits for a single line of maths.
While the word variable may funciton, only the lower 8 bits (ie lower byte) would be sued and the upper byte is "wasted".


Therefore, your code can be:

Code:
SYMBOL Servo1 = B.1
SYMBOL Pot1    = C.1

SYMBOL PotPos = b1
SYMBOL SerPos = b2

Init:
 SERVO Servo1,SerPos

Main:
  READADC Pot1, PotPos
   SerPos =PotPos * 150 / 255 + 75
  SERVOPOS Servo1,  SerPos
  GOTO Main
A few further comments:
1. I gave used the [code] and [/code] tags around the program code which is good forum etiquette for more than a couple of lines of code.
Not grumbling, just letting you know these tags exist.


2. Indented the program code so only the labels are in the 1st (left most) column of each line which helps folks to read the code, particularly when you start getting many lines of code.


3. Added three more SYMBOL statements – again for consistency (you had 1) and with a bigger program listing makes changing a variable or IO pin easier when there are many instances of use.


4. The Programming Editor is not case sensitive but presentation/formatting of the program listings helps others read and understand your future bigger listings resulting in more responses. So while my personal preference only here goes:
(a) for IO pins consider a capital for the port letter (helps to differentiate between variables (ie B.1 vs b.1 vs b1)
(b) For program keywords use capitals throughout – for me at least makes it easier to identify the program commands and structure at a glance.


And for more information with respect to comment 1 above:
In case you need the information, a relatively easy way to get the [code] and [/code] markers around your program listing is:
1. click on the Go Advanced button at the bottom right of the post/edit screen
2. select the code text in your post
3. click on the hash symbol (#) at the right hand end of the middle row of formatting icons at the top of the post/edit window which wraps your code in the relevant [code] and [/code] markers.
 
Last edited:

shamu

Member
Hi.

I'm still having trouble looking at the content of a variable as a simulation is running, I can see the list of variables but not their content.
Is it possible to view whats in them?

Thanks.
 
Last edited:

westaust55

Moderator
In the P.E. simulator, with the PICAXE panel displayed (which shows the state of the pins),
there is a >> button at the bottom right of the panel.
Click that button to expand the panel and display the variables and their values to the right of the PICAXE chip and IO pins display.
 
Top