Shop OBEX P1 Docs P2 Docs Learn Events
servo and motors problem — Parallax Forums

servo and motors problem

edited 2008-11-04 03:50 in Propeller 1
Hey guys.
I am just writing a code for an object avoidance. however i encountered a problem. Servos seems not to be holding long enough to make my turns. I already tryed everything that i can think of, but dumb mistakes are very hard to see. So maybe you can find something that is wrong with this code.

(CON
  _clkmode      = xtal1 + pll16x                        ' use crystal x 16
  _xinfreq      = 5_000_000
    ' Electronic Speed Control (ESC) 1
  _esc1Forward      =     1                              ' forward direction
  _esc1Reverse      =     0                              ' reverse direction
  _esc1delayPerStep =    10                              ' ms delay per % change in duty cycle
  _esc1Freq         =   400                              ' ESC 1  PWM Frequency
  _esc1In1          =    17                              ' Input 1 (L298 In1 pin 5)
  _esc1In2          =    18                             ' Input 2 (L298 In2 pin 7) 
  _esc1EnA          =    16                              ' Enable A (L298 EnA pin 6)
  ULTRASONIC        =    1
  SERVOPIN          =    23
  SYRINGE           =    22
OBJ
  motor[noparse][[/noparse]2] : "L298SetMotor"
  lcd      : "ILM-216_LCD"
  num      : "simple_numbers"
  PINGSens : "Ping"
  SMotor   : "Servo32v3"
  gps      : "GPS_SmartMode"             
  text     : "Serial_Lcd"
  
VAR
   WORD pwServo1                            ' pulse 
   WORD pwServo2
   long lonposi
   long latposi
   long homelon
   long homelat
   long finallon
   long finallat
   
PUB main | dutyCycle1, direction1, direction2, delay1, tmp, lon, lat,index
dira[noparse][[/noparse]SERVOPIN] := 1
motor[noparse][[/noparse]0].init(_esc1EnA, _esc1In1, _esc1In2, _esc1Freq, 0)   ' initialize SetMotor.spin 0
SMotor.Set(SERVOPIN,1500)                                   ' Start @ 90 degree position                                                
SMotor.Start    
dutyCycle1 := 25
direction1 := _esc1Forward
direction2 := _esc1Reverse
delay1 := _esc1delayPerStep
  if PINGSens.centimeters(ULTRASONIC) > 50  
    ''----------------Motor 1 Forward ----------------- 
    SMotor.Set(SERVOPIN,1500)
    motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction1, delay1)
    
      '' -----------------Motor 1 Avoidance --------------  
  elseif PINGSens.centimeters(ULTRASONIC) < 50
    motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
    SMotor.Set(SERVOPIN, 1500)
    waitcnt(cnt + 200_000_000)
    if PINGSens.centimeters(ULTRASONIC) > 50 
      motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction1, delay1)
      SMotor.Set(SERVOPIN, 2000)
      waitcnt(cnt+200_000_000)
       if PINGSens.centimeters(ULTRASONICC) > 50  
         motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
         SMotor.Set(SERVOPIN, 1500)
         waitcnt(cnt+200_000_000))

I· am using Servo32v3.

The way i want it is while the robot does not senses anything, it goes forward, as soon as it detects something reverse for 2 seconds, then make a right or left turn for 2-3 seconds·while going forward, and then straighten it self out. Can someone tell me maybe i am not holding them long enough, but how do i do it?

