Shop OBEX P1 Docs P2 Docs Learn Events
Learning on ActivityBot. How do I execute multiple tasks at once? — Parallax Forums

Learning on ActivityBot. How do I execute multiple tasks at once?

I purchased the activity bot a couple days ago. I was so excited I went through all the tutorial builds in a day haha. I'm sure I rushed things just to see how it works, and with this I know I'm missing a lot of knowledge on the programming side.
I'm not a 100% novice to programming, I just dont know C. I do Cisco programming (if you want to call it programming).

So my basic question is how can I get the bot to do 2 things at once. I want it to flash LED's and piezo beep when finding a wall and stopping. I can get it to find the wall, stop and beep OR/THEN flash, but not flash AND beep simultaneously.

I was playing around with the multicore functionality last night trying to have the LED's go to a core and the beeps to another core but for some reason I couldnt get the code to build. Is the multicore approach the best approach to have 2 things happen at the same time?

Comments

  • TymkrsTymkrs Posts: 539
    edited 2015-08-06 18:54
    You could have the code run it linearly - I think it'd be easier (esp given that you probably wouldn't be able to distinguish between the two activities). In spin-glish it'd be something like:
    repeat 
       if Ping sees something that's too close
          outa[1] := 1                  ' where the led is on pin 1 - turn on
          waitcnt((clkfreq/2) + cnt)
          outa[1] := 0                  ' turn led off
          piezobeep   ' or however you choose to tell the piezo to beep
       else take over the world
    
    

    You could also have the Ping write to a global var that's monitored by 2 cogs. 1 cog could initiate the leds, and another cog could initiate the beep while both were reading that variable.

    Hope this helps!
    Addie
  • Q: Is the multicore approach the best approach to have 2 things happen at the same time? Ans:Yes. That's pretty much what multicore is all about.
    Q:...but for some reason I couldnt get the code to build. Show what you did and what you got. That way people can help.
  • Q: Is the multicore approach the best approach to have 2 things happen at the same time? Ans:Yes. That's pretty much what multicore is all about.
    Q:...but for some reason I couldnt get the code to build. Show what you did and what you got. That way people can help.

    Code is on my laptop at home. I'll paste it when I get back shortly.
  • cstambaughcstambaugh Posts: 7
    edited 2015-08-06 20:32
    /*
    Blank Simple Project.c
    http://learn.parallax.com/propeller-c-tutorials
    */
    #include "simpletools.h" // Include simple tools
    #include "abdrive.h"
    #include "ping.h"

    int turn;
    void leds();
    int *cog1;


    int main() // Main function
    {
    drive_setRampStep(10);

    while(1)
    {
    drive_ramp(128, 128);
    while(ping_cm(10) >= 20) pause(5);
    drive_ramp(0, 0);
    freqout(0, 200, 1000);
    cog1 = cog_run(leds, 128);
    turn = rand() % 2;
    if (turn == 1)
    drive_speed(64, -64);
    else
    drive_speed(-64, 64);
    while(ping_cm(10) < 20);
    }
    }

    void leds() // Blink function for other cog
    {
    for(int n = 1; n <= 15; n++)
    {
    high(8);
    pause(100);
    low(8);
    pause(100);
    high(9);
    pause(100);
    low(9);
    pause(100);
    }
    cog_end(cog1);


    }
  • 1. You would have to start the led cog prior to starting the beep. What you have now will beep and then blink.
    2. I don't see, offhand, why what you have won't build. What are the error messages?
  • #include "simpletools.h"                      // Include simple tools
    #include "abdrive.h"
    #include "ping.h"
    
    void leds();
    void siren();
    int turn;
    int cog_leds;
    int cog_siren;
    int wall;
    
    
    int main()                                    // Main function
    {
      drive_setRampStep(10);
      turn = rand() % 2;
      wall = (ping_cm(10) >=20);
      cog_leds = cog_run(leds, 128);
      cog_siren = cog_run(siren, 128);
     
      
      while(1)
        {
        drive_ramp(128, 128);
        while(ping_cm(10) >= 20) pause(5);
        drive_ramp(0, 0);
        
        if (wall)
          high(8);
      
        if (turn == 1)
          drive_speed(64, -64);
        else
          drive_speed(-64, 64);
        while(ping_cm(10) < 20); 
        }
    }
    
    void leds()                            // Blink function for other cog
    {
      for(int n = 1; n <= 15; n++)                             
      {
        high(8);                          
        pause(100);                        
        low(8);                          
        pause(100);
        high(9);
        pause(100);
        low(9);
        pause(100);                       
      }
    
    }
    
    void siren()
    {
      for(int n = 1; n <= 5; n++)
      {
        freqout(0, 100, 1000);
        freqout(0, 100, 1500);
        freqout(0, 100, 2000);
        freqout(0, 100, 1500);
        freqout(0, 100, 1000);
      }
    }      
          
    

    Here are the errors:

    UltraBot.c:18:3: warning: implicit declaration of function 'cog_run' [-Wimplicit-function-declaration]
    C:\Users\Chad\AppData\Local\Temp\ccE0j86H.o: In function `_main':
    (.text+0x98): undefined reference to `_cog_run'
    C:\Users\Chad\AppData\Local\Temp\ccE0j86H.o: In function `_main':
    (.text+0xa5): undefined reference to `_cog_run'
    collect2: ld returned 1 exit status
    Done. Build Failed!

    Check source for bad function call or global variable name `_cog_run'
    C:\Users\Chad\AppData\Local\Temp\ccE0j86H.o: In function `_main':
    (.text+0xa5):


    For some reason it just does not like the cog_run or cog_end code. I brought up a new project to test cog_runs in a while loop and they work perfect. It's just this code thats having issues. I tried putting the int "cog_leds" under the "if (wall)" and it wont execute. I have an LED there now and that works fine.
  • For sure the cog data (cog_led and cog_siren) have to be defined as pointers. Don't know what else may be wrong.

    One of the things I always disliked most about every "C" compiler I ever met is that the compiler error messages seem to be totally unrelated to the actual problem.
  • It looks like it cannot find 'cog_run', so you may have to go to the folder that contains libsimpletools and see if cog_run is there. Cog_run was a recent addition, so maybe somehow you are not using the latest simpletools library. Once you verify that, then other things can be eliminated.

    Ray
  • I've been doing cog_run in other projects just fine, I know it's in my library.
    I've semi abandoned this project out of wasting too much time trying to troubleshoot stupid stuff haha. I've been having fun playing with multi-core functions in other projects.
  • I tried compiling (using SimpleIDE) the code you had trouble with. It built fine with no errors.

    Sometimes, after editing a project with problems multiple times I would get errors that made no sense. After checking for missing } or ; I would just highlight all the code in the SimpleIDE window, right click on it and click on copy, open a new instance of the IDE, click on new project, highlight all the code that new project enters, and copy the code from the clipboard over the highlighted code in the new project.

    Once I do that the program either builds w/o errors or gives me an error that makes sense.

    Tom
  • twm47099 wrote: »
    I tried compiling (using SimpleIDE) the code you had trouble with. It built fine with no errors.

    Sometimes, after editing a project with problems multiple times I would get errors that made no sense. After checking for missing } or ; I would just highlight all the code in the SimpleIDE window, right click on it and click on copy, open a new instance of the IDE, click on new project, highlight all the code that new project enters, and copy the code from the clipboard over the highlighted code in the new project.

    Once I do that the program either builds w/o errors or gives me an error that makes sense.

    Tom

    I'm thinking it was some sort of bug. It was acting like it couldnt understand the cog_run function. I was executing the same type of functions in another project just fine.

Sign In or Register to comment.