All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

commotion.cc 10.5 KB
Newer Older
Timothy Stack's avatar
Timothy Stack committed
1 2 3 4 5 6
/*
 * EMULAB-COPYRIGHT
 * Copyright (c) 2005 University of Utah and the Flux Group.
 * All rights reserved.
 */

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
/* Garcia robot motion program (command and interactive mode)
 *
 * Dan Flickinger
 *
 * 2004/09/13
 * 2004/11/17
 */

#include "dgrobot/commotion.h"

void interact(grobot & Robot);
void drive(char *filename, grobot & Robot);
void print_acpValue(acpValue *v);
void printHELP();


int main(int argc, char **argv)
{

  char commandIN [64];	 // command in
  char callbackMSG[128]; // callback message
  char * pCPRIM;	 // pointer to Command PRIMitive character
  char * pCARG;		 // pointer to Commmand ARGument string
  
  int fileMode = 0;	 // file in mode for commands
  
 

  // battery parameters
  float battLevel, battVoltage;
  
  // wheel odometry parameters (distance and velocity)
  float dLeft, dRight;
  float vLeft, vRight;
  
  // GOTO displacement (Dx, Dy) and final orientation angle (Rf)
  float Dx, Dy, Rf;

  ifstream fileIN;
  ofstream fileOUT;
  
  strcpy(commandIN, "");
 
  grobot robot1;
 
  
  fileOUT.open("vpos.log", ios::out | ios::app);
  if (!(fileOUT.is_open())) {
    // file not opened for some reason
    cout << "ERROR opening log file!" << endl;
  } else {
    cout << "Appending wheel position and velocity data to 'vpos.log'" << endl;
  }
  
  cout << "COMMAND: (h for help)" << endl;
     
  
  while (strcmp(commandIN, "q") != 0) {
    cout << endl << "? ";
    
    // get a command
    if (fileMode) {
      // read a line from file
      if (fileIN.is_open()) {
      if (fileIN.eof()) {
        // done reading, drop back to command mode
        cout << "Finished reading command file." << endl;
        fileIN.close();
        fileMode = 0;
      } else {
        fileIN.getline(commandIN, 64);
        cout << commandIN << endl;
      }
    } else {
      // file not open
      cout << "FILE CLOSED." << endl;;
      fileMode = 0;
    }
    } else {
      // read from console input
      cin.getline(commandIN, 64);
    }
    
    if (strcmp(commandIN, "h") == 0) {
      // print HELP     
      printHELP();
    } else {
      // not help

      
      if (strcmp(commandIN, "") !=0) {
     
     pCPRIM = strtok(commandIN, " ");
     pCARG = strtok(NULL, " ");
 

     if (strcmp(pCPRIM, "q") == 0) {
       // quit
       cout << "QUIT..." << endl;
     } else if (strcmp(pCPRIM, "s") == 0) {
       // set wheelspeed
       // (This value is persistent for all primitives.)
       
       acpValue wheelSpeed((float)(atof(pCARG)));
       robot1.garcia.setNamedValue("speed", &wheelSpeed);
       cout << "Set wheelspeed to " << pCARG << endl;
       
     } else if (strcmp(pCPRIM, "a") == 0) {
       // set stall threshold
       
       acpValue stallThreshold((float)(atof(pCARG)));
       robot1.garcia.setNamedValue("stall-threshold", &stallThreshold);
       cout << "Set stall threshold to " << pCARG << endl;
       
     } else if (strcmp(pCPRIM, "b") == 0) {
       // get battery level
       
       battLevel = robot1.garcia.getNamedValue("battery-level")->getFloatVal();
       battVoltage = robot1.garcia.getNamedValue("battery-voltage")->getFloatVal();
       cout << "Battery: " << 100 * battLevel << "%, "
            << battVoltage << " V" << endl;
       
     } else if (strcmp(pCPRIM, "o") == 0) {
       // read wheel odometry
       
       dLeft = robot1.garcia.getNamedValue("distance-left")->getFloatVal();
       dRight = robot1.garcia.getNamedValue("distance-right")->getFloatVal();
       
       cout << "Odometry: left = " << dLeft
            << ", right = " << dRight << endl;
     } else if (strcmp(pCPRIM, "i") == 0) {
       // enter interactive mode
       interact(robot1);
       
     } else if (strcmp(pCPRIM, "v") == 0) {
       // get acpValue
       
       acpValue *retValue = robot1.garcia.getNamedValue(pCARG);
       if (retValue == NULL) {
         cout << "Invalid property" << endl;
       } else {
         print_acpValue(retValue);
       }
       
     } else if (strcmp(pCPRIM, "f") == 0) {
       // set file read mode
       
       fileIN.open(pCARG, ios::in);
       if (fileIN.is_open()) {
         cout << "Opened command file: " << pCARG << endl;
         fileMode = 1;
       } else {
         cout << "FAILED to open command file: " << pCARG << endl;
         fileMode = 0;
       }
       
     } else if (strcmp(pCPRIM, "d") == 0) {
       // set file read mode for driving
       drive(pCARG, robot1);
       
     } else {
       // parse a motion command
  
 
             
       if (strcmp(pCPRIM, "m") == 0) {
         // move primitive
         cout << "Executing MOVE (distance: " << pCARG << ")" << endl;
         fileOUT << "# MOVE " << pCARG << ":" << endl;
    
         robot1.pbMove((float)(atof(pCARG)));
       }
    
       if (strcmp(pCPRIM, "p") == 0) {
         // pivot primitive
         cout << "Executing PIVOT (angle: " << pCARG << ")" << endl;
         fileOUT << "# PIVOT " << pCARG << ":" << endl;
   
         robot1.pbPivot((float)(atof(pCARG)));
       }   
       
       if (strcmp(pCPRIM, "g") == 0) {
         // goto displacement-orientation (3 primitives)
        
         cout << "Executing GOTO (Dx=";
         fileOUT << "# GOTO Dx=";
         
         Dx = atof(pCARG);
         cout << Dx << ", Dy=";
         fileOUT << Dx << " Dy=";
         
         pCARG = strtok(NULL, " ");
         Dy = atof(pCARG);
         cout << Dy << ", Rf=";
         fileOUT << Dy << " Rf=";
         
         pCARG = strtok(NULL, " ");
         Rf = atof(pCARG);
         cout << Rf << ")" << endl;
         fileOUT << Rf << ":" << endl;
         
         robot1.dgoto(Dx, Dy, Rf);
         
       }
       
       

       while (!robot1.garcia.getNamedValue("idle")->getBoolVal()) {
         robot1.garcia.handleCallbacks(100);
         
         // wheel speed is only accessible while the NULL primitive is running
         dLeft = robot1.garcia.getNamedValue("distance-left")->getFloatVal();
         dRight = robot1.garcia.getNamedValue("distance-right")->getFloatVal();
         // vLeft = robot1.garcia.getNamedValue("damped-speed-left")->getFloatVal();
         // vRight = robot1.garcia.getNamedValue("damped-speed-right")->getFloatVal();
         
         // use numerical differentiation instead
         // FIXME
         vLeft = 0.0f;
         vRight = 0.0f;
         
         // log data to outfile
         fileOUT << dLeft << " "
                 << dRight << endl;
         
         
       } // end while garcia is idle
     } // end parse a motion command
   } // end not a blank command ("")
 } // end not help
 } // end while command in is not 'q'

 
 fileOUT.close();
 return 0;
  
}


