A problem about the global variable in Servo_32 v7
kevinspace
Posts: 56
Hello everybody, I encountered a problem about the global variable in Servo_32 v7 Demo.
I have 2 servos (servo1 and servo2).
First, in the PUB1, I let the servo1 and servo2 turning to the central position (1500), then I let the servo1
turn from the most of right side (2300) to the most of left side and repeat it.
Here is the program and the video :
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
ServoCh1 = 0
ServoCh2 = 1
OBJ
SERVO: "Servo32v8.spin"
Debug: "FullDuplexSerialPlus"
PUB Control_servo1
SERVO.Start
SERVO.Ramp
SERVO.Set(ServoCh1,1500)
SERVO.Set(ServoCh2,1500)
repeat
SERVO.SetRamp(ServoCh1,2300,500)
waitcnt(clkfreq*5 + cnt)
SERVO.SetRamp(ServoCh1,700,500)
waitcnt(clkfreq*5 + cnt)
Next, I added another PUB (called PUB2) to control the servo2 and excited it by PUB1,
the progam captured the position of servo1
Here is the program :
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
ServoCh1 = 0
ServoCh2 = 1
VAR long Stack[50]
long Position
OBJ
SERVO: "Servo32v8.spin"
Debug: "FullDuplexSerialPlus"
PUB Control_servo1
SERVO.Start
SERVO.Ramp
SERVO.Set(ServoCh1,1500)
SERVO.Set(ServoCh2,1500)
repeat
SERVO.SetRamp(ServoCh1,2300,500)
waitcnt(clkfreq*5 + cnt)
SERVO.SetRamp(ServoCh1,700,500)
waitcnt(clkfreq*5 + cnt)
PUB Control_servo2
repeat
Position := SERVO.GetServoPosition(ServoCh1)
Above the programwhich I ensure the position of global variable which can be gotten availably.
And the next, I try to use the PUB2 to control the servo2,
and I hope the position of servo2 could follow the position of servo1.
Here is the programand the video :
PUB Control_servo2
repeat
Position := SERVO.GetServoPosition(ServoCh1)
SERVO.SetRamp(ServoCh2,Position,500) waitcnt(clkfreq*5 + cnt)
But thet will cause some bug ! The servo1 would stop turning after few seconds.
Are there anybody could tell me why or give me any suggesstion?
thanks
I have 2 servos (servo1 and servo2).
First, in the PUB1, I let the servo1 and servo2 turning to the central position (1500), then I let the servo1
turn from the most of right side (2300) to the most of left side and repeat it.
Here is the program and the video :
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
ServoCh1 = 0
ServoCh2 = 1
OBJ
SERVO: "Servo32v8.spin"
Debug: "FullDuplexSerialPlus"
PUB Control_servo1
SERVO.Start
SERVO.Ramp
SERVO.Set(ServoCh1,1500)
SERVO.Set(ServoCh2,1500)
repeat
SERVO.SetRamp(ServoCh1,2300,500)
waitcnt(clkfreq*5 + cnt)
SERVO.SetRamp(ServoCh1,700,500)
waitcnt(clkfreq*5 + cnt)
Next, I added another PUB (called PUB2) to control the servo2 and excited it by PUB1,
the progam captured the position of servo1
Here is the program :
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
ServoCh1 = 0
ServoCh2 = 1
VAR long Stack[50]
long Position
OBJ
SERVO: "Servo32v8.spin"
Debug: "FullDuplexSerialPlus"
PUB Control_servo1
SERVO.Start
SERVO.Ramp
SERVO.Set(ServoCh1,1500)
SERVO.Set(ServoCh2,1500)
repeat
SERVO.SetRamp(ServoCh1,2300,500)
waitcnt(clkfreq*5 + cnt)
SERVO.SetRamp(ServoCh1,700,500)
waitcnt(clkfreq*5 + cnt)
PUB Control_servo2
repeat
Position := SERVO.GetServoPosition(ServoCh1)
Above the programwhich I ensure the position of global variable which can be gotten availably.
And the next, I try to use the PUB2 to control the servo2,
and I hope the position of servo2 could follow the position of servo1.
Here is the programand the video :
PUB Control_servo2
repeat
Position := SERVO.GetServoPosition(ServoCh1)
SERVO.SetRamp(ServoCh2,Position,500) waitcnt(clkfreq*5 + cnt)
But thet will cause some bug ! The servo1 would stop turning after few seconds.
Are there anybody could tell me why or give me any suggesstion?
thanks
Comments
This is my program diagram .
Thanks
Wouldn't it be a heck of a lot easier to command the second servo when you know the command for the first servo?
This can be done by a few mouseclicks within the propellertool
make your mainfile the topobject and compile it with F8
click file - archive - project...
this will create zip-file wich contains all files that are nescessary to compile your code
then attach this zipfile to a posting with tha "Go Advanced" version of the reply-website
to the problem itself:
no idea in the style "that's it"
The waitcnt-command in your method Control_Servo2 makes the cog stop completely for five seconds
If the servo1 moves in any other way than forward backward 180 degress servo 2 will NOT follow this change
(as it waits until 5 seconds are over
so just remove the waitcnt and use method "Set" instead of SetRamp
without the waitcnt the values are read and method "Set" is called 10000 times per second
and this is fast enough to make servo 2 follow servo1
in the cognew-command you have coded
cognew(ControlServo2,@stack[20])
you should code it
cognew(ControlServo2,@stack) to make use of the full 50 longs defined for stack
why servo1 stops after some time I can't tell.
This requieres detailed debugging which means adding debug-code to see if all methods are still working as expected
using another cog to measure the pulselength of the signal on the IO-pins or if you got an oscilloscope watching the signals
on the scope
you can use the jm_freqin-object for that
watching the values of all involved variables by using
Hanno Sander's EZLog.EXE
If I want to say more than the above I would have to do this debugging myself
best regards
Stefan
Perhaps you could stick the desired position value into a global variable and let both servo controller cogs read that variable and move each servo to the desired position. Something like:
In Control_servo2, I shortened the waitcnt to pause for 1/10th of a second. This way, your servo #2 will get the updated value quicker (rather than getting the updated value up to 5 seconds later).
Let us know how it works.