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

test/angleTest.cpp

Go to the documentation of this file.
00001 
00002 #include <iostream>
00003 #include <termios.h>
00004 #include <errno.h>
00005 #include <fcntl.h>
00006 #include "../truckd/servo.h"
00007 
00008 using std::cin;
00009 using std::cout;
00010 using std::cerr;
00011 using std::endl;
00012 
00013 
00014 
00015 
00016 int main(int argc, char * argv[])
00017 {
00018         int servo_fd;
00019   int result;
00020         
00021         int servoNum;
00022 
00023         char *servo_dev = "/dev/ttyS1"; 
00024 
00025 
00026         // grab servo number
00027         cout << "Enter servo number (0 to 7) > ";
00028         cin >> servoNum;
00029         if (!cin) {
00030                 cerr << "Invalid input" << endl;
00031                 return 1;
00032         }
00033         if ((servoNum < 0) || (servoNum > 7)) {
00034                 cerr << "Servo number not between 0 and 7" << endl;
00035                 return 1;
00036         }
00037 
00038 
00039         float minAngle, minValue ;
00040         float maxAngle, maxValue;
00041 
00042         // get angle servo mapping
00043         cout << "Enter minimum angle and servo value for it > ";
00044         cin >> minAngle >> minValue;
00045         if (!cin) {
00046                 cerr << "Invalid input" << endl;
00047                 return 1;
00048         }
00049 
00050         // get angle servo mapping
00051         cout << "Enter maximum angle and servo value for it > ";
00052         cin >> maxAngle >> maxValue;
00053         if (!cin) {
00054                 cerr << "Invalid input" << endl;
00055                 return 1;
00056         }
00057 
00058         if (minAngle >= maxAngle) {
00059                 cerr << "MIN angle must be less than MAX angle" << endl;
00060                 return 1;
00061         }
00062 
00063         cout << "MIN angle : " << minAngle << "  value : " << minValue << endl
00064                          << "MAX angle : " << maxAngle << "  value : " << maxValue << endl;
00065 
00066         Servo s(servoNum, 0, minAngle, minValue, maxAngle, maxValue);
00067 
00068 
00069   // open serial port for controlling servos / speed controllers
00070         struct termios newtio;
00071         servo_fd = open(servo_dev, O_WRONLY | O_NOCTTY | O_NONBLOCK);
00072         
00073         if (servo_fd < 0)
00074         {       perror("hwInterface : opening serial port failed");
00075                 exit(EXIT_FAILURE);
00076         }
00077         // set new port settings for 9600 baud, ignore control lines
00078         newtio.c_cflag = CS8 | B9600 | CLOCAL;
00079         newtio.c_iflag = IGNPAR;
00080         newtio.c_oflag = 0;
00081         newtio.c_lflag = 0;
00082         newtio.c_cc[VMIN]=1;
00083         newtio.c_cc[VTIME]=0;
00084         tcflush(servo_fd, TCIFLUSH);
00085         result = tcsetattr(servo_fd,TCSANOW,&newtio);
00086         
00087         if (result == -1) {
00088                 perror("hwInterface tcsetattr failed");
00089                 return 1;
00090         }
00091 
00092         float angle;
00093 
00094         do {
00095                 cout << "Enter angle value > ";
00096                 cin >> angle;
00097                 if (!cin) {
00098                         cerr << "Invalid input" << endl;
00099                         break;
00100                 }                       
00101 
00102                 s.setAngle(angle);
00103 
00104                 // write servo command to serial port
00105     unsigned char command[3];           
00106                 command[0] = 0xff; //controller magic number
00107                 command[1] = servoNum;
00108                 command[2] = s.getValue();
00109 
00110                 cout << "moving servo " << servoNum << " to " << s.getValue() << endl;
00111                 int result = write(servo_fd, command, sizeof(command));      
00112                 if (result == -1) {     
00113                         if (errno == EAGAIN) {
00114                                 fprintf(stderr, "hwInterface write, would block writing servo values to serial port\n");
00115                         }
00116                         else {
00117                                 perror("hwInterface error writing to serial port");
00118                         }
00119                         break;
00120                 }
00121 
00122         } while (1);
00123 
00124         
00125         close(servo_fd);
00126 
00127         return 0;
00128 }
00129 

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