Shop OBEX P1 Docs P2 Docs Learn Events
Storing pulseCount Data — Parallax Forums

Storing pulseCount Data

HatemHatem Posts: 20
edited 2008-04-28 21:10 in BASIC Stamp
Hello ... How do I store the value of pulseCounts used to drive the servos at a certain period of time·then recall this value at another time. For example, the following code is used in my program:
·
FOR pulseCount = 0 TO 2310··· ' PulseCount to Drive·396 Inches forward········
····PULSOUT LeftServo, 850
··· PULSOUT RightServo, 650
··· PAUSE 20
NEXT

·
if an object is detected,·then my Bot will maneuver around it using the following simple code:

FOR pulseCount = 0 TO 20························ ' 90 Degrees Right Turn
··· PULSOUT LeftServo, 850
··· PULSOUT RightServo, 850
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 70······················ ' 12 inches
··· PULSOUT LeftServo, 850
··· PULSOUT RightServo, 650
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 20····················· ' 90 Degrees Left Turn
··· PULSOUT LeftServo, 650
··· PULSOUT RightServo, 650
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 140··················· ' Avoid Object
··· PULSOUT LeftServo, 850
··· PULSOUT RightServo, 650
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 20···················· ' 90 Degrees Left Turn
··· PULSOUT LeftServo, 650
··· PULSOUT RightServo, 650
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 70···················· ' 12 Inches
··· PULSOUT LeftServo, 850
··· PULSOUT RightServo, 650
··· PAUSE 20
· NEXT

· FOR pulseCount = 0 TO 15··················· ' 90 Degrees Right Turn
··· PULSOUT LeftServo, 850
··· PULSOUT rightServo, 850
··· PAUSE 20
· NEXT



The problem is how can i recall the pulseCount value·when an object is detected and use it after maneuvering to calculate for the remaining pulses needed to drive the servos for the remaining distance??
·
Let us say that an object is detected at 200 Inches, I must obtain the pulseCount at this time, and·after manuvering,·i must recall the value to use it in the following logic:

pulseCount·=pulseCount + 140·· ( This is the covered Distance)
reamining distance is 2310 - pulseCount

Thank you