/**********************
 * FUNCTIONS          *
 **********************/



 
void interact(grobot & Robot) {
  // Keyboard interactive mode
  
  // terminal interaction
  struct termios old_mode;
  struct termios new_mode;

  tcgetattr(fileno(stdout),&old_mode);
  new_mode = old_mode;

  new_mode.c_lflag &= ~(ECHOKE | ECHOK | ECHOE | ECHO);
  //new_mode.c_lflag |= ECHONL;
  new_mode.c_lflag &= ~(ICANON);
  tcsetattr(fileno(stdout),TCSANOW,&new_mode);

  setbuf(stdin,(char*)NULL);

  
  cout << "Interactive mode: " <<
          "use numeric keypad,PgUP/DOWN or ijkm,ol,<space>" << endl;
  
  float Vleft = 0.0f;
  float Vright = 0.0f;
  
  // speed change increment
  float Vdelta = 0.02f;
  

  grobot robot1;
  
  int local_quit = 0;
  char c;
  
  while (!local_quit) {
    // interactive loop
 
    // handle the callbacks (check and see if a behavior terminated)
    robot1.garcia.handleCallbacks(10);

    fread(&c,sizeof(char),1,stdin);
 
    switch(c) {
      case '9':
      case 'o':
        // PigUp, inrease speed change
        Vdelta += 0.02f;
        break;
      case '3':
      case 'l':
        // PigDown, descrease speed change
        Vdelta -= 0.02f;
        break;
      case '8':
      case 'i':
        // UP arrow, increase forward speed
        Vleft += Vdelta;
        Vright += Vdelta;
        break;
      case '2':
      case 'm':
        // DOWN arrow, decrease forward speed
        Vleft -= Vdelta;
        Vright -= Vdelta;
        break;
      case '4':
      case 'j':
        // LEFT arrow, turn left
        Vleft -= Vdelta;
        Vright += Vdelta;
        break;
      case '6':
      case 'k':
        // RIGHT arrow, turn right
        Vleft += Vdelta;
        Vright -= Vdelta;
        break;
      case '5':
      case ' ':
        // CENTER, stop!!
        Vleft = 0.0f;
        Vright = 0.0f;
        break;
      case 'q':
        local_quit = 1;

        // Don't forget to stop the robot~!
        Vleft = 0.0f;
        Vright = 0.0f;
        break;
      default:
          ;
      }
       
      // send new speed information to the robot
      Robot.setWheels(Vleft, Vright);
    }




  Robot.garcia.flushQueuedBehaviors();  
  tcsetattr(fileno(stdout),TCSANOW,&old_mode);
}


