dxl-changeid.cpp
Go to the documentation of this file.
00001 /*
00002  * dxl-changeid.cpp - FTDI serial communication version
00003  *
00004  *  Created on: Nov 17, 2010
00005  *      Author: Erik Schuitema
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         // Find all dynamixels and configure and init them
00035         CDxlConfig dxlConfig;
00036         gDynamixel.setSerialPort(&serialPort);
00037         gDynamixel.setConfig(dxlConfig.setID(gOldID));
00038         int initResult = gDynamixel.init(false);
00039         if (initResult == DXL_SUCCESS)  // false means: do not send (the default) config to motor
00040         {
00041                 // Dynamixel with ID = iID responded!
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         // Check if init worked
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         // Change baud rate if needed
00076         if (gNewBaudRate > 0)
00077         {
00078                 // Send new baud rate command
00079                 gDynamixel.setBaudRate(gNewBaudRate);
00080                 // Change serial port speed
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         // Set serial device name
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         // Open serial port
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         // Set correct baud rate
00131         int baudrate = atoi(argv[2]);
00132         serialPort.set_speed_int(baudrate);
00133 
00134         // Create dynamixel task - not needed anymore
00135         int ret = 0;
00136 
00137         // Detect old ID
00138         gOldID = atoi(argv[3]);
00139 
00140         // Detect new ID
00141         gNewID = atoi(argv[4]);
00142 
00143         // Detect possible new baud rate
00144         if (argc == 6)
00145                 gNewBaudRate = atoi(argv[5]);
00146 
00147         // Start the appropriate task
00148         dxl_task_change_proc(NULL);
00149 
00150         // Now wait for the main proc to finish
00151         while (!gDxlTaskProcDone)
00152                 usleep((int)1E5);       // 100ms
00153 
00154         // Clean up
00155         serialPort.port_close();
00156         printf("End of dxl-all.\n");
00157         return 0;
00158 }


threemxl
Author(s):
autogenerated on Fri Aug 28 2015 13:21:08