Shop OBEX P1 Docs P2 Docs Learn Events
Activity bot Sensors Question — Parallax Forums

Activity bot Sensors Question

GoBearsGoBears Posts: 4
edited 2017-08-31 13:44 in General Discussion
Does anyone know if I can use more than one sensor on the activity bot at the same time? I am trying to use the whiskers as well as the ultrasound at the same time but they have conflicting pins. I moved the ultrasound to a new pin but I am unsure of how to specify that in the simple IDE code. All help is appreciated.

Comments

  • ercoerco Posts: 20,250
    Sure, you can move a sensor to another pin, all you have to do is change the pin assignments in the code. What's your specific example?
  • As erco says, it's doable. Which Mico do you have in mind?
  • Hello again. So I found a way to change the pins in code. I am now attempting to use the whiskers as well as the IR sensors at the same time. I used the terminal to test to make sure they are both operational, but when ever I try to have the robot move forward and navigate with the IR sensors, then move backwards if a whisker is pressed, the robot completely ignores what happens with the whiskers. I am sure that both sensors are working because I confirmed that with the terminal. I am attempting to use multiple if else statements inside of a while loop but it seems that the more complex the code is, the less responsive the activity bot is. Has anyone else experienced the same?
  • What language are you using? Please post your code using [ code ] and [ /code ] brackets (without the extra spaces).

    The Propeller in the ActivityBot can do up to 8 things at a time. It takes time for the Bot to get the distance info from the Ping and that slows down other processing done in the same cog (like deciding what to do). By moving the Ping timing to another cog, you may free up a lot of processing time to the cog running the main program.
  • I wrote a program in C using SIMPLEIDE that controls the ActivityBot with a Sony TV remote and that uses the whiskers to detect and avoid obstacles. To get the bot to react quickly to the obstacle I have the whisker code run in a second cog. This thread has a couple versions of the program:
    forums.parallax.com/discussion/161769/activitybot-sirc-control-whiskers

    Hope this helps,
    Tom
  • Hello again. Thanks so much for all of the responses. I'm learning a lot as I continue to mess with the bot day to day.

    So I'm using propeller C to program. Right now I'm just trying to get the bot to go down the hallway without hitting things. I looked at twm's code and figured out how to use different cores to run multiple functions at the same time.

    When my bot sees an obstacle it turns way more than it should, sometimes all the way around, but I'm still tweaking the code as I go. Right now its a struggle trying to make sure the different functions running of each of the cores don't conflict with each other. Sometimes I also feel like the bot gets stuck in if loops and I don't know why. I wish there was an easier way to debug in propeller C. I'm still going to keep messing with it as I go. I've posted my code below. Any further help would also be appreciated. Thanks again!


    /*
    Roam W/ Ultrasound & IR
    */
    #include "simpletools.h" // Include simple tools
    #include "abdrive.h"
    #include "ping.h" // Include ping header

    void ultrasound();
    void infrared();
    int flag;

    int main() // Main function
    {
    flag = 0;
    cog_run(ultrasound, 128);
    cog_run(infrared, 128);
    }

    void ultrasound() {
    int turn; // Navigation variable

    while (1) {

    drive_speed(100, 100); // Forward

    // While disatance greater than or equal
    // to 10 cm, wait 5 ms & recheck.
    while (ping_cm(8) >= 10) pause(5); // Wait until object in range
    if (flag == 0)
    drive_speed(0, 0); // Then stop
    if (flag == 0)
    drive_speed(0, 0); // Then stop

    // Turn in a random direction
    turn = rand() % 2; // Random val, odd = 1, even = 0

    if (turn == 1) // If turn is odd
    drive_speed(64, -64); // rotate right
    else // else (if turn is even)
    drive_speed(-64, 64); // rotate left

    // Keep turning while object is in view
    while (ping_cm(8) < 10); // Turn till object leaves view
    if (flag == 0)
    drive_speed(0, 0); // Stop & let program end
    }
    }

    void infrared() {
    int irLeft, irRight; // IR variables

    low(26); // D/A0 & D/A1 to 0 V
    low(27);

    while (1) {
    freqout(11, 1, 38000); // Check left & right objects
    irLeft = input(10);

    freqout(1, 1, 38000);
    irRight = input(2);

    if (irRight == 0) // Just right obstacle?
    {
    flag = 1;
    drive_speed(-50, 50); // ...rotate left
    } else if (irLeft == 0) // Just left obstacle?
    {
    flag = 1;
    drive_speed(50, -50); // ...rotate right
    }
    flag = 0;
    }
    }
  • twm47099twm47099 Posts: 867
    edited 2017-09-22 16:45
    I made some notes in your code that might help.
    Tom


    /*
    Roam W/ Ultrasound & IR
    */
    #include "simpletools.h" // Include simple tools
    #include "abdrive.h"
    #include "ping.h" // Include ping header

    void ultrasound();
    void infrared();
    int flag; // 0 = ultrasound controlling, 1 = IR controlling

    int main() // Main function
    {
    flag = 0;
    cog_run(ultrasound, 128);
    cog_run(infrared, 128);
    }

    void ultrasound() {
    int turn; // Navigation variable

    while (1) {

    drive_speed(100, 100); // Forward

    // While disatance greater than or equal
    // to 10 cm, wait 5 ms & recheck.
    while (ping_cm(8) >= 10) pause(5); // Wait until object in range

    // *************** why is flag == 0 and stopping done 2x’s?
    if (flag == 0)
    drive_speed(0, 0); // Then stop
    if (flag == 0)
    drive_speed(0, 0); // Then stop

    // Turn in a random direction
    turn = rand() % 2; // Random val, odd = 1, even = 0

    if (turn == 1) // If turn is odd
    drive_speed(64, -64); // rotate right
    else // else (if turn is even)
    drive_speed(-64, 64); // rotate left
    /* I suggest that you set a new flag = 1 when doing this rotation
    So that if flag == 1 from the ir cog you can still stop the rotation

    */

    // Keep turning while object is in view
    while (ping_cm(8) < 10); // Turn till object leaves view
    if (flag == 0) // change this to the new flag
    drive_speed(0, 0); // Stop & let program end
    }
    }

    void infrared() {
    int irLeft, irRight; // IR variables

    low(26); // D/A0 & D/A1 to 0 V
    low(27);

    while (1) {
    freqout(11, 1, 38000); // Check left & right objects
    irLeft = input(10);

    freqout(1, 1, 38000);
    irRight = input(2);

    if (irRight == 0) // Just right obstacle?
    {
    flag = 1;
    drive_speed(-50, 50); // ...rotate left
    } else if (irLeft == 0) // Just left obstacle?
    {
    flag = 1;
    drive_speed(50, -50); // ...rotate right
    }
    flag = 0;
    /* I’m not sure how you are stopping the rotation.
    Maybe ultrasound cog stops it? I recommend that you complete
    the movement command in the cog that starts it. Then set or reset the flag that passes control to the other cog. That way you can limit or eliminate
    undesired cog interactions.
    Also, use pause() after you start a rotation to control the number of degrees rotated. Then take a new reading and rotate again. It is probably better to stop the rotation & then take the reading so you don't have to be concerned with the timing of the reading affecting the amount of rotation. If you were programming in PASM you can arrange the program to synchronize the actions taken by the cogs. But in C you can't tell how long it takes to execute each command so one cog may execute its main loop many times while the other is plodding along. So you should rely on your program controlling when things happen.
    You may want to use smaller drive speed commands to slow things down.
    Once you get the program working, then you can try speeding things up.

    */
    }
    }

Sign In or Register to comment.