Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <sys/mman.h>
00009 #include <signal.h>
00010 #include <threemxl/platform/hardware/dynamixel/dynamixel/Dynamixel.h>
00011
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include <stdlib.h>
00015
00016 LxSerial serialPort;
00017
00018 #define MAX_DEVICE_NAME_LEN 20
00019 char devicename[MAX_DEVICE_NAME_LEN];
00020
00021 int gNumDynamixels = 0;
00022 int gOldID = 1;
00023 int gNewID = 1;
00024 int gNewBaudRate = -1;
00025 CDynamixel gDynamixel;
00026
00027 bool gDxlTaskProcDone=false;
00028
00029 bool gQuit=false;
00030
00031 bool dxl_init_all_motors()
00032 {
00033 printf("Trying to init Dynamixel with ID %d ...", gOldID);
00034
00035 CDxlConfig dxlConfig;
00036 gDynamixel.setSerialPort(&serialPort);
00037 gDynamixel.setConfig(dxlConfig.setID(gOldID));
00038 int initResult = gDynamixel.init(false);
00039 if (initResult == DXL_SUCCESS)
00040 {
00041
00042 printf("success!\n");
00043 return true;
00044 }
00045 else
00046 {
00047 printf("failed (error = %d).\n", initResult);
00048 fflush(stdout);
00049 return false;
00050 }
00051 }
00052
00053 void dxl_task_change_proc(void *arg)
00054 {
00055
00056 if (!dxl_init_all_motors())
00057 {
00058 printf("Check if initial baud rate and ID were set correctly. Quitting.\n");
00059 gDxlTaskProcDone = true;
00060 return;
00061 }
00062
00063 printf("Changing ID to %d...", gNewID);
00064 int error = gDynamixel.changeID(gNewID);
00065 if (error == DXL_SUCCESS)
00066 printf("done! ID is now %d!\n", gDynamixel.getID());
00067 else
00068 {
00069 printf("failed (error %d)\n", error);
00070 printf("Could not set new ID. Quitting.\n");
00071 gDxlTaskProcDone = true;
00072 return;
00073 }
00074
00075
00076 if (gNewBaudRate > 0)
00077 {
00078
00079 gDynamixel.setBaudRate(gNewBaudRate);
00080
00081 serialPort.set_speed_int(gNewBaudRate);
00082 }
00083
00084 int pingResult = gDynamixel.ping();
00085 if (pingResult == DXL_SUCCESS)
00086 printf("Final ping check .. done!\n");
00087 else
00088 printf("Final ping check .. fail! (ID = %d; error = %d)\nSomething went wrong!?\n", gDynamixel.getID(), pingResult);
00089
00090 gDxlTaskProcDone = true;
00091 }
00092
00093 void catch_signal(int sig)
00094 {
00095 printf("Break signal received.\n");
00096 gQuit = true;
00097 }
00098
00099 int main(int argc, char** argv)
00100 {
00101 if (argc < 5)
00102 {
00103 printf("Usage: dxl-changeid [serial device] [baud-rate] [old-ID] [new-ID] [new baud rate (optional)]\n");
00104 return -1;
00105 }
00106 else
00107 if (argc > 6)
00108 {
00109 printf("Too many arguments!\n");
00110 printf("Usage: dxl-changeid [serial device] [baud-rate] [old-ID] [new-ID] [new baud rate (optional)]\n");
00111 return -1;
00112 }
00113
00114
00115 if (strlen(argv[1]) < MAX_DEVICE_NAME_LEN)
00116 strcpy(devicename, argv[1]);
00117 else
00118 {
00119 printf("[ERROR] Device name too long (probably not /dev/ttyUSB0 ..)!\n");
00120 return -1;
00121 }
00122
00123
00124 if (!serialPort.port_open(devicename, LxSerial::RS485_FTDI))
00125 {
00126 printf("[ERROR] Failed to open serial port!\n");
00127 return -1;
00128 }
00129
00130
00131 int baudrate = atoi(argv[2]);
00132 serialPort.set_speed_int(baudrate);
00133
00134
00135 int ret = 0;
00136
00137
00138 gOldID = atoi(argv[3]);
00139
00140
00141 gNewID = atoi(argv[4]);
00142
00143
00144 if (argc == 6)
00145 gNewBaudRate = atoi(argv[5]);
00146
00147
00148 dxl_task_change_proc(NULL);
00149
00150
00151 while (!gDxlTaskProcDone)
00152 usleep((int)1E5);
00153
00154
00155 serialPort.port_close();
00156 printf("End of dxl-all.\n");
00157 return 0;
00158 }