Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

test/circle.cpp

Go to the documentation of this file.
00001 
00002 // This program will run the truck to produce a cicular path at a certain speed
00003 // and record the encoder values to a file
00005 
00006 
00007 #include "../truckd/hw_interface.h"
00008 #include "../estimator/estimator.h"
00009 #include <unistd.h>
00010 #include <iostream>
00011 #include <signal.h>
00012 #include <sys/time.h>
00013 #include <errno.h>
00014 #include <fstream>
00015 
00016 using namespace std;
00017 
00018 int quit=0;
00019 
00020 // signal handler will set quit the first time ctrl-c is pressed 
00021 // called and will force on exit the second time
00022 void sighandler(int signum) {
00023         if (signum == SIGINT) {
00024                 if (!quit) {      
00025                         cerr << "caught ctrl-C quitting" << endl;
00026                         quit = 1;
00027                 } else {
00028                         cerr << "second ctrl-C forcing exit" << endl;
00029                         exit (EXIT_FAILURE);
00030                 }
00031         } else {
00032                 cerr << "caught unknown signal?" << endl;
00033         }
00034 }
00035 
00036 
00037 
00052 float calcAdjustedSteeringValue(float value) {   
00053         static float valueErr = 0.0f;   
00054         float intValue = roundf(value + valueErr);
00055         valueErr += value - intValue;   
00056         return intValue;
00057 }
00058 
00059 
00060 
00061 int main(int argc, char *argv[]) {
00062 
00063         if ((argc != 4) && (argc != 3)) {
00064                 cout <<
00065                         "desc  : move the truck in a circle at a given SC power\n"
00066                         "        and record encoder values to a file \n"
00067                         "usage : circle <steering value> <SC power> [filename]\n"
00068                         "     steering value - value for steering servo (0-255)\n"
00069             "        if the value is fractional the steering controller will\n"
00070                         "        alternate between two closest integer values\n"
00071                         "     SC power - power setting for speed controller (-1 to 1)\n"
00072                         "     filename - name of file to save matlab data script to (default 'circ.m')\n";
00073                         return -1;
00074         }  
00075 
00076         errno = 0;
00077         char *temp;
00078         float value = strtof(argv[1], &temp);
00079         if (errno != 0) {
00080                 cerr << "could not convert '" << argv[1] << "' to float :"
00081                          << strerror(errno) << endl;
00082                 return -1;
00083         }
00084         
00085         float power = strtof(argv[2], &temp);
00086         if (errno != 0) {
00087                 cerr << "could not convert '" << argv[2] << "' to float :"
00088                          << strerror(errno) << endl;
00089                 return -1;
00090         }
00091 
00092         if ((power > 0.1) || (power < -0.1)) {
00093                 cerr << "power of " << power << " is just too much to be safe!" << endl
00094                          << "change circle.cpp to allow this if it is really what you want" << endl;
00095                 return -1;
00096         }
00097 
00098         
00099         const char *filename = "/tmp/circ.m";
00100         if (argc>=4)
00101                 filename = argv[3];
00102 
00103         ofstream f(filename);
00104         if (!f) {
00105                 cerr << "could not open file '" << filename << "' for writing" << endl;
00106                 return -1;
00107         }
00108         
00109         // initialize intreface to truck hardware and set steering servo value
00110         hwInterface hw( 1 /*enable write control*/);
00111         hw.reset();
00112         int intValue = (int) calcAdjustedSteeringValue(value);
00113         hw.setSteeringValue(intValue);
00114         hw.writeControl();
00115         
00116         // install ctrl-c signal handler
00117         if (signal(SIGINT,sighandler) == SIG_ERR) {
00118                 cerr << "error installing signal handler" << endl;
00119                 return -1;
00120         }
00121         
00122         // write debugging info to a file that can be run by matlab
00123         f << "% circle data" << endl
00124           << "steeringValue=" << value << endl
00125           << "power=" << power << endl
00126           << "% [time, encoder fr, fl, br, bl, steering A2d, steering value]" << endl
00127           << "data=[" << endl; 
00128         
00129         // Let the truck be adusted before starting, if nessasary
00130         cout << "press ENTER when truck is ready" << endl;
00131         getchar();
00132 
00133         float time = 0.0f;
00134         hw.setAdjustedSCPower(power);
00135         hw.writeControl();
00136         
00137         while (!quit) {         
00138 
00139                 for (int j = 1; (j<33) && (!quit); ++j) {
00140                         for (int i = 1; i<3; ++i) {
00141                                 hw.readStatus();
00142                                 f << '['
00143                                   << time << ",\t" 
00144                                   << hw.encTicksFR() << ",\t"
00145                                   << hw.encTicksFL() << ",\t"
00146                                   << hw.encTicksBR() << ",\t"
00147                                   << hw.encTicksBL() << ",\t"
00148                                   << hw.getSteeringA2DValue() << ",\t"
00149                                   << intValue << " ]" << endl;                                                          
00150                                 hw.wait();
00151                                 time += 0.01;
00152                         }               
00153 
00154                         intValue = (int) calcAdjustedSteeringValue(value);
00155                         hw.setSteeringValue(intValue);
00156                         hw.setAdjustedSCPower(power);
00157                         hw.writeControl();
00158                 }
00159                         
00160                 cout  << hw.encTicksFR() << '\t'
00161                           << hw.encTicksFL() << '\t'
00162                           << hw.encTicksBR() << '\t'
00163                           << hw.encTicksBL() << endl;           
00164         }
00165 
00166         f << "];" << endl;
00167         
00168         hw.setAdjustedSCPower(0);
00169         hw.writeControl();
00170         
00171         return 0;
00172 }
00173 
00174 

Generated on Fri Sep 1 14:25:53 2006 for Raptor by  doxygen 1.4.4