Thanks in advance·

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-10-30 16:07
    Vladimir Andrejev,
    Looking at this on the scope the Servo signal looks fine...· However your indentions and lack of a repeat causes the cog to terminate.
    I made a few changes to the PINGSens section·that might help...

    repeat
     
      if PINGSens.centimeters(ULTRASONIC) > 50  
        ''----------------Motor 1 Forward ----------------- 
        motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction1, delay1)
        '' -----------------Motor 1 Avoidance --------------  
      else
        motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
        
    
      SMotor.Set(SERVOPIN, 1500)
    
      waitcnt(cnt + 200_000_000)
     
      if PINGSens.centimeters(ULTRASONIC) > 50 
         motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction1, delay1)
         SMotor.Set(SERVOPIN, 2000)
         waitcnt(cnt+200_000_000)
      if PINGSens.centimeters(ULTRASONICC) > 50  
         motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
         SMotor.Set(SERVOPIN, 1500)
         waitcnt(cnt+200_000_000)
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • edited 2008-11-02 04:00
    It still does not work. The servoo motor does not hold, and i think it stops my dc motor from spining aswell, because dc motor works on its own.
    Can someone help?
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-11-02 05:17
    Vladimir Andrejev,

    Initialize the Servo motors before your motor routine instead...

    SMotor.Set(SERVOPIN,1500)                                   ' Start @ 90 degree position
    motor[noparse][[/noparse]0].init(_esc1EnA, _esc1In1, _esc1In2, _esc1Freq, 0)   ' initialize SetMotor.spin 0
    
    



    ...Also, you don't nee to set the DIR pins for the servo, the object already does that for you.

    remove this line:
    dira[noparse][[/noparse]SERVOPIN] := 1
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-11-03 01:58
    I had the same problem. To someone coming from C++ it is a little strange to call set and then init. Usually it goes constructor/ init/ pass values but it works fine now that I have gotten the hang of it.

    Sort of related question so I hopefully I don't have to start a new thread:

    I use Servo 32v3 and it seems like often only get about 90 degrees of motion out of servos (at most) whereas with a Parrallax servo controller (with a BS2) O could get more like 135 degrees (or more) of motion. I have tried different brands of servos (Futaba and Hitec) and the results are similar. Also, I get the same results with my code and the demo code. I think it is because the servos could take a wider range then 1000uS-2000uS but that is just a guess.

    Does someone know why the servos might have a smaller response range then with a controller? Does anyone have a software solution?

    Just wondering.

    George Collins
  • edited 2008-11-03 03:02
    Well i did it a bit dodgy but it works. What i did when i set the servo a put a waitcnt in there and after that i initialize the dc motors. My servo motor works at 180 degreen.

    I just set the initial position to a 1000 or 2000 and after that i start the servo. and when you set the servo again it should move 180 degreen. so if you would like to move 180 degrees.

    Servo.set(SERVOPIN, 1000)

    Servo.Start

    Servo.Set(SERVOPIN, 2000)

    waitcnt(cnt + xxxx+

    Seems to work for me, but very very glitchy.
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-11-03 04:37
    I wonder if the voltage matters-- when it is within the acceptable range-- that is another difference I wasn't thinking about. When I used the servo controller I supplied the servo with 7.2 v 3000 mAh ( I was controlling 8). Now I am only controlling one, buy I am using the 5 V supply with less amps as well. Could that cause my servo to have less rotation range?

    George Collins
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-11-03 05:31
    Vladimir Andrejev,

    "Seems to work for me, but very very glitchy" -

    Can you describe what you mean by glitchy?
    Can you post a schematic?
    How long are the leads between the Propeller and the Servo?
    Is a resistor used on the signal line?
    Is a capacitor located near the Servo power?
    The grounds are shared between the servo power supply and the Propeller Power supply correct?

    Sorry for all of the questions, but this object was written awhile ago and has seemed to be stable.
    I just want to make sure that I understand what might be causing an undesired servo behavior.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-11-03 06:04
    Beau Schwabe,
    Would there be an easy (or relatively easy) way to modify your code to widen the range of the servo pulse? I am a fairly competent programmer, but completely new to the Prop and weak at assembly code.

    By the way-- thank you for Servo32v3-- I use it on my Quadruped all the time now. You have made the prop chip useful for me and it is very cool.

    George Collins
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-11-03 06:11
    ·GeorgeCollins,
    Absolutely... Inside of the Servo32 object there is a line that determines the limitations.· It is there to protect standard servos from going out of bounds from the 1ms to 2ms pulse width.
    Width := 1000 #> Width <# 2000
    


    PUB Set(Pin, Width)                                                             'Set Servo value
          Width := 1000 #> Width <# 2000                                            'limit Width value between 1000uS and 2000uS
            Pin :=    0 #>   Pin <# 31                                              'limit Pin value between 0 and 31
          ServoData[noparse][[/noparse]Pin] := clkfreq / _1uS * Width                                  'calculate # of clocks for a specific Pulse Width
          dira[noparse][[/noparse]Pin] := 1                                                            'set selected servo pin as an OUTPUT
          ServoPinDirection := dira                                                 'Read I/O state of ALL pins
        
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • edited 2008-11-03 09:09
    Thanks for quick replies beau Schwabe.
    I attached the schematic as an attachement.
    I used 4.7 k resistors. The propstick uses a volateg regulator with a cap, schematic is in propstick manual. The whole thing is on the breadboard. No caps are located near it.

    Just braging but we are making a robot that uses gps to navigate itself to a position that is programed through bluetooth. (Position is in water) So it goes on land and after that it goes in water(doesnt submerge). It also avoids all objects on the way.
    1280 x 800 - 137K
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-11-03 14:48
    Vladimir Andrejev,

    I'm still not sure what you mean by glitchy...· what is the power supply for·the servo's?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • edited 2008-11-03 16:18
    5 volts.
    if PINGSens.centimeters(ULTRASONICC) > 50
    motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
    SMotor.Set(SERVOPIN, 1500)
    waitcnt(cnt+200_000_000)

    if i do the code like that the servo doesnt turn and it stop the motor. If i do the servo first and then the motor still does the same thing.
    if i put
    motor[noparse][[/noparse]0].setMotor(dutyCycle1, direction2, delay1)
    waitcnt(xxx)
    SMotor.Set(SERVOPIN, 2000)
    waitcnt(xxx)

    It starts working but switches of the motor at the same time as a servo. And it doesnt centre itself instantly.

    I tried to do as you have said to Set the servo first and then switch of the motor but still doesnt work. you can hear it starts to switch the motor PWM but doesnt turn and switches both things off at once, without even spining the motor or turning servo.

    Strange i know, but i checked all my wiring and all the voltages, its like it terminates both cogs.

    What if i want to spin the motors for 20 seconds and turn servo only for 1 second. Is that possible?
  • Beau SchwabeBeau Schwabe Posts: 6,560
    edited 2008-11-03 16:30
    Vladimir Andrejev,

    What happens if you don't run the motors... just the servos?

    What are the other motors you are running? .. and how are they being powered?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • GeorgeCollinsGeorgeCollins Posts: 132
    edited 2008-11-04 03:50
    Beau,
    I tried changing the range to 800- 2200 tonight after work. It worked great-- looks like roughly 45 more degrees. Thank you very much for your help!

    George Collins
Sign In or Register to comment.