[FIXED] Servo dances like crazy
English is not my first language, so please excuse me. I've tried my best to make sure most errors are eliminated so that you can understand my post in the clearest way possible.
Hi guys, I'm new to PICAXE so I'm wondering whether I could get some of your help.
At the moment, I've got a servo coupled with an 08M2 PICAXE chip which is connected to my desktop via serial to USB. The chip is connected to the download cable for getting commands from a C# program that I've written, while the servo is connected to the C.1 pin. I've setup the servo so that it looks similar to the layout from the manual, which means that it has its own batter pack (6 volts). My goal here is to achieve a stable servo movement that I can adjust from 0 to 90 degrees positioning.
My main problem here is that whenever I use 'servopos' to send signal to my servo, the servo decides to move erratically. I've observed that at the beginning, it moves to the start where there is nearly 0 pulse, then it moves back to the angle/pulse it was specified. It repeats this several times until the signal stops (pause duration), and eventually the servo either stays at the specified point, or stops at the middle of nowhere.
In theory, I think that the servo is losing the pulse from the PICAXE and interprets this as an actual value. I do not have an oscilloscope to test this out as I'm also a little bit new to electronics. I also know that the PICAXE does not turn itself off when doing so, as the values (like 'b3') which were previously recorded can be recalled by other functions/variables.
I've read other threads around this forum but I can't seem to find an answer. The only ones I've seen were about the jittery movement of servos when moving, and none even state similar erratic behavior of the servo (so far).
Here is the PICAXE code that I use:
Hopefully you can understand what I'm doing in the code.
Before I end this, I heard that using serrxd/sertxd and servopos or any time dependent commands would cause bad things to happen as some PICAXE chips only have a single hardware timer. Doing so would have to make the commands share the single timer, thus the timer could become inaccurate to some commands.
I tried this without the serial commands, and I basically get the same problem. It shouldn't be the timer sharing that's causing this weird behavior. But I believe it might be how the pulses are changing too much in short intervals, which is something that I saw when the servo moves sometimes; when it is near the specified point, it moves there directly without any fuss, otherwise.
Do you think it's a busted servo, or is it a flaw in the PICAXE's internal hardware?
(Note: The servo I'm currently using is the GWS mini STD/BB
Link: http://www.gws.com.tw/english/product/servo/002.htm
Note 2: Current PICAXE firmware is PICAXE-08M2 v4.A)
-----
Other questions while I'm at it:
What do the other parameters in the serrxd command mean?
I can understand the square brackes, and @ptr parameter, but not the two @ptrinc. I usually use the command in this format as it's the only one I know:
I tried using the qualifier bit of the command too, but I seem to be unable to receive anything when I send a command with the qualifier in it as well.
Eg:
-"c=" as qualifier (without quote marks)
-I write "c=F" from program to PICAXE
-The PICAXE does not understand the qualifier I've stated for it to have: "c="
The same goes with the sertxd command; I don't know what the last two parameters do.
Thank you for taking your time reading this. Suggestions are always welcome too.
English is not my first language, so please excuse me. I've tried my best to make sure most errors are eliminated so that you can understand my post in the clearest way possible.
Hi guys, I'm new to PICAXE so I'm wondering whether I could get some of your help.
At the moment, I've got a servo coupled with an 08M2 PICAXE chip which is connected to my desktop via serial to USB. The chip is connected to the download cable for getting commands from a C# program that I've written, while the servo is connected to the C.1 pin. I've setup the servo so that it looks similar to the layout from the manual, which means that it has its own batter pack (6 volts). My goal here is to achieve a stable servo movement that I can adjust from 0 to 90 degrees positioning.
My main problem here is that whenever I use 'servopos' to send signal to my servo, the servo decides to move erratically. I've observed that at the beginning, it moves to the start where there is nearly 0 pulse, then it moves back to the angle/pulse it was specified. It repeats this several times until the signal stops (pause duration), and eventually the servo either stays at the specified point, or stops at the middle of nowhere.
In theory, I think that the servo is losing the pulse from the PICAXE and interprets this as an actual value. I do not have an oscilloscope to test this out as I'm also a little bit new to electronics. I also know that the PICAXE does not turn itself off when doing so, as the values (like 'b3') which were previously recorded can be recalled by other functions/variables.
I've read other threads around this forum but I can't seem to find an answer. The only ones I've seen were about the jittery movement of servos when moving, and none even state similar erratic behavior of the servo (so far).
Here is the PICAXE code that I use:
Code:
init:
symbol pulse_pin = C.1 ' Servo signal pin
symbol minServo = 84 ' 0 degrees pulse
symbol maxServo = 169 ' 90 degrees pulse
symbol stp = 16/2 ' step value pulse. inc/dec pulses by this amount
symbol val = b9 ' Temporary value for FOR loops
b1 = minServo ' variable for calibrating b2 to correct position
b2 = minServo ' Used for setting the position of servo
b3 = minServo ' Used to record old position of b2 before setting a new value for it
servo pulse_pin, 70 ' Initialise servo
main:
do
serrxd [5000, timeout], b0 ' Get data from download link and chuck it in 'b0'
' If after 5secs, no command has been received,
' go to timeout sub.
' PICAXE receives the data nicely.
select case b0
case "C" ' Close
b1 = 0
case "1" ' Open by 10%
b1 = stp
case "2" ' Open by 20%
b1 = stp * 2
case "3" ' Open by 30%
b1 = stp * 3
case "4" ' Open by 40%
b1 = stp * 4
case "5" ' Open by 50%
b1 = stp * 5
case "6" ' Open by 60%
b1 = stp * 6
case "7" ' Open by 70%
b1 = stp * 7
case "8" ' Open by 80%
b1 = stp * 8
case "9" ' Open by 90%
b1 = stp * 9
case "F" ' Fully open
b1 = maxServo - minServo
case "A" ' Auto
gosub getTemp
else
gosub timeout
endselect
b3 = b2 ' Record old position
b2 = b1 + minServo ' Set b2 to desired position
gosub driveServo
loop
goto main
' Sub for moving servo to a specific angle by pulse
driveServo:
' Smooth out the servo's transition to make it move slower
if b3 > b2 then
for val = b3 to b2 step -1
servopos pulse_pin, val
pause 50
next val
elseif b2 > b3 then
for val = b3 to b2 step 1
servopos pulse_pin, val
pause 50
next val
endif
' Make sure servo stay at that position
for val = 0 to 3
servopos pulse_pin, b2
pause 500
next val
sertxd("Servo moved!") ' Send back to program
return
getTemp:
' TODO:
' use readadc to read analogue voltage of temperature from sensor
b1 = 17
return
timeout:
b3 = b2 ' Record old position
if b0 = "A" then ' If previous command is 'Auto', then use that instead
gosub getTemp ' set b1 & 2 variable from sensor
b2 = b1 + minServo
endif
gosub driveServo
goto main
return
Before I end this, I heard that using serrxd/sertxd and servopos or any time dependent commands would cause bad things to happen as some PICAXE chips only have a single hardware timer. Doing so would have to make the commands share the single timer, thus the timer could become inaccurate to some commands.
I tried this without the serial commands, and I basically get the same problem. It shouldn't be the timer sharing that's causing this weird behavior. But I believe it might be how the pulses are changing too much in short intervals, which is something that I saw when the servo moves sometimes; when it is near the specified point, it moves there directly without any fuss, otherwise.
Code:
init:
symbol pulse_pin = C.1 ' Motor 1
b0 = 84 ' Left (0 degrees)
b1 = 125 ' Center (45 degrees)
b2 = 169 ' Right (90 degrees)
servo pulse_pin, 85
main:
do
SERVOPOS pulse_pin, b0 ' Go to left
pause 3000
' SERVOPOS pulse_pin, b1 ' Go to centre
' pause 2000
SERVOPOS pulse_pin, b2 ' Go to right
pause 3000
loop
(Note: The servo I'm currently using is the GWS mini STD/BB
Link: http://www.gws.com.tw/english/product/servo/002.htm
Note 2: Current PICAXE firmware is PICAXE-08M2 v4.A)
-----
Other questions while I'm at it:
What do the other parameters in the serrxd command mean?
Code:
disconnect
serrxd [1000, timeout],@ptrinc,@ptrinc,@ptr
reconnect
Code:
serrxd [<timeout duration>, <timeout address>], <storage pointer>.
Eg:
-"c=" as qualifier (without quote marks)
-I write "c=F" from program to PICAXE
-The PICAXE does not understand the qualifier I've stated for it to have: "c="
The same goes with the sertxd command; I don't know what the last two parameters do.
Code:
main: for b1 = 0 to 63 ; start a loop
sertxd("The value of b1 is ",#b1,13,10)
pause 1000
next b1
Last edited: