Shop OBEX P1 Docs P2 Docs Learn Events
Making a pwm almost endless loop — Parallax Forums

Making a pwm almost endless loop

Ady111Ady111 Posts: 15
edited 2011-07-10 15:12 in BASIC Stamp
Hello, i have some questions about a project that i build. I have to control using pwm signal 5 motors.
First is a brushless motor that has ESC, so i need a PWM to set it's speed. This motor should run from the start to the finish of the prcess with constant speed, so it needs a permanent signal, but constant.
There are 2 more little brushed motors controlled by transistors, therefore i need PWM signal to control their speed. This motors should run only in specific situations(sensor activation), and should run independently.
And finaly 2 servo motors. For this i need PWM signal to set their angle. This should run independently and only in some cases.(when a sensor is activated).
When a third sensor is activated the brushless motor should stop(finish of the process).
My concern is about generating the PWM for the brushless motor, beacause it has to run all the time until one sensor is active, but in this time i have to make some monitoring process of the sensors and comands for the other motors and i'm afraid it will stop when i make the interogations.
Is there a way to set a pin of the bs2 to generate endless PWM signal while i'm using the others to check sensors and generate other PWM signals?
P.S. It's not abslut necessary to stop the brushless motor at the end of the process, i can turn it off manual eventually.
Any sugestion is welcomed. Greetings
«1

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-28 11:56
    Stamps cannot automatically generate a continuous PWM signal nor can they automatically generate more than one PWM signal at a time. What they can do is interleave several PWM signals as part of your program's cycle. Typically, a Stamp can handle 3 or 4 servo motors this way with the typical 50 pulses per second at 0.5ms to 2.5ms per pulse. Worst case, this leaves a few milliseconds in a 20ms cycle to read a sensor or two and execute a few statements to process the sensor data. There's a program called Roaming with the PING))) that's available via the Parallax webstore page for the PING ultrasonic distance sensor that illustrates this technique.

    The ServoPAL and the Propeller Servo Controller are external devices that take over the burden of issuing these individual PWM pulses on a regular basis. The Propeller Servo Controller can also be programmed to handle sensors, communications, etc.
  • Ady111Ady111 Posts: 15
    edited 2011-06-28 13:04
    This mean i don't have a chance to make my project with the basic stamp:(. Am I right?
  • ercoerco Posts: 20,256
    edited 2011-06-28 13:20
    Per Mike, a Stamp needs some extra hardware to support all that you ask. Another option is the discontinued but still available PWMPal (~$30). Have a look at http://www.hobbyengineering.com/H1472.html and http://www.solarbotics.com/products/30030/

    Neat item, they pop up on Ebay occasionally as well.
  • Ady111Ady111 Posts: 15
    edited 2011-06-28 13:36
    unfortunately i have only the bs2 oem to handle with and i have to finish the project in one week. I have to quickly find a solution. I could renounce to control the 2 brushed motors, but i still need the PWM signal for the brushless and the ones for the servos.
  • ercoerco Posts: 20,256
    edited 2011-06-28 14:44
    For your motor PWM (which is a fixed speed), you might consider using an external signal generator, like a 555/556 IC. Simple, and you can control (enable) it with one Stamp pin using a High/Low command. You'll adjust the motor speed with a potentiometer on the 555/556. That would leave the Stamp to read your sensors and drive 2 servos, which it can easily do.
  • Ady111Ady111 Posts: 15
    edited 2011-06-28 16:52
    erco wrote: »
    For your motor PWM (which is a fixed speed), you might consider using an external signal generator, like a 555/556 IC. Simple, and you can control (enable) it with one Stamp pin using a High/Low command. You'll adjust the motor speed with a potentiometer on the 555/556. That would leave the Stamp to read your sensors and drive 2 servos, which it can easily do.
    Can you help me with a scheme for 555/556 for my brushless motor?
  • ercoerco Posts: 20,256
    edited 2011-06-28 17:07
    Can sure try. If I can't, I'm sure someone else here can. Post the specs (freq, duty cycle, etc) and let's see what happens.
  • Ady111Ady111 Posts: 15
    edited 2011-06-28 17:19
    erco wrote: »
    Can sure try. If I can't, I'm sure someone else here can. Post the specs (freq, duty cycle, etc) and let's see what happens.
    I dont know the freq, but in bs2 to run it i wrote a loop:
    pulseout 13, 450
    pause 18
    with this 2 comands i manage to send the propper pwm to run the motor. For 450 the motor stops. While running i incrised the number until 670, when i reached the speed that i wanted. Maybe this help you.
    I think it should work also with 20 pause.
  • ercoerco Posts: 20,256
    edited 2011-06-28 18:12
    So standard servo control signals work with your ESC? Should be easy...
  • ercoerco Posts: 20,256
    edited 2011-06-28 22:17
    In fact, the Stamp can just barely manage 3 servo signals at once, without any outside hardware. You have to nest all your pulsouts in a loop and execute that loop each 20 ms, so you can't do a whole lot else, but you can read some inputs or sensors and/or do some quick calculations. Will that suffice for you? Quicker & easier than adding external hardware.
  • Ady111Ady111 Posts: 15
    edited 2011-06-29 06:06
    erco wrote: »
    In fact, the Stamp can just barely manage 3 servo signals at once, without any outside hardware. You have to nest all your pulsouts in a loop and execute that loop each 20 ms, so you can't do a whole lot else, but you can read some inputs or sensors and/or do some quick calculations. Will that suffice for you? Quicker & easier than adding external hardware.
    It's very sufficient. If you could write an example i would understand better. For the other 2 motors i will use 2 pins for start stopp only. I will have 2 ultrasonic sensors and one to three infrared sensors. I have to move the servo in a specific angle and start the brushed motor attached to it when the sensors send a specific distance signal.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-06-29 08:26
    With the number of sensors you want to use, you're going to have a hard time. Worst case, 3 servo signals can take nearly 10ms (2ms each plus overhead). If you're using a PING))) or something like it for ultrasonic sensors, these can take 18ms each worst case before they time out. At this point, you're already past your time budget for a 20ms cycle. You can get around this by cycling through the sensors using one ultrasonic sensor in one 20ms cycle, then the other ultrasonic sensor in another 20ms cycle, then the IR sensors in a 3rd 20ms cycle, but that complicates the programming and there are no ready-made examples. Roaming with the PING))) that I mentioned earlier illustrates how to manage 3 servos and a PING))), but it doesn't do it all at once. The BoeBot scans the environment using the PING))) and one servo, then moves for a time using the other 2 servos.

    What you want to do is definitely not a beginner's project and it's probably quite marginal with a Stamp without external hardware. It's not something we can show you how to do in a forum setting. You'd have to become quite good at Stamp programming yourself and use the Robotics with the BoeBot tutorial as a base along with Roaming with the PING))).
  • ercoerco Posts: 20,256
    edited 2011-06-29 08:38
    Start with this quickie code and modify with your sensor inputs:

    pwm=450' stop motor on pin0
    servo1=750 'center servo1 on pin1
    servo2=750 'center servo2 on pin2

    start:
    pulsout 0,pwm ' set motor speed
    pulsout 1,servo1 ' set servo1 position
    pulsout 2,servo2 ' set servo2 position
    gosub sensor1
    gosub sensor2
    gosub sensor3
    if sense1=x then pwm=670 else pwm=450 ' change motor speed if sense1=x
    if sense2=y then ... ' add your own condition 2
    if sense3=z then ... ' add your own condition 3
    pause ??? ' adjust this pause value to yield a 20 ms pause including sensor checks and IF checks
    goto start

    sensor1:
    ...
    return
    sensor2
    ...
    return
    sensor3
    ...
    return

    The loop needs to execute about 50 times per second to keep the servos supplied with a pulse each 20 ms. If your total sensor readings and code take a lot of execution time, longer than 15 ms or so, you may have to get tricky and only read one sensor per loop, and rotate through which sensor gets read. You could do a for/next loop for that. Hope this helps!

    Edit: Mike beat me to replying! We're in agreement that it's gonna be close on Stamp capability and you'll have to poll your sensors individually. Give it a whirl and let us know how it goes.
  • Ady111Ady111 Posts: 15
    edited 2011-06-29 10:01
    erco wrote: »
    Start with this quickie code and modify with your sensor inputs:

    pwm=450' stop motor on pin0
    servo1=750 'center servo1 on pin1
    servo2=750 'center servo2 on pin2

    start:
    pulsout 0,pwm ' set motor speed
    pulsout 1,servo1 ' set servo1 position
    pulsout 2,servo2 ' set servo2 position
    gosub sensor1
    gosub sensor2
    gosub sensor3
    if sense1=x then pwm=670 else pwm=450 ' change motor speed if sense1=x
    if sense2=y then ... ' add your own condition 2
    if sense3=z then ... ' add your own condition 3
    pause ??? ' adjust this pause value to yield a 20 ms pause including sensor checks and IF checks
    goto start

    sensor1:
    ...
    return
    sensor2
    ...
    return
    sensor3
    ...
    return

    The loop needs to execute about 50 times per second to keep the servos supplied with a pulse each 20 ms. If your total sensor readings and code take a lot of execution time, longer than 15 ms or so, you may have to get tricky and only read one sensor per loop, and rotate through which sensor gets read. You could do a for/next loop for that. Hope this helps!

    Edit: Mike beat me to replying! We're in agreement that it's gonna be close on Stamp capability and you'll have to poll your sensors individually. Give it a whirl and let us know how it goes.
    Thanks a lot for your reply. Tomorow morning i will try to make the program with your ideea...i think i got it. If the ultrasonic sensors are a problem i can change them with infrared sensors so i can read analog input. I will use an oscilloscope to see all the signals involved and i will reply.
  • ercoerco Posts: 20,256
    edited 2011-06-29 10:13
    XLNT. If you get bogged down polling the sensors individually, then just stretch the program out, you have plenty of program space. Along these lines:

    start:
    send 3 pulsouts
    read sensor 1
    If check...
    pause n1
    send 3 pulsouts
    read sensor 2
    If check...
    pause n2
    send 3 pulsouts
    read sensor 3
    If check...
    pause n3
    goto start
  • Ady111Ady111 Posts: 15
    edited 2011-06-29 12:01
    erco wrote: »
    XLNT. If you get bogged down polling the sensors individually, then just stretch the program out, you have plenty of program space. Along these lines:

    start:
    send 3 pulsouts
    read sensor 1
    If check...
    pause n1
    send 3 pulsouts
    read sensor 2
    If check...
    pause n2
    send 3 pulsouts
    read sensor 3
    If check...
    pause n3
    goto start
    I thnik that's the ones of the most reliable way to finish my tasks. Thank's for the ideea
  • Ady111Ady111 Posts: 15
    edited 2011-07-01 08:15
    For the moment i have renounced to use the sensors. I want to make a program so that my project works following a specific path that i make. I maked the 2 brushed motors to start and stop when a pin from the stam is set on logical 1 or 0, saving time and instrictions. If you want to make a better impression about my project here is a picture of it.
    1024 x 613 - 61K
  • logan996logan996 Posts: 281
    edited 2011-07-01 09:58
    erco wrote: »
    For your motor PWM (which is a fixed speed), you might consider using an external signal generator, like a 555/556 IC. Simple, and you can control (enable) it with one Stamp pin using a High/Low command. You'll adjust the motor speed with a potentiometer on the 555/556. That would leave the Stamp to read your sensors and drive 2 servos, which it can easily do.

    You could control the pot via the BS2 using the digital pot. http://www.parallax.com/StoreSearchResults/tabid/768/List/0/SortField/4/ProductID/246/Default.aspx?txtSearch=digital+pot
  • Ady111Ady111 Posts: 15
    edited 2011-07-01 14:35
    logan996 wrote: »
    I don't understand what are you trying to say.
  • Mike GMike G Posts: 2,702
    edited 2011-07-01 14:52
    Ady111, consider using a PWM controller as suggested in thread #2

    A PWM controller can really open up your BS2 project.
  • ercoerco Posts: 20,256
    edited 2011-07-01 22:58
    Even a simple $15 Servopal can offload 2 channels of pwm processing: http://www.parallax.com/tabid/768/txtSearch/servopal/List/0/SortField/4/Default.aspx
  • Ady111Ady111 Posts: 15
    edited 2011-07-02 12:19
    erco wrote: »
    Even a simple $15 Servopal can offload 2 channels of pwm processing: http://www.parallax.com/tabid/768/txtSearch/servopal/List/0/SortField/4/Default.aspx
    It's not about money, and you are right it's a very usefull tool for me this servopal, but i don't have this chip and the shipping would last longer than a week. I have to handle all the tasks with the bs2.
  • ercoerco Posts: 20,256
    edited 2011-07-02 12:24
    I like your persistence. How's it coming? Baby steps..!
  • Ady111Ady111 Posts: 15
    edited 2011-07-04 08:53
    erco wrote: »
    I like your persistence. How's it coming? Baby steps..!
    I maneged to run the brushless motor, now i'm trying to move the servos while the brushless is running. It's harder than i thought. I run the brushless with the speed i want with the comand pulsout 8, 650 and after i give the comand to the servo to move pulsout 2, 680 but it doesen't do a thing. where am i wrong?
  • ercoerco Posts: 20,256
    edited 2011-07-04 14:45
    Keep on loopin' Brother...

    a:pulsout 8,650
    pulsout 2,680
    pause 20
    goto a
  • Mike GMike G Posts: 2,702
    edited 2011-07-05 10:19
    @Ady111, as you increase the number of PWM device in the loop, you 'll need to decrease the pause value. There will be a point where you can no longer keep a 50 pulse/second rate while executing other logic like reading a sensor and updating a variable. See post #2.

    Anyway, post you code so we can see what you're trying to do.
  • Ady111Ady111 Posts: 15
    edited 2011-07-10 08:24
    Mike G wrote: »
    @Ady111, as you increase the number of PWM device in the loop, you 'll need to decrease the pause value. There will be a point where you can no longer keep a 50 pulse/second rate while executing other logic like reading a sensor and updating a variable. See post #2.

    Anyway, post you code so we can see what you're trying to do.
    i have abandoned de ideea of using a sensorial sistem. it's way to much for the BS2. i VAR Byte
    PAUSE 2000
    FOR i=1 TO 200
    PULSOUT 8, 450
    PAUSE 10
    NEXT
    FOR i=1 TO 250
    PULSOUT 8, 500
    PAUSE 5
    NEXT
    FOR i=1 TO 250
    PULSOUT 8, 550
    PAUSE 5
    NEXT
    FOR i=1 TO 250
    PULSOUT 8, 600
    PAUSE 5
    NEXT
    program:
    PULSOUT 8, 650


    PAUSE 20
    GOTO program
    with this loop i managed to start the brushless motor in ramp mode and keeping it on the speed i desire. I have to add now the comands for the servos and the brusheds.
  • Mike GMike G Posts: 2,702
    edited 2011-07-10 09:26
    i have abandoned de ideea of using a sensorial sistem. it's way to much for the BS2.

    The BS2 can handle sensors no problem. You decided to use the STAMP as a PWM controller and that's fine. It does not mean the STAMP can't handle sensors.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    i VAR Byte
    PAUSE 2000
    
    FOR i=1 TO 200
      PULSOUT 8, 450
      PAUSE 10
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 500
      PAUSE 5
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 550
      PAUSE 5
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 600
      PAUSE 5
    NEXT
    
    program:
      PULSOUT 8, 650
      'Do something
      'Do something
      'Do something
      PAUSE 20 'minus the time it takes to do stuff
    GOTO program
    
  • Ady111Ady111 Posts: 15
    edited 2011-07-10 13:34
    Mike G wrote: »
    The BS2 can handle sensors no problem. You decided to use the STAMP as a PWM controller and that's fine. It does not mean the STAMP can't handle sensors.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    i VAR Byte
    PAUSE 2000
    
    FOR i=1 TO 200
      PULSOUT 8, 450
      PAUSE 10
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 500
      PAUSE 5
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 550
      PAUSE 5
    NEXT
    
    FOR i=1 TO 250
      PULSOUT 8, 600
      PAUSE 5
    NEXT
    
    program:
      PULSOUT 8, 650
      'Do something
      'Do something
      'Do something
      PAUSE 20 'minus the time it takes to do stuff
    GOTO program
    
    I have to mention that if the regulator of the brushless loses the signal for 1 sec he automatically stops the motor. sometimes this happens. Still not sure why. If i change the "do something" with FORs that include the pulsout for the brushless and the comands for the other motors making timed loops will it work? Anyway i also need a condition to exit this "program" loop. I had an idea of using the "BRANCH" command, but i don't know if it will help me and the syntax. I want to have an constant that i increment every time i send a set of pulses and depending on this constant to go to different subroutines, finally going to the end program loop that keeps my bs2 busy with an endless doing nothing loop.
  • Mike GMike G Posts: 2,702
    edited 2011-07-10 14:38
    Ady111, all PULSOUT commands have to execute in the same tight loop. You should not nest a FOR...NEXT loop in your tight loop.

    Here's a simple example. Do not use this with your motors as it uses the DEBUG command and a 1 second pause. At some point you'll also need to update speed/position values and that takes time as well.
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    
    i     VAR   Byte  ' For next loop
    m1    VAR   Word  ' Motor 1
    m2    VAR   Word  ' Motor 2
    m3    VAR   Word  ' Motor 3
    cnt   VAR   Nib   ' Conter
    dir   VAR   Bit   ' Direction
    
    'Initialize
    m1 = 650
    m2 = 700
    m3 = 200
    dir = 0
    
    ' Main loop
    Main:
      PULSOUT 8, m1
      PULSOUT 1, m2
      PULSOUT 2, m3
    
      GOSUB LoadValues
      PAUSE 10
    
      GOSUB Display
    GOTO Main
    
    ' Load new speed/position values
    LoadValues:
      IF dir THEN
        m1 = m1 + 1
        m2 = m2 + 1
        m3 = m3 + 1
      ELSE
        m1 = m1 - 1
        m2 = m2 - 1
        m3 = m3 - 1
      ENDIF
    
      cnt = cnt + 1
      IF cnt = 15 THEN dir = dir + 1
    RETURN
    
    ' Dump values for testing loop
    Display:
      DEBUG ?m1, ?m2, ?m3, ?cnt, ?dir
      PAUSE 1000
      DEBUG CLS
    RETURN
    

    Good luck... I'd pick up a PWM controller.
Sign In or Register to comment.