Shop OBEX P1 Docs P2 Docs Learn Events
Running two parts of a program at the same time — Parallax Forums

Running two parts of a program at the same time

MarioMario Posts: 1
edited 2007-01-11 14:02 in Learn with BlocklyProp
·I'm building a very simple robot that goes forward and when there is an object less than 30 cm away, it turns. I'll add some more things later on, but for now, how can I make the servos work without pulsing them, i mean having a smooth motion? I'm using PULSOUT as a command and I'm using a PING))) SENSOR. To fix the problem I tried taking away the PAUSE line in the Pinging section but now the servos work, but the Ping is checking for object 200 times a second.... I would like to check just like twice or so a second. In other words, i'd like for Address1 to run WHILE Address2 is running so that I can run two parts of the program at the same time. How can I do that?

EDITS::

Couch-pilot I can't use this because then for a fraction of a second the actions in the loop won't run. It would be fine for the servos, but I also implemented another piece that checks for a remote control button to be pressed to activate a servo. I need for the check to be always run, WHILE running the rest of the program.

Attached is my program so far. I added comments for easier readability...

2 EDIT::

Thank you all for ur help... I'll try to fix up my code. I was just wondering, do you guys think that using the ping sensor like 500 times a second to eliminate the pulses as an alternative to couch-pilot's code would break the sensor or heat it up??

Post Edited (Mario) : 12/19/2006 7:03:38 PM GMT

Comments

  • boeboyboeboy Posts: 301
    edited 2006-12-19 14:39
    can i see the code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lets see what this does... KA BOOM (note to self do not cross red and black)
  • BenoitBenoit Posts: 47
    edited 2006-12-19 14:57
    You could use a variable in a "pre-scaler" mode. Something like this:

    PingDelay VAR word
    PingTimeOut CON 500 'This is an arbritrary number, and has to be adjusted depending on the number of pbasic statements executed in the loop
    DO

    <PULSOUT Right_motor>
    <PULSOUT Left_motor>

    PingDelay = PingDelay +1
    IF PingDelay > PingTimeOut THEN
    'Do the PING thing
    PingDelay=0
    ENDIF

    LOOP

    -Ben
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2006-12-19 17:00
    Hi Mario, one of the first things I noticed was that PINGsensor and Get_Sonar always run·as one·and so do Turn and Turn2 so I think you could condense to 6 subroutines instead of 8

    ·1)Servorun 2)PINGsensor 3)Obstacle 4)Turn 5)Forward and 6)Remote

    Then get rid of the GOTO statements and call the subroutines as required. Unfortunately you can't run 2 routines at the same time so you have to try and overcome your problem with timing/speed. If you need to constantly monitor the remote increase the number of calls to that sub and nest the call within the other subs. Reducing the call to the PING to once or twice a second then Ben's solution is ideal, adjust the PingDelay to fine tune until you get the timing desired.

    hope this helps

    Jeff T.
  • AImanAIman Posts: 531
    edited 2007-01-10 13:37
    There are some things to consider if you are still working on this program.

    First, run your Sonar loop as a nested loop. Example - Do servo·if servo >= 100 revolutions·run sonar end if loop

    Second - if you prefer not to do that consider simply inserting a pause between sub routines. I mean pausing the program for the sonar to run after the servo goes a set time. You will have a slight hickup in movment but it will be minor.

    Third - reverse number 1 so that the sonar loop always runs and the servo is a do While loop.Example - Do sonar - do while sonar >= 10 inches end while loop.

    Fourth - run a Propeller and do both at once. Kidding [noparse]:)[/noparse] If you now how to link stamps or have a coprocessor you could hook up one to the master and the other to the slave.
  • Steve JoblinSteve Joblin Posts: 784
    edited 2007-01-11 00:17
    Since the Stamp is a single threaded processor, many third parties have created "co-processors" that off load simple tasks. Once such devise is the Dual Serial Motor Controller by Pololu... simple, small, and inexpensive it is a great way to have the Stamp send one command once and the servo will keep doing it until you give it new instructions. This frees ups the Stamp to focus on other things like take readings from the Ping))).... check it out at http://www.pololu.com/products/pololu/0101/
  • edited 2007-01-11 01:08
    I'm confident you can get this done with a BASIC Stamp 2.

    The problem appears to be the way the program is checking for an incoming IR message from the remote - with the RCTIME command. Instead, check for a low signal from the IR detector first, and if it's low, then do the RCTIME command, and don't let the program check the Ping))) until it's done with the IR message and a refreshing pulse to the servo. You may need to check the IR output several times in order to make sure you don't miss the message, but just check for the low signal. If it's low, then do the RCTIME command.

    Another thing to do is set it up so that it check's the Ping))) sensor between one set of servo pulses, and then checks for IR messages between the next set of servo pulses.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
  • AImanAIman Posts: 531
    edited 2007-01-11 14:02
    Andy is right about running your sonar loop within the servo pause, I do that all the time. (A variation of my nested loop statement.)

    WARNING!!!

    One of my robots ran 3 sonar within the·serov's loop. If you get to many sonar·within a servo loop·the servo will jitter at the start and end of its movment. If you take the sonar out of the loop then the servo's won't jitter. This robot had only·2 servo's and they started to jitter when there was that much within the loop. I was supposed to run 4 servo's ...
Sign In or Register to comment.