move_device.cpp
Go to the documentation of this file.
00001 
00060 #include <utility>
00061 #include "ipa_canopen_core/canopen.h"
00062 
00063 int main(int argc, char *argv[]) {
00064 
00065     if (argc != 7) {
00066         std::cout << "Arguments:" << std::endl
00067                   << "(1) device file" << std::endl
00068                   << "(2) CAN deviceID" << std::endl
00069                   << "(3) Baud Rate" << std::endl
00070                   << "(4) sync rate [msec]" << std::endl
00071                   << "(5) target velocity [rad/sec]" << std::endl
00072                   << "(6) acceleration [rad/sec^2]" << std::endl
00073                   << "(enter acceleration '0' to omit acceleration phase)" << std::endl
00074                   << "Example 1: ./move_device /dev/pcan32 12 10 0.2 0.05" << std::endl
00075                   << "Example 2 (reverse direction): "
00076                   << "./move_device /dev/pcan32 12 500K 10 -0.2 -0.05" << std::endl;
00077         return -1;
00078     }
00079     std::cout << "Interrupt motion with Ctrl-C" << std::endl;
00080     std::string deviceFile = std::string(argv[1]);
00081     uint16_t CANid = std::stoi(std::string(argv[2]));
00082     canopen::syncInterval = std::chrono::milliseconds(std::stoi(std::string(argv[4])));
00083     canopen::baudRate = std::string(argv[3]);
00084     double targetVel = std::stod(std::string(argv[5]));
00085     double accel = std::stod(std::string(argv[6]));
00086 
00087     canopen::devices[ CANid ] = canopen::Device(CANid);
00088     std::this_thread::sleep_for(std::chrono::milliseconds(10));
00089 
00090     canopen::incomingPDOHandlers[ 0x180 + CANid ] = [CANid](const TPCANRdMsg m) { canopen::defaultPDO_incoming_status( CANid, m ); };
00091     canopen::incomingPDOHandlers[ 0x480 + CANid ] = [CANid](const TPCANRdMsg m) { canopen::defaultPDO_incoming_pos( CANid, m ); };
00092     canopen::sendPos = canopen::defaultPDOOutgoing_interpolated;
00093 
00094     std::string chainName = "test_chain";
00095     std::vector <uint8_t> ids;
00096     ids.push_back(CANid);
00097     std::vector <std::string> j_names;
00098     j_names.push_back("joint_1");
00099     canopen::deviceGroups[ chainName ] = canopen::DeviceGroup(ids, j_names);
00100 
00101     canopen::init(deviceFile, chainName, canopen::syncInterval);
00102     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00103 
00104     canopen::sendSync();
00105 
00106     canopen::setMotorState((uint16_t)CANid, canopen::MS_OPERATION_ENABLED);
00107 
00108     //Necessary otherwise sometimes Schunk devices complain for Position Track Error
00109     canopen::devices[CANid].setDesiredPos((double)canopen::devices[CANid].getActualPos());
00110     canopen::devices[CANid].setDesiredVel(0);
00111 
00112     canopen::sendPos((uint16_t)CANid, (double)canopen::devices[CANid].getDesiredPos());
00113 
00114     canopen::controlPDO((uint16_t)CANid, canopen::CONTROLWORD_ENABLE_MOVEMENT, 0x00);
00115     std::this_thread::sleep_for(std::chrono::milliseconds(10));
00116 
00117     canopen::devices[CANid].setInitialized(true);
00118 
00119     if (accel != 0) {  // accel of 0 means "move at target vel immediately"
00120         std::chrono::milliseconds accelerationTime( static_cast<int>(round( 1000.0 * targetVel / accel)) );
00121         double vel = 0;
00122         auto startTime = std::chrono::high_resolution_clock::now();
00123         auto tic = std::chrono::high_resolution_clock::now();
00124 
00125         // increasing velocity ramp up to target velocity:
00126         std::cout << "Accelerating to target velocity" << std::endl;
00127         while (tic < startTime + accelerationTime) {
00128             tic = std::chrono::high_resolution_clock::now();
00129             vel = accel * 0.000001 * std::chrono::duration_cast<std::chrono::microseconds>(tic-startTime).count();
00130             canopen::devices[ CANid ].setDesiredVel(vel);
00131             std::this_thread::sleep_for(canopen::syncInterval - (std::chrono::high_resolution_clock::now() - tic));
00132             canopen::sendSync();
00133         }
00134     }
00135 
00136     // constant velocity when target vel has been reached:
00137     std::cout << "Target velocity reached!" << std::endl;
00138     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00139     //std::cout << "sending Statusword request" << std::endl;
00140     //canopen::sendSDO(CANid, canopen::STATUSWORD);
00141     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00142 
00143     while (true) {
00144         std::this_thread::sleep_for(std::chrono::seconds(1));
00145         canopen::sendSync();
00146     }
00147 }


ipa_canopen_core
Author(s): Tobias Sing, Thiago de Freitas
autogenerated on Thu Aug 27 2015 13:32:20