testServoSerial.cpp
Go to the documentation of this file.
1 #include "ServoSerial.h"
2 
3 #define deg2rad(deg) ((deg*M_PI/180.0))
4 
5 int kbhit(void)
6 {
7  struct termios oldt, newt;
8  int ch;
9  int oldf;
10 
11  tcgetattr(STDIN_FILENO, &oldt);
12  newt = oldt;
13  newt.c_lflag &= ~(ICANON | ECHO);
14  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
15  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
16  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
17 
18  ch = getchar();
19 
20  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
21  fcntl(STDIN_FILENO, F_SETFL, oldf);
22 
23  if (ch != EOF) {
24  ungetc(ch, stdin);
25  return 1;
26  }
27 
28  return 0;
29 }
30 
31 static int id = 2;
32 void usage() {
33  printf("servo test program, current id = %d\n", id);
34  printf(" 1-9 : change id\n");
35  printf(" r : setReset\n");
36  printf(" o : setTorqueOn\n");
37  printf(" f : setTorqueOff\n");
38  printf(" b : setTorqueBreak\n");
39  printf(" p : setPosition to 90\n");
40  printf(" P : setPosition to -90\n");
41  printf(" i : setPositions 90 to id 2 3 4 5 6 6 8\n");
42  printf(" I : setPositions -90 to id 2 3 4 5 6 6 8\n");
43  printf(" m : setMaxTorque to 100\n");
44  printf(" M : setMaxTorque to 50\n");
45  printf(" g : getPosition\n");
46  printf(" G : getPositions\n");
47  printf(" d : getDuration\n");
48  printf(" s : getSpeed\n");
49  printf(" t : getTorque\n");
50  printf(" c : getTemperature\n");
51  printf(" v : getVoltage\n");
52  printf(" S : getState\n");
53 }
54 
55 int main() {
56  ServoSerial *serial;
57 
58  serial = new ServoSerial("/dev/serusb1");
59 
60  char ch = 0;
61  usage();
62  while (ch!='q') {
63  if(kbhit()) {
64  ch = getchar();
65  switch (ch) {
66  case '1':
67  case '2':
68  case '3':
69  case '4':
70  case '5':
71  case '6':
72  case '7':
73  case '8':
74  case '9':
75  id = ch-'0';
76  serial->clear_packet();
77  break;
78  case 'r':
79  serial->setReset(id);
80  break;
81  case 'o':
82  serial->setTorqueOn(id);
83  break;
84  case 'f':
85  serial->setTorqueOff(id);
86  break;
87  case 'b':
88  serial->setTorqueBreak(id);
89  break;
90  case 'p':
91  serial->setPosition(id, deg2rad(90), 3);
92  break;
93  case 'P':
94  serial->setPosition(id, deg2rad(-90), 3);
95  break;
96  case 'i':
97  {int ids[8] = {2,3,4,5,6,7,8,9};
98  double rads[8] = {90,90,90,90,90,90,90,90};
99  double secs[8] = { 3, 3, 3, 3, 3, 3, 3, 3};
100  serial->setPositions(8, ids, rads, secs);}
101  break;
102  case 'I':
103  {int ids[8] = {2,3,4,5,6,7,8,9};
104  double rads[8] = {-90,-90,-90,-90,-90,-90,-90,-90};
105  double secs[8] = { 3, 3, 3, 3, 3, 3, 3, 3};
106  serial->setPositions(8, ids, rads, secs);}
107  break;
108  case 'm':
109  serial->setMaxTorque(id, 100);
110  break;
111  case 'M':
112  serial->setMaxTorque(id, 50);
113  break;
114  case 'g':
115  double angle;
116  serial->getPosition(id, &angle);
117  fprintf(stderr, "angle = %f [deg]\n", angle);
118  break;
119  case 'G':
120  {int ids[8] = {2,3,4,5,6,7,8,9};
121  double angles[8];
122  for(int i = 0; i < 8; i++ ) {
123  while ( serial->getPosition(ids[i], &angles[i]) < 0 )
124  usleep(1000);
125  }
126  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]);}
127  break;
128  case 'd':
129  double duration;
130  serial->getDuration(id, &duration);
131  fprintf(stderr, "duration = %f [msec]\n", duration);
132  break;
133  case 's':
134  double speed;
135  serial->getSpeed(id, &speed);
136  fprintf(stderr, "speed = %f [deg/sec]\n", speed);
137  break;
138  case 't':
139  double torque;
140  serial->getTorque(id, &torque);
141  fprintf(stderr, "torque = %f [mA]\n", torque);
142  break;
143  case 'c':
144  double temperature;
145  serial->getTemperature(id, &temperature);
146  fprintf(stderr, "temperature = %f [C]\n", temperature);
147  break;
148  case 'v':
149  double voltage;
150  serial->getVoltage(id, &voltage);
151  fprintf(stderr, "voltage = %f [V]\n", voltage);
152  break;
153  case 'S':
154  unsigned char data[30];
155  serial->getState(id, data);
156  fprintf(stderr, "state =");
157  for(int i = 0; i < 30; i++) {
158  fprintf(stderr, " %02X", data[i]);
159  if ( i % 10 == 9 ) fprintf(stderr, " : ");
160  }
161  fprintf(stderr, "\n");
162  break;
163  }
164  usage();
165  }
166  }
167  printf("done");
168 
169  delete serial;
170 }
int getSpeed(int id, double *duration)
Definition: ServoSerial.h:178
int getVoltage(int id, double *voltage)
Definition: ServoSerial.h:234
int getPosition(int id, double *angle)
Definition: ServoSerial.h:150
int getDuration(int id, double *duration)
Definition: ServoSerial.h:164
void clear_packet()
Definition: ServoSerial.h:389
png_uint_32 i
int temperature(int s)
int setTorqueOff(int id)
Definition: ServoSerial.h:138
int getTemperature(int id, double *temperature)
Definition: ServoSerial.h:220
int getState(int id, unsigned char *data)
Definition: ServoSerial.h:248
int setMaxTorque(int id, short percentage)
Definition: ServoSerial.h:125
int getTorque(int id, double *torque)
Definition: ServoSerial.h:206
int main()
void usage()
int setTorqueBreak(int id)
Definition: ServoSerial.h:144
int setPosition(int id, double rad)
Definition: ServoSerial.h:78
#define deg2rad(deg)
int setTorqueOn(int id)
Definition: ServoSerial.h:132
int setReset(int id)
Definition: ServoSerial.h:74
int setPositions(int len, int *id, double *rad)
Definition: ServoSerial.h:86
JSAMPIMAGE data
int usleep(useconds_t usec)
int kbhit(void)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51