Shop OBEX P1 Docs P2 Docs Learn Events
Ramp coding problem — Parallax Forums

Ramp coding problem

Slo-botSlo-bot Posts: 4
edited 2009-05-09 09:07 in Robotics
Hi guys!

I'm working my way through the BoeBot projects and have completed the whisker project.· I'm trying to modify the coding in the book to ramp up into motion and turns.· I can get all the subroutines sorted apart from the forward subroutine as it is designed to only pulse forward once before going off to check the whisker states.· I've tried adding FOR-NEXT and DO-LOOP routines but can't get it right.

Ideally I need something like:
FOR pulsecount = 1 to (somehow I'm thinking inifite here)
PULSOUT 13, 750 + pulsecount MAX 829·· '829 for max speed without arcing
PULSOUT 12, 750 - pulsecount MIN 650
PAUSE 20
NEXT

Any ideas on what I could use that would have the same effect as the rampup routine above, whilst being able to check whisker states regularly?

Any help much appreciated
Thanks!

Comments

  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-04 21:19
    ramp VAR byte
    
    for ramp = 1 to 100 step 10
    pulsout 13, 750 + ramp
    pulsout 12, 750 - ramp
    gosub checkWhiskers
    pause 20   (probably less depending on how long it takes to check whiskers)
    next 
    
    



    Would that work?

    Rich H
  • SRLMSRLM Posts: 5,045
    edited 2009-05-04 21:54
    Here is a project I did a while back. It has a ramp routine in the Roving.bse under _Move.

    http://forums.parallax.com/showthread.php?p=741023
  • Slo-botSlo-bot Posts: 4
    edited 2009-05-09 07:15
    Thanks for the help.· I've managed to come up with something based on what I've read.· I'm sure there's an easier way but for now it'll do.· Here's what I came up with:
    DO
    IF (leftcount = 829) AND (rightcount = 650) THEN
    GOSUB forwardpulse
    ELSE
    PULSOUT 13, leftcount
    PULSOUT 12, rightcount
    PAUSE 20
    leftcount·= leftcount + 3 MAX 829
    rightcount = rightcount - 3 MIN 650
    ENDIF

    Then loop then carries on to check the whiskers·smile.gif
  • SRLMSRLM Posts: 5,045
    edited 2009-05-09 07:33
    I'm not quite sure exactly how your code is working, so it prompts a couple of code suggestions: never use magic number(829, 650, 13, 12), instead use constants; comment your code as much as you think is necessary, then add some more.

    Of course, these are just suggestions (not hard and fast rules) but they help others understand your code, and you to understand it in a month.
  • Slo-botSlo-bot Posts: 4
    edited 2009-05-09 07:54
    Ok I think I know what you mean, the values 829 and 650 can be held as a constant so instead of using numbers I could simply call it something like "maxspeed" is that what you're meaning?· I've only just begun on this under 2 weeks ago so forgive my lack of knowledge.· As for 12 and 13, they're the pins on the stamp which the servos are connected to, I'm not yet sure how I could do away with these numbers???
    I do comment my code alot, especially as I'm very forgetful!

    Here is the code I used.· I can't find the files when I use the attachment manager, they don't show up even though I know they're there so sorry if the big post bugs you smilewinkgrin.gif I havn't fine tuned my pause statements or ramping speeds yet either.
    leftcount VAR Word
    rightcount VAR Word
    pulsecount VAR Word
    
    leftcount = 750                       'Initial value for stationary servo
    rightcount = 750                      'Initial value for stationary servo
     
    
    DO                                                'Main Routine contained within this DO-LOOP
    IF (leftcount = 829) AND (rightcount = 650) THEN  'If Bot already moving at max speed THEN
    GOSUB forwardpulse                                'forwardpulse to maintain speed
    ELSE                                              'If not yet at max speed then pulse each
    PULSOUT 13, leftcount                             'servo adding 3 to the pulse count of each
    PULSOUT 12, rightcount                            'servo.
    PAUSE 20                                          'This results in a ramped up speed for smooth
    leftcount = leftcount + 3 MAX 829                 'acceleration.
    rightcount = rightcount - 3 MIN 650               'MAX and MIN values are those which maintain
    ENDIF                                             'a straight course ON a tiled surface.
    
    'Start of sensing routine.  Feeler states checked each time through the loop.
    
    IF (IN5 = 0) AND (IN7 = 0) THEN  'Both feelers contacting obstacle.
    HIGH 10                          'HIGH outputs light LED's to visually indicate feeler contact
    HIGH 1
    GOSUB backup                     'BOT hits obstacle head on so reverses and executes a U-turn
    GOSUB uturn
    ELSEIF (IN5 = 0) THEN            'Left feeler contacted
    HIGH 10                          'Left LED lights
    GOSUB backup                     'Bot reverses and turns 90 degrees right to move away
    GOSUB turnright                  'from the left hand obstacle.
    ELSEIF (IN7 = 0) THEN            'Right feeler contacted
    HIGH 1                           'Right LED lights
    GOSUB backup                     'Bot reverses and turns 90 degrees left to move away
    GOSUB turnleft                   'from the right hand obstacle.
    ELSE
    LOW 10                           'If Bot is in motion and no feelers are contacted it will
    LOW 1                            'carry ON forward untill an obstacle is detected.
    ENDIF                            'In the meantime no LED's are lit.
    LOOP                             'End of main loop
    forwardpulse:                    'Single pulse forward before returning to 'feeler state' loop
    PULSOUT 13, 829
    PULSOUT 12, 650
    PAUSE 20
    RETURN
    turnleft:                            '90 degree turn left.
    PAUSE 200                            'Pause is added before and after for accurate turn
    FOR pulsecount = 1 TO 65 STEP 2      'Turn is ramped to prolong servo life
    PULSOUT 13, 750 - pulsecount MIN 672
    PULSOUT 12, 750 - pulsecount MIN 650
    PAUSE 20
    NEXT
    leftcount = 750                      'Servo speed returned to zero.
    rightcount = 750                     'Bot will ramp up forwards again
    PAUSE 50
    RETURN
                                         '90 degree turn right.
    turnright:                           'Pause is added before and after the manouevre to
    PAUSE 200                            'ensure that an accurate turn is executed.
    FOR pulsecount = 1 TO 65 STEP 2      'Turn is ramped to prolong servo life
    PULSOUT 13, 750 + pulsecount MAX 829
    PULSOUT 12, 750 + pulsecount MAX 850
    PAUSE 20
    NEXT
    leftcount = 750                      'Servo speed returned to zero.
    rightcount = 750                     'Bot will ramp up forwards again.
    PAUSE 50
    RETURN
                                         'Pause before reversing to ensure servos have stopped
                                         'rotating.  Approx 1cm to come to a standstill so BOT
    backup:                              'still has room left before  fully coming into contact
    PAUSE 200                            'with any obstacles.
    FOR pulsecount = 1 TO 120 STEP 4     'Reverse is ramped to prolong servo life.
    PULSOUT 13, 750 - pulsecount MIN 672
    PULSOUT 12, 750 + pulsecount MAX 850
    PAUSE 20
    NEXT
    leftcount = 750                      'Servo speed returned to zero.
    rightcount = 750                     'Bot will ramp up forwards again.
    RETURN
    uturn:                               'Bot pauses before executing a U-Turn
    PAUSE 200                            '(180 degrees spin)
    FOR pulsecount = 1 TO 100 STEP 2     'Turn is ramped to prolong servo life.
    PULSOUT 13, 750 - pulsecount MIN 672
    PULSOUT 12, 750 - pulsecount MIN 650
    PAUSE 20
    NEXT
    leftcount = 750                      'Servo speed is returned to zero.
    rightcount = 750                     'Bot will ramp up forwards again.
    PAUSE 50
    RETURN
    
  • SRLMSRLM Posts: 5,045
    edited 2009-05-09 08:46
    Here are some methods to make the code a bit more clear:

    leftservo     PIN    13
    
    maxvalue   CON   873
    
    
    



    Also, white space in reasonable amounts is your friend. You can also add comments aligned left, instead of feeling limited to comments stuck on the right. A good program probably has both. As a personal preference, I like to put in a big multiline comment that is simply a big line, with the word SUBROUTINE, to indicate that everything afterwards is subroutines.

    I'd like to compliment you on your well commented code. I work with beginning programmers every day, and they have the bad habit of choosing "a,b,c,x,y,z" for their variable names, and not commenting at all! And, they've been programming for 6 weeks now...
  • Slo-botSlo-bot Posts: 4
    edited 2009-05-09 09:07
    Thanks!

    Now that I understand how to label the pins, it should be much easier work through coding with simplifying tricks like that.· I see what you mean about white space now, I didn't bother looking at the code once I'd pasted it in here but now that I do look I notice it's taken out a whole bunch of unused lines.· I had compressed it all down to fit 2 pages of A4 also so I could print it and hand it in as part of an assessment.



    Thanks for your tips, I'll keep pottering my way through this Boe-bot book and see if I can improve on the given coding tongue.gif .· Gotta say I'm enjoying this stuff more than I thought I would.
Sign In or Register to comment.