Shop OBEX P1 Docs P2 Docs Learn Events
Another C Question — Parallax Forums

Another C Question

ajwardajward Posts: 1,130
edited 2015-02-25 03:15 in Learn with BlocklyProp
Hi All...

Working on some code for my Prop BoeBot and trying to use functions (probably not necessary for this small program, but hey). I'm getting an error, but I don't quite understand why.

This is the error message... test_bob (1).c:35:5: error: expected expression before 'while'. What "expression" is expected before while?

Scratching my head!

Amanda

Edit: There are some other issues, but I'll ask after I get this sorted out. :-)
/*
  Test_Bob.c:
  From: Detect and Turn Project.c
  Modified from:
  http://learn.parallax.com/propeller-c-tutorials
  Amanda Ward - 30 Dec 2014 
*/
#include "simpletools.h"                // Include simple tools
#include "servodiffdrive.h"             // Include servodiffdrive tools
#include "servo.h"                      // Include servo Tools
#include "ping.h"                       // Include ping tools  

void straight_on(void);                 // Call straight_on


int main()                              // Main function
{
   // Add startup code here.
        
    freqout(4, 1000, 3000);             // (pin, duration, frequency) 
    drive_pins(14, 15);                 // Define servo ports  
    drive_setramp(10, 10);              // Set servo ramping values
    drive_speeds(100, 100);             // Set initial servo speeds
    servo_set(18, 1450);                // Aim Ping forward
    
    straight_on();                  
                
    drive_speeds(100, -100);            // Rotate right
    pause(500);                         // Pause 0.5 seconds
    drive_sleep();                      // Stop servos
}

void straight_on(void) 
[
    while(ping_cm(19) >= 25) pause(15); // Continue till Ping range is less than 25cm
    drive_sleep();                      // Stop Servos
    pause(1500);                        // Pause 1.5 seconds
]