Comments

  • jmalaysiajmalaysia Posts: 97
    edited 2008-04-06 01:38
    I don't see a need to store it since it is a fixed value each time an object is avoided. Just account for the avoidance distance·in the main loop.


    FOR pulseCount = 0 TO 2310 ' PulseCount to Drive 396 Inches forward
    IF Objectavoided then pulsecount=pulsecount+140
    PULSOUT LeftServo, 850
    PULSOUT RightServo, 650
    PAUSE 20
    NEXT

    Post Edited (jmalaysia) : 4/6/2008 1:44:25 AM GMT
  • HatemHatem Posts: 20
    edited 2008-04-06 03:55
    That is not correct ... It must figure out how much distance left. Like i mentioned earlier, if it detected an object at 200 inches, then manuvered around it, it must calculate the remainig distance. This is done by storing the pulseCount vlaue when an object is detected then adding to it the 140 from the manuver to avoid the object. The remaining pulseCount is the total pulseCount what is covered.

    (pulseCount when object is detected + 140) = Distance Covered

    Original value for pulseCount which is 2310 - Distnace covered = remaining pulses needed.

    I am not sure hot to store the pulseCount value when the object is detected and recall it later after manuvering? I was trying to use the Read command but it did not work!!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-06 04:03
    Just use a new variable. Your main movement routine would look like:
    StartFresh:
      InitialValue = 0
    DoAMove:
      FOR pulseCount = InitialValue TO 2310
        IF ObjectFound THEN
          GOSUB GoAroundIt
          InitialValue = pulseCount + AdjustmentForAvoidance
          GOTO DoAMove
        ENDIF
        PULSOUT LeftServo, 850
        PULSOUT RightServo, 650
        PAUSE 20
      NEXT
    


    Oops, see change above

    Post Edited (Mike Green) : 4/6/2008 4:31:35 AM GMT
  • HatemHatem Posts: 20
    edited 2008-04-06 04:21
    Thanks Mike ... That make sense. I tested your method and it worked fine.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-06 04:33
    Oops ... see change above to "InitialValue = pulseCount + AdjustmentForAvoidance"
  • HatemHatem Posts: 20
    edited 2008-04-28 17:10
    Hey guys,

    I need some explanation for what my Bot is doing; I am using the following simple code:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    pulseCount VAR Byte ' Used For Measuring Turns
    RightServo PIN 12 ' Right Servo is connected to pin 12
    LeftServo PIN 13 ' Left Servo is connected to pin 13
    task VAR Nib ' Current Task Being Performed
    InitialValue VAR Nib ' Pulse value for task0,2,4
    Piezo PIN 4 ' Piezo Speaker is connected to pin 4

    GOSUB Drive
    END

    Drive:

    InitialValue = 0
    FOR pulseCount = InitialValue TO 272 *** This is where my problem is .... Please read below
    PULSOUT LeftServo, 850
    PULSOUT RightServo, 675
    PAUSE 20
    NEXT
    RETURN

    when i set my value to 68, my bot will go forward for 12 Inches ... But when i set my vlaue for 272 to have my Bot drive forward for 48 Inches, It just keep going forward for ever. I am not sure why it is doing that, can some one please help. Thank you
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-28 17:19
    You declared pulseCount as a byte which can only hold a value from 0 to 255. pulseCount can never reach the value of 272, so the FOR loop never stops. Try declaring pulseCount as a word.

    By the way, you have 3 identical messages. Please delete the extra ones (use the X in the upper right corner of the message window).
  • HatemHatem Posts: 20
    edited 2008-04-28 20:57
    Thanks Mike, that fixed the problem. Another thing I want to ask is related to the current Task the Bot is executing. I am using the following to have my Bot execute these tasks in order but it only executes Task0 and stops:

    pulseCount VAR Word ' Used For Measuring Turns
    RightServo PIN 12 ' Right Servo is connected to pin 12
    LeftServo PIN 13 ' Left Servo is connected to pin 13
    Task VAR Nib ' Current Task Being Performed
    pulse VAR Byte ' Pulse value for task0,2,4
    InitialValue VAR Word ' Pulse value for task0,2,4
    Piezo PIN 4 ' Piezo Speaker is connected to pin 4


    PAUSE 2000
    FREQOUT Piezo, 100, 3000 ' Buzzer Initiate Indicating



    ON Task GOSUB Task0, Task1, Task2, Task3, Task4
    END

    Task0:

    InitialValue = 0
    FOR pulseCount = InitialValue TO 2310
    PULSOUT LeftServo, 850 ' Forward for 396 Inches
    PULSOUT RightServo, 675
    PAUSE 20
    NEXT
    RETURN

    Task1:
    FOR pulseCount = 0 TO 20 ' 90 Degrees Left Turn
    PULSOUT LeftServo, 650
    PULSOUT RightServo, 650
    PAUSE 20
    NEXT
    RETURN

    Task2:

    InitialValue = 0
    FOR pulseCount = InitialValue TO pulse
    PULSOUT LeftServo, 850 ' Forward for 408 Inches
    PULSOUT RightServo, 675
    PAUSE 20
    NEXT
    RETURN

    Task3:

    FOR pulseCount = 0 TO 20 ' 90 Degrees Left Turn
    PULSOUT LeftServo, 650
    PULSOUT RightServo, 650
    PAUSE 20
    NEXT
    RETURN

    Task4:
    InitialValue = 0
    FOR pulseCount = InitialValue TO pulse
    PULSOUT LeftServo, 850 ' Forward for 60 Inches
    PULSOUT RightServo, 675
    PAUSE 20
    NEXT
    RETURN
  • Mike GreenMike Green Posts: 23,101
    edited 2008-04-28 21:06
    That's what your program is written to do. It executes the ON / GOSUB statement once, then stops at the END statement. If you want to execute other routines, you need to call them in whatever order you want. Read the description of the ON statement in the manual. You'll see it selects one of a list of labels to GOSUB to. You probably want a series of GOSUB statements, one per line.
  • allanlane5allanlane5 Posts: 3,815
    edited 2008-04-28 21:10
    Correct. So you need a little loop:

    MAIN:
    On Task Gosub ...
    Task = Task + 1
    ·IF Task > 5 THEN
    · Task = 0
    ·ENDIF
    GOTO MAIN
Sign In or Register to comment.