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     canopen::init(deviceFile, canopen::syncInterval);
00095     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00096 
00097     canopen::sendSync();
00098 
00099     if (accel != 0) {  // accel of 0 means "move at target vel immediately"
00100         std::chrono::milliseconds accelerationTime( static_cast<int>(round( 1000.0 * targetVel / accel)) );
00101         double vel = 0;
00102         auto startTime = std::chrono::high_resolution_clock::now();
00103         auto tic = std::chrono::high_resolution_clock::now();
00104 
00105         // increasing velocity ramp up to target velocity:
00106         std::cout << "Accelerating to target velocity" << std::endl;
00107         while (tic < startTime + accelerationTime) {
00108             tic = std::chrono::high_resolution_clock::now();
00109             vel = accel * 0.000001 * std::chrono::duration_cast<std::chrono::microseconds>(tic-startTime).count();
00110             canopen::devices[ CANid ].setDesiredVel(vel);
00111             std::this_thread::sleep_for(canopen::syncInterval - (std::chrono::high_resolution_clock::now() - tic));
00112             canopen::sendSync();
00113         }
00114     }
00115 
00116     // constant velocity when target vel has been reached:
00117     std::cout << "Target velocity reached!" << std::endl;
00118     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00119     //std::cout << "sending Statusword request" << std::endl;
00120     //canopen::sendSDO(CANid, canopen::STATUSWORD);
00121     std::this_thread::sleep_for(std::chrono::milliseconds(100));
00122 
00123     while (true) {
00124         std::this_thread::sleep_for(std::chrono::seconds(1));
00125         canopen::sendSync();
00126     }
00127 }


ipa_canopen_core
Author(s): Tobias Sing, Thiago de Freitas
autogenerated on Mon Oct 6 2014 00:59:25