Comments

  • David BetzDavid Betz Posts: 14,516
    edited 2015-02-15 11:23
    You are using square brackets in your straight_on function instead of curly braces.
  • ajwardajward Posts: 1,130
    edited 2015-02-15 13:25
    David Betz wrote: »
    You are using square brackets in your straight_on function instead of curly braces.

    AAUGH!!!!!! (My usual reaction to C.)

    Thanks David!!! :-)

    Other things to sort out, but this really helps.

    Amanda
  • GenetixGenetix Posts: 1,754
    edited 2015-02-15 18:31
    Amanda,

    You might to comment those curly braces to make your code more readable.
    {          // Start of Statement or Function - Notice it resembles a (
         // Statements go here - Everything within the braces
    }          // End of Statement or Function - Notice it resembles a )
    
  • TorTor Posts: 2,010
    edited 2015-02-15 18:51
    Also,
    void straight_on(void);                 // Call straight_on
    
    that comment may seem to indicate that this line calls straight_on, but it's not, it's a prototype. And you can avoid that prototype entirely (it's duplication of work) by moving your straight_on function there, in full, and make it a static function. As it is now it'll be in the global namespace. If you have functions only called from inside the same file, make them 'static', and arrange them so that you don't need prototypes and won't have to worry about namespace (names colliding with function names in other files or libraries). It follows that 'main' will always be the last function in the file then.

    -Tor
  • ajwardajward Posts: 1,130
    edited 2015-02-15 22:17
    Genetix wrote: »
    Amanda,

    You might to comment those curly braces to make your code more readable.
    {          // Start of Statement or Function - Notice it resembles a (
         // Statements go here - Everything within the braces
    }          // End of Statement or Function - Notice it resembles a )
    

    Good idea. I usually comment the dickens out out of my code, but this is the 5th or 6th iteration of this and I'm getting a little lazy! :-)

    Amanda
  • ajwardajward Posts: 1,130
    edited 2015-02-15 22:29
    Tor wrote: »
    Also,
    void straight_on(void);                 // Call straight_on
    
    that comment may seem to indicate that this line calls straight_on, but it's not, it's a prototype. And you can avoid that prototype entirely (it's duplication of work) by moving your straight_on function there, in full, and make it a static function. As it is now it'll be in the global namespace. If you have functions only called from inside the same file, make them 'static', and arrange them so that you don't need prototypes and won't have to worry about namespace (names colliding with function names in other files or libraries). It follows that 'main' will always be the last function in the file then.

    -Tor

    Thanks Tor! That was just an "Oops" on my part. I've been tinkering with this section of the program, on and off, for two days and I just got a bit sloppy/lazy.
    Actually, that bit might end up in another file (or not). I have several functions to accomplish various tasks for my BoeBot and they should reside in at least two cogs. Just trying to sort out how best to load everything into the BOE and get them to play well together.

    Thanks again!

    Amanda
  • ajwardajward Posts: 1,130
    edited 2015-02-16 09:18
    Okey Dokey, another question. I've re-written the program so the 'bot centers the Ping servo and drives forward till it detects something. Then it does a ~180 and runs till it detects something else and stops. The problem is with centering the Ping. If the ping is already centered at startup, everything is fine. If it isn't centered, the 'bot will just jerk along a half inch or so at a time till the Ping servo gets its act together. If I remove the line " servo_angle(18, 980);", the 'bot starts right up smoothly. I've tried "servo_set(18,1450)" with the same results. Also put the "servo_set/angle" in its own function with a long delay. Seems as long as I try to set the Ping servo position there is some sort of "interference".

    Of course, I can always position the servo by hand, but at some point I want to have the drive and Ping servos working simultaneously.

    Any advice will be appreciiated!

    Amanda

     /*
      Test_Bob.c:
      Modified from: Detect and Turn Project.c
      http://learn.parallax.com/propeller-c-tutorials
      Amanda Ward - 16 Feb 2015 
    */
    #include "simpletools.h"                // Include simple tools
    #include "servodiffdrive.h"             // Include servodiffdrive tools
    #include "servo.h"                      // Include servo tools
    #include "ping.h"                       // Include ping tools  
    
    int straight_on();                      // Function straight_on
    int turn_180();                         // Function turn_180
    
    int main()                              // Main function
    {
       // Add startup code here.
       
        drive_pins(14, 15);                 // Define servo ports  
        drive_setramp(10, 10);              // Set servo ramping values
        freqout(4, 1000, 3000);             // (pin, duration, frequency)
                                            // Just some white space :-)
        straight_on();                      // Call straight_on
        turn_180();                         // Call turn_180
        straight_on();                      // Call straight_on
    }
       
    int straight_on()                       // Run in "straight" line 
    {                                       // till Ping detects object < 25cm
        drive_speeds(100, 100);             // Set initial servo speeds
        servo_angle(18, 980);               // Rotate Ping forward
        while(ping_cm(19) >= 25) pause(15); // Continue till Ping range is less than 25cm
        drive_sleep();                      // Stop Servos
        pause(500);                         // Pause 0.5 seconds
        return(0);                          // Back to main 
    }
    
    int turn_180()                          // Turn vehicle 180 degrees in CW direction
    {                                       // Just some white space :-)
        drive_speeds(100, -100);            // Rotate CW ~180
        pause(650);                         // Pause 0.65 seconds
        drive_sleep();                      // Stop servos
        return(0);                          // Back to main
    }    
    
    
  • ratronicratronic Posts: 1,451
    edited 2015-02-16 11:58
    Amanda you might try putting the "drive_sleep()" in "straight_on" after the pause instead of before the pause. I changed the

    pin#'s in your program to match my Activitybot. Using servo_angle I was not able to get it to work. So I tried servo_set(pin, 1500);

    instead in "straight_on" and your code came to life.

    Edit: Here is my modified code for my Activitybot.
    /*
      Test_Bob.c:
      Modified from: Detect and Turn Project.c
      http://learn.parallax.com/propeller-c-tutorials
      Amanda Ward - 16 Feb 2015 
    */
    #include "simpletools.h"                // Include simple tools
    #include "servodiffdrive.h"             // Include servodiffdrive tools
    #include "servo.h"                      // Include servo tools
    #include "ping.h"                       // Include ping tools  
    
    int straight_on();                      // Function straight_on
    int turn_180();                         // Function turn_180
    
    int main()                              // Main function
    {
       // Add startup code here.
       
        drive_pins(12, 13);                 // Define servo ports  
        drive_setramp(10, 10);              // Set servo ramping values
        freqout(4, 1000, 3000);             // (pin, duration, frequency)
                                            // Just some white space :-)
        straight_on();                      // Call straight_on
        turn_180();                         // Call turn_180
        straight_on();                      // Call straight_on
    }
       
    int straight_on()                       // Run in "straight" line 
    {                                       // till Ping detects object < 25cm
        drive_speeds(100, 100);             // Set initial servo speeds
        servo_set(16, 1500);//servo_angle(16, 980);               // Rotate Ping forward
        while(ping_cm(17) >= 25) pause(15); // Continue till Ping range is less than 25cm
        //drive_sleep();                      // Stop Servos
        pause(500);                         // Pause 0.5 seconds
        drive_sleep();                       // put stop servos here instead of above
        return(0);                          // Back to main 
    }
    
    int turn_180()                          // Turn vehicle 180 degrees in CW direction
    {                                       // Just some white space :-)
        drive_speeds(100, -100);            // Rotate CW ~180
        pause(650);                         // Pause 0.65 seconds
        drive_sleep();                      // Stop servos
        return(0);                          // Back to main
    }
    
  • GenetixGenetix Posts: 1,754
    edited 2015-02-16 14:35
    Amanda,

    You should put a pause between rotating the Ping servo and checking the Ping distance.
    The servo needs time to move and it might already see something in the direction it's pointed.

    While not necessary, putting the pause after that while statement and enclosing it in braces will make it easier to understand..
    In addition it's good practice to document what a function does and what the inputs or outputs are.
  • ajwardajward Posts: 1,130
    edited 2015-02-25 03:15
    Thanks for all the input!!!!

    I've sorted things out a bit. I had the drive servos starting before trying to center the Ping. Running in just one cog, this wasn't an ideal situation. I swapped the two statements so the Ping centers before the 'bot starts to roll. Right now the Ping overshoots the center position 2 or 3 times before coming to a stop, but I can live with that. It's just a few seconds delay and it makes him look just a tiny bit less machine like.

    The goal is to have him wander around, constantly scanning from side to side for any obstacle within a predetermined distance. Then stop, find the direction that is the most open, turn that direction and race off again. Kind of like roaming with Ping and IR, but without the IR. I have all the standalone routines to accomplish this, but have yet to put them all together. ACK!

    Time for sleep.

    Amanda
Sign In or Register to comment.