Question in servo motors code
wafa86
Posts: 44
hi
I am using BS2PX with two Parallax Continuous Rotation Servos (#900-00008) and I need the user to control the amount of movement by pressing keys in the keybaord. I defined a counter to count the number of presses that the user enter and the code as follows:
'forwad
counterdis =0
SERIN RX, Baud,[DirChar]
DO
IF (DirChar = "w") THEN 'direction charcter
PULSOUT RightMotor, 1625
PULSOUT LeftMotor, 2125
countdis = countdis + 1
LOOP
SEROUT Tx,Baud,[SDEC Countdis,CR]
the result shows in the debug screen that the counter increasing by 1 perfectly when swicth on 1 and when swicth on 2(motors works) the counter value is = random values.
how to increment a counter with pulsout?
I am using BS2PX with two Parallax Continuous Rotation Servos (#900-00008) and I need the user to control the amount of movement by pressing keys in the keybaord. I defined a counter to count the number of presses that the user enter and the code as follows:
'forwad
counterdis =0
SERIN RX, Baud,[DirChar]
DO
IF (DirChar = "w") THEN 'direction charcter
PULSOUT RightMotor, 1625
PULSOUT LeftMotor, 2125
countdis = countdis + 1
LOOP
SEROUT Tx,Baud,[SDEC Countdis,CR]
the result shows in the debug screen that the counter increasing by 1 perfectly when swicth on 1 and when swicth on 2(motors works) the counter value is = random values.
how to increment a counter with pulsout?
Comments
You said that you want the keyboard to control the amount of movement of the servos. That's hard to do. The PULSOUT value approximately controls the speed of the servo motor. The only way to control distance is to accurately control the amount of time the motor is activated. With continuous motion servos, the speed is not tightly controlled, so that makes things a bit unpredictable. In order to accurately control distance, you'd need some sort of distance measurement. A wheel encoder is one possible solution (like this).
Given this information, what do you want to do?
distance = constant * counter
the constant is the distance in 0.02second
and the counter is the number of presses that the user enter.
for forward movement -- > counter = counter + 1
backward movement -- > counter = counter - 1
the big problem here is the code, when I use the pulsout command the counter will have random values, but when I switch off the motors the counter increase normally, I tried the EEPROM commands to store counter value like READ and WRITE command the code as follows:
RightMotor PIN 12 'pin 12 that is connected to Right Servo Motor
LeftMotor PIN 13 'pin 13 that is connected to Left Servo Motor
RX PIN 2 'Receive Pin
TX PIN 0 'Transmit Pin
Baud CON 396 '9600 Baud
DirChar VAR Byte'direction charcter
Counter VAR Word'counter used by For...loop
Countdis VAR Word(2)
new VAR Bit
prev VAR Bit
FREQOUT 4, 1, 18090
DirChar = 0 'Initialize direction charcter
new = 1
prev = 0
Countdis(prev) = 0
SERIN RX, Baud,[DirChar]
DO
'
Forward
IF (DirChar = "w") THEN
PULSOUT RightMotor, 1625
PULSOUT LeftMotor, 2125
GOSUB Count_Meature_Inc
Count_Meature_Inc:
Countdis(prev) =Countdis(prev)+1
Countdis(new)=Countdis(prev)+1
WRITE 50, Word Countdis(prev)
READ 50, Word Countdis(new)
SEROUT Tx,Baud,[SDEC Countdis(prev),CR]
LOOP
the result is the same as the previous code.
my Question is how to calculate the counter perfectlly without any random values????? and does PULSOUT command involved in what happen the random values of the counter? I ll attach my results also.
your solution about Boe-Bot Digital Encoder Kit is perfect, but I can not purchase it, because I do not have time, I 'll submit my project on the end of november and the kit takes two weeks to arrive. Do you have any suggestion on the code???
You also call Count_Meature_Inc using a GOSUB, but there's no corresponding RETURN. It's not going to do what you want.
I suggest you back up and start with a step by step description of what you want to accomplish, essentially make a verbal flowchart. Avoid using actual PBasic for now.
Also, if you have a program, any program using servo motors, where the program behaves one way if the motors are active and another way if the motors are not "pulsed", then you likely have a power problem. Either there's noise from the motors getting into the power supply or the additional current drain is causing the power supply voltage to drop. In both cases, the Stamp resets repeatedly.
If you're in a bind (will it really take 2 weeks to get your encoders?) you could improve your situation by NOT using a diff drive platform. Make a Tricycle. One front drive motor (continuous rotation) that is steered by a standard servo (~180 degrees of travel). You could make a crude encoder for your drive wheel with a microswitch and a cam, 1-4 clicks per revolution. Using a small wheel, resolution will be "good enough" to measure unit distances and turns. The steering servo will allow you to go fairly straight and make turns fairly consistently.