00001 #include "ServoSerial.h" 00002 00003 #define deg2rad(deg) ((deg*M_PI/180.0)) 00004 00005 int kbhit(void) 00006 { 00007 struct termios oldt, newt; 00008 int ch; 00009 int oldf; 00010 00011 tcgetattr(STDIN_FILENO, &oldt); 00012 newt = oldt; 00013 newt.c_lflag &= ~(ICANON | ECHO); 00014 tcsetattr(STDIN_FILENO, TCSANOW, &newt); 00015 oldf = fcntl(STDIN_FILENO, F_GETFL, 0); 00016 fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); 00017 00018 ch = getchar(); 00019 00020 tcsetattr(STDIN_FILENO, TCSANOW, &oldt); 00021 fcntl(STDIN_FILENO, F_SETFL, oldf); 00022 00023 if (ch != EOF) { 00024 ungetc(ch, stdin); 00025 return 1; 00026 } 00027 00028 return 0; 00029 } 00030 00031 static int id = 2; 00032 void usage() { 00033 printf("servo test program, current id = %d\n", id); 00034 printf(" 1-9 : change id\n"); 00035 printf(" r : setReset\n"); 00036 printf(" o : setTorqueOn\n"); 00037 printf(" f : setTorqueOff\n"); 00038 printf(" b : setTorqueBreak\n"); 00039 printf(" p : setPosition to 90\n"); 00040 printf(" P : setPosition to -90\n"); 00041 printf(" i : setPositions 90 to id 2 3 4 5 6 6 8\n"); 00042 printf(" I : setPositions -90 to id 2 3 4 5 6 6 8\n"); 00043 printf(" m : setMaxTorque to 100\n"); 00044 printf(" M : setMaxTorque to 50\n"); 00045 printf(" g : getPosition\n"); 00046 printf(" G : getPositions\n"); 00047 printf(" d : getDuration\n"); 00048 printf(" s : getSpeed\n"); 00049 printf(" t : getTorque\n"); 00050 printf(" c : getTemperature\n"); 00051 printf(" v : getVoltage\n"); 00052 printf(" S : getState\n"); 00053 } 00054 00055 int main() { 00056 ServoSerial *serial; 00057 00058 serial = new ServoSerial("/dev/serusb1"); 00059 00060 char ch = 0; 00061 usage(); 00062 while (ch!='q') { 00063 if(kbhit()) { 00064 ch = getchar(); 00065 switch (ch) { 00066 case '1': 00067 case '2': 00068 case '3': 00069 case '4': 00070 case '5': 00071 case '6': 00072 case '7': 00073 case '8': 00074 case '9': 00075 id = ch-'0'; 00076 serial->clear_packet(); 00077 break; 00078 case 'r': 00079 serial->setReset(id); 00080 break; 00081 case 'o': 00082 serial->setTorqueOn(id); 00083 break; 00084 case 'f': 00085 serial->setTorqueOff(id); 00086 break; 00087 case 'b': 00088 serial->setTorqueBreak(id); 00089 break; 00090 case 'p': 00091 serial->setPosition(id, deg2rad(90), 3); 00092 break; 00093 case 'P': 00094 serial->setPosition(id, deg2rad(-90), 3); 00095 break; 00096 case 'i': 00097 {int ids[8] = {2,3,4,5,6,7,8,9}; 00098 double rads[8] = {90,90,90,90,90,90,90,90}; 00099 double secs[8] = { 3, 3, 3, 3, 3, 3, 3, 3}; 00100 serial->setPositions(8, ids, rads, secs);} 00101 break; 00102 case 'I': 00103 {int ids[8] = {2,3,4,5,6,7,8,9}; 00104 double rads[8] = {-90,-90,-90,-90,-90,-90,-90,-90}; 00105 double secs[8] = { 3, 3, 3, 3, 3, 3, 3, 3}; 00106 serial->setPositions(8, ids, rads, secs);} 00107 break; 00108 case 'm': 00109 serial->setMaxTorque(id, 100); 00110 break; 00111 case 'M': 00112 serial->setMaxTorque(id, 50); 00113 break; 00114 case 'g': 00115 double angle; 00116 serial->getPosition(id, &angle); 00117 fprintf(stderr, "angle = %f [deg]\n", angle); 00118 break; 00119 case 'G': 00120 {int ids[8] = {2,3,4,5,6,7,8,9}; 00121 double angles[8]; 00122 for(int i = 0; i < 8; i++ ) { 00123 while ( serial->getPosition(ids[i], &angles[i]) < 0 ) 00124 usleep(1000); 00125 } 00126 fprintf(stderr, "angles = %f %f %f %f %f %f %f %f [deg]\n", angles[0],angles[1],angles[2],angles[3],angles[4],angles[5],angles[6],angles[7]);} 00127 break; 00128 case 'd': 00129 double duration; 00130 serial->getDuration(id, &duration); 00131 fprintf(stderr, "duration = %f [msec]\n", duration); 00132 break; 00133 case 's': 00134 double speed; 00135 serial->getSpeed(id, &speed); 00136 fprintf(stderr, "speed = %f [deg/sec]\n", speed); 00137 break; 00138 case 't': 00139 double torque; 00140 serial->getTorque(id, &torque); 00141 fprintf(stderr, "torque = %f [mA]\n", torque); 00142 break; 00143 case 'c': 00144 double temperature; 00145 serial->getTemperature(id, &temperature); 00146 fprintf(stderr, "temperature = %f [C]\n", temperature); 00147 break; 00148 case 'v': 00149 double voltage; 00150 serial->getVoltage(id, &voltage); 00151 fprintf(stderr, "voltage = %f [V]\n", voltage); 00152 break; 00153 case 'S': 00154 unsigned char data[30]; 00155 serial->getState(id, data); 00156 fprintf(stderr, "state ="); 00157 for(int i = 0; i < 30; i++) { 00158 fprintf(stderr, " %02X", data[i]); 00159 if ( i % 10 == 9 ) fprintf(stderr, " : "); 00160 } 00161 fprintf(stderr, "\n"); 00162 break; 00163 } 00164 usage(); 00165 } 00166 } 00167 printf("done"); 00168 00169 delete serial; 00170 }