void drive(char *filename, grobot & Robot) {
  // drive with path from a file
  
  float Vleft = 0.0f;
  float Vright = 0.0f;

  
  // open the file
  ifstream fileDRIVE; 
  fileDRIVE.open(filename, ios::in);
  
  if (!(fileDRIVE.is_open())) {
    cout << "FAILED to open drive file: " << filename << endl;
  } else {
    cout << "Opened drive file: " << filename << endl;
  
    while (!(fileDRIVE.eof())) {
      // read through file line by line
      fileDRIVE >> Vleft >> Vright;
      
      // send new speed information to the robot
      Robot.setWheels(Vleft, Vright);
      Robot.sleepy();
    }
    fileDRIVE.close();
    Robot.garcia.flushQueuedBehaviors();  
  }
  
}


void print_acpValue(acpValue *v) {
  acpValue::eType t = v->getType();
  if (t == acpValue::kInt) { cout << v->getIntVal(); }
  else if (t == acpValue::kFloat) { cout << v->getFloatVal(); }
  else if (t == acpValue::kString) { cout << v->getStringVal(); }
  else if (t == acpValue::kBoolean) {
        const char *s = (v->getBoolVal())?"true":"false";
        cout << s;
  } else {
    cout << "<UNRECOGNIZED acpValue TYPE>";
  }
  cout << endl;
}



void printHELP() {
  // print HELP
  

  cout << "HELP:" << endl
       << "q        : QUIT" << endl  
       << "f <name> : Read in command file" << endl
       << "d <name> : Drive from file" << endl
       << "b        : Battery level, voltage" << endl
       << "m 0.00   : MOVE (distance)" << endl
       << "p 0.00   : PIVOT (angle)" << endl
       << "s 0.00   : set wheel speed" << endl
       << "a 0.00   : set stall threshold" << endl
       << "o        : get wheel odometry" << endl
       << "g 0.00 0.00 0.00 : goto displacement/orientation" << endl
       << "i        : enter interactive mode" << endl
       << "v <name> : print acpValue" << endl
       << endl;
       
}