Shop OBEX P1 Docs P2 Docs Learn Events
Question in servo motors code — Parallax Forums

Question in servo motors code

wafa86wafa86 Posts: 44
edited 2010-11-06 09:07 in BASIC Stamp
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?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-03 09:38
    As usual when people post only part of their code, it's not clear what you're doing. What you've shown doesn't do what you've described.

    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?
  • wafa86wafa86 Posts: 44
    edited 2010-11-04 09:58
    In brief, I want to calculate the distance that the servo motors move. I did not use the text book method to calculate the distance. I did some tests to calculate the distance that the wheel will rotate in 0.5 second and I measure the degree in 0.5 second of the wheel. In brief, I concluded that the equation of distance:

    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???
  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-04 10:47
    I suggest you not use the WRITE and READ statements as you have used them. Every time you call Count_Meature_Inc you will write to the same location in EEPROM. There is a limit of about 100000 writes to any single location in EEPROM after which it "wears out". It would take quite a few days of continuous usage to do this, but it's not "forever".

    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.
  • ercoerco Posts: 20,256
    edited 2010-11-04 12:28
    Sounds like you plan to make a Big Trak type of vehicle, to accurately control a differential-drive robot or platform using two continuous rotation servos. Without encoders you won't get very close. Timing commands to drive a motor for N seconds are not particularly accurate. Having encoders will let you achieve 2 things: 1) drive fairly straight and 2) measure distance & turns. So not having encoders will prevent you from achieving those two very important things.

    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.
  • wafa86wafa86 Posts: 44
    edited 2010-11-06 09:07
    your are right about the power problem. As a solution, I change the transformer that is supply the BOE, and replace it with four 1.5v batteray, and the program works perfectly, with increasing the counter perfectly. thanks alot :)
Sign In or Register to comment.