PRINTING

Usually when you take a programming class, you start with something like the following. Go ahead. Write it out and run it.

int main() {
 printf("Hello World!");
 }

This just tells the computer to print Hello Word! on the screen.* I, however, decided to save this for much later, because we are programming robots and want them to do stuff! There does come a point when we want to have something printed to the screen. For example, if we have a program that requires us to use the virtual buttons to control the robot or change a parameter, we want the robot to print reminders and instructions so that we know what to do.  Most importantly, printed display is an invaluable aid in running performance & reliability tests on your robot.  The command that causes the robot to print, or display text to its screen, is the printf() command. Try out this program:

int main()
{
  int sp=750; // speed of motor
  int time=2500 //time for running motor, in milliseconds
  printf("Press 'A' to continue: motor speed is %d \n",sp); /* the %d is a place
  holder for the integer value of the variable sp */
  while(!a_button()) //while A_button is not pressed
    {} // empty while loop keeps robot waiting for A button.
  mav(0,sp);
  mav(3,sp);
  msleep(time);
  ao();
  printf("The motor ran for %d milliseconds.",time); /* the %d is a place holder
  for the integer value of the variable time. */ 
}

When you run it or simulate it, you’ll get the robot asking you to press the A button, tells you how fast the motors will go, and then it will wait for you to push the a button. Then it will move forward at the reported speed.  When it stops, it will tell you how long it went for. If you want to calibrate the speed in linear distance/sec,  rather than tics/sec, just measure the distance moved and divide by 2.5 sec.

If you want to have the CBC read out levels as you adjust them, it’s convenient to use a special CBC printing function: cbc_printf(x,y,”text”).

Write and run the following code for testing your robot:

/*This program prints instructions for testing behavior of a CBC-controlled robot,
 and provides feedback as to speed level before the robot is run for 2 seconds,
 then allows further adjustment*/ 
int main()
{     
   int velocity=0, p=0;// declare variables    
   cbc_display_clear(); //prepares display memory [for Links: display_clear()]    
   //print instructions     
   cbc_printf(0,0,"Use the up & down arrow keys ");     
   cbc_printf(0,1,"to adjust velocity.(- is rev)");     
   cbc_printf(0,2,"Press A to go!");         
   while(1){   
      while(!a_button()) //adjust velocity until A button is pressed   
      {       
       if(p){cbc_printf(2,5,"Velocity: %d   ",velocity);   p=0;} //print changes    
       if(up_button()) {velocity=velocity+50;  p=1;}    
       if(down_button()) {velocity=velocity-50; p=1;}      
       if(velocity>1000) velocity=1000; //max forward speed        
       if(velocity<-1000) velocity=-1000; //min reverse speed       
       msleep(150) ;// allow display time to print  
       }         
      mav(0,velocity);   
      mav(3,velocity);    
      msleep(2000);  // set time to move  
      ao(); // turn off motors    
     } // end repeat loop
}

Notice how the new function is used: cbc_printf(). – this tells the CBC to print something at exact coordinates (col, row) so when it loops, it rewrites over the same spot. (note spaces after %d to erase a bigger value.)  To see how this looks without cbc_printf(), change the cbc_printf() in the program to printf() [with no position numbers] and see how crazy the display gets when you run it.  Adding a ‘n ‘ at the end of the text lines will start a new line and make it more readable. Be careful with line length using cbc_printf, it cuts off any characters beyond 28.  Also, be warned that running the cbc_printf() in a loop for more than 5 minutes can cause the operating system to crash (thus we’ve added a ‘p’ flag to limit the print cycles). Note that for the Link controller and using the KISS IDE v. 4 or higher the function names have changed:  “display_printf” is substituted for “cbc_printf” for printing at a row & column, and “display_clear” for “cbc_display_clear”  Furthermore, instead of arrow keys, the Link console has three soft keys which can be labeled with up to 12 arbitrary text characters each.  (with three optional  soft keys designated ‘x’, ‘y’, & ‘z’).   The new function set for doing this takes the form “set_name_button_text(“text”) where name is ‘a’, ‘b’, or ‘c’, etc. In addition, the Link code uses only “msleep” for a time delay function.  Furthermore, the max velocity has increased, and, to avoid putting display_printf  in a loop, the coding has been revised in a different way.  Thus, for Link code we have the following example:

/*This program prints instructions and shows velocity for testing behavior on a
Link-controlled robot, and allows adjustment of the speed before the robot is
run for 2 seconds, then allows further adjustment 2/25/14*/
int main()
{
    int velocity=0;// declare variable
    set_b_button_text("Up"); set_c_button_text("Down");
    display_clear(); //prepares display memory
    //print instructions
    display_printf(0,0,"Use the up & down keys ");
    display_printf(0,1,"to adjust velocity.(- is rev)");
    display_printf(0,2,"Press A to go!");    
    while(1){
        while(!a_button()) //adjust velocity until A button is pressed
        {
            if(b_button() || c_button()){
               if(b_button()) velocity=velocity+50;
               if(c_button()) velocity=velocity-50;
               if(velocity>1500) velocity=1500; //max forward speed
               if(velocity<-1500) velocity=-1500; //min reverse speed
               display_printf(2,5,"Velocity: %d   ",velocity);
               msleep(150) ;// allow display time to print
               }
         }
         mav(0,velocity);
         mav(3,velocity);
         msleep(2000);
         ao(); // turn off motors
     } // end repeat loop
 }

*Assignment to play with Hello World  and to extend Link bot tests – click the big, red A.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s