CANopenMotorDriver.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2012, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita on 17/04/2012
00036 *********************************************************************/
00037 
00038 #include <canopen/CANopenMotorDriver.h>
00039 
00041 /*#define CANOPEN_EXCEPT(except, msg, ...) \
00042 { \
00043     char buf[1000]; \
00044     snprintf(buf, 1000, msg " (in canopen::CANopenMotorDriver::%s)" , ##__VA_ARGS__, __FUNCTION__); \
00045     throw except(buf); \
00046 }*/
00047 
00048 canopen::CANopenMotorDriver::CANopenMotorDriver(unsigned char axis_id, ros::NodeHandle * nh) //: nh_()
00049 {
00050         id_ = axis_id;
00051     
00052     can_pub_ = nh->advertise<can_msgs::CANFrame>("/can_bus_tx", 10);
00053     can_sub_ = nh->subscribe<can_msgs::CANFrame>("/can_bus_rx", 10, &canopen::CANopenMotorDriver::receivedCANframe, this);
00054     
00055     ros::Duration(0.5).sleep();
00056 }
00057 
00058 canopen::CANopenMotorDriver::~CANopenMotorDriver()
00059 {
00060     
00061 }
00062 
00063 void canopen::CANopenMotorDriver::receivedCANframe(const can_msgs::CANFrame::ConstPtr& msg)
00064 {
00065     ROS_INFO("Got a CANFrame!!!");
00066     
00067     // If this message is not intended for this axis return
00068     if(msg->id & ID_MASK != id_) return;
00069 }
00070 
00071 
00072 
00073 /***************************************** SCALING FACTORS STUFF *****************************************/
00074 
00075 void canopen::CANopenMotorDriver::setupDCmotorWithEncoder(unsigned int num_of_encoder_lines, double Tr, double T)
00076 {    
00077     // Time units
00078     T_ = T;
00079     
00080     // Position units
00081     load_to_motor_position_ = 2*M_PI/(4*num_of_encoder_lines*Tr);
00082     
00083     // Speed units
00084     load_to_motor_speed_ = 2*M_PI/(4*num_of_encoder_lines*Tr*T);
00085     
00086     // Acceleration units
00087     load_to_motor_acceleration_ = 2*M_PI/(4*num_of_encoder_lines*Tr*T*T);
00088     
00089     // Jerk units
00090     load_to_motor_jerk_ = 2*M_PI/(4*num_of_encoder_lines*Tr*T*T*T);
00091 }
00092 
00093 void canopen::CANopenMotorDriver::setupStepperMotor(unsigned int num_of_steps, unsigned int num_of_usteps, double Tr, double T)
00094 {
00095     // Time units
00096     T_ = T;
00097     
00098     // Position units
00099     load_to_motor_position_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr);
00100     
00101     // Speed units
00102     load_to_motor_speed_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T);
00103     
00104     // Acceleration units
00105     load_to_motor_acceleration_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T*T);
00106     
00107     // Jerk units
00108     load_to_motor_jerk_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T*T*T);
00109 }
00110 
00111 
00112 
00113 /***************************************** GENERAL STUFF *****************************************/
00114 
00115 void canopen::CANopenMotorDriver::startNode()
00116 {
00117     ROS_INFO("CANopenMotorDriver - %s", __FUNCTION__);
00118     
00119     can_msgs::CANFrame frame;
00120     frame.stamp = ros::Time::now();
00121     frame.id = COB_NMT_SERVICE;
00122     frame.data.resize(2);
00123     frame.data[0] = 0x01;
00124     frame.data[1] = id_;
00125     can_pub_.publish(frame);
00126 }
00127 
00128 void canopen::CANopenMotorDriver::readyToSwitchOn()
00129 {
00130     ROS_INFO("CANopenMotorDriver - %s", __FUNCTION__);
00131     
00132     can_msgs::CANFrame frame;
00133     frame.stamp = ros::Time::now();
00134     frame.id = COB_R_PDO1+id_;
00135     frame.data.resize(2);
00136     frame.data[0] = 0x06;
00137     frame.data[1] = 0x00;
00138     can_pub_.publish(frame);
00139 }
00140 
00141 void canopen::CANopenMotorDriver::switchOn()
00142 {
00143     ROS_INFO("CANopenMotorDriver - %s", __FUNCTION__);
00144     
00145     can_msgs::CANFrame frame;
00146     frame.stamp = ros::Time::now();
00147     frame.id = COB_R_PDO1+id_;
00148     frame.data.resize(2);
00149     frame.data[0] = 0x07;
00150     frame.data[1] = 0x00;
00151     can_pub_.publish(frame);
00152 }
00153 
00154 void canopen::CANopenMotorDriver::enableOperation()
00155 {
00156     ROS_INFO("CANopenMotorDriver - %s", __FUNCTION__);
00157     
00158     can_msgs::CANFrame frame;
00159     frame.stamp = ros::Time::now();
00160     frame.id = COB_R_PDO1+id_;
00161     frame.data.resize(2);
00162     frame.data[0] = 0x0F;
00163     frame.data[1] = 0x00;
00164     can_pub_.publish(frame);
00165 }
00166 
00167 void canopen::CANopenMotorDriver::run()
00168 {
00169     ROS_INFO("CANopenMotorDriver - %s", __FUNCTION__);
00170     
00171     can_msgs::CANFrame frame;
00172     frame.stamp = ros::Time::now();
00173     frame.id = COB_R_PDO1+id_;
00174     frame.data.resize(2);
00175     frame.data[0] = 0x1F;
00176     frame.data[1] = 0x00;
00177     can_pub_.publish(frame);
00178 }
00179 
00180 
00181 
00182 /***************************************** HOMING MODE *****************************************/
00183 
00184 void canopen::CANopenMotorDriver::setupHomingMode(canopen::HomingMethod method, double fast_speed, double slow_speed, double acceleration, double offset)
00185 {
00186     can_msgs::CANFrame frame;
00187     
00188     int homing_speed_iu = (int)(fast_speed/load_to_motor_speed_*65535);
00189     
00190     ROS_INFO("CANopenMotorDriver - %s - Homing Speed: %d", __FUNCTION__, homing_speed_iu);
00191     
00192     // Homing speed during search for switch
00193     frame.stamp = ros::Time::now();
00194     frame.id = COB_R_SDO+id_;
00195     frame.data.resize(8);
00196     frame.data[0] = WRITE_REQUEST_4BYTE;
00197     frame.data[1] = 0x99;
00198     frame.data[2] = 0x60;
00199     frame.data[3] = 0x01;
00200     frame.data[4] = homing_speed_iu;
00201     frame.data[5] = homing_speed_iu>>8;
00202     frame.data[6] = homing_speed_iu>>16;
00203     frame.data[7] = homing_speed_iu>>24;
00204     can_pub_.publish(frame);
00205     
00206     int homing_slow_speed_iu = (int)(slow_speed/load_to_motor_speed_*65535);
00207     
00208     ROS_INFO("CANopenMotorDriver - %s - Homing Slow Speed: %d", __FUNCTION__, homing_slow_speed_iu);
00209     
00210     // Homing speed during search for index
00211     frame.stamp = ros::Time::now();
00212     frame.id = COB_R_SDO+id_;
00213     frame.data.resize(8);
00214     frame.data[0] = WRITE_REQUEST_4BYTE;
00215     frame.data[1] = 0x99;
00216     frame.data[2] = 0x60;
00217     frame.data[3] = 0x02;
00218     frame.data[4] = homing_slow_speed_iu;
00219     frame.data[5] = homing_slow_speed_iu>>8;
00220     frame.data[6] = homing_slow_speed_iu>>16;
00221     frame.data[7] = homing_slow_speed_iu>>24;
00222     can_pub_.publish(frame);
00223     
00224     int homing_acceleration_iu = (int)(acceleration/load_to_motor_acceleration_*65535);
00225     
00226     ROS_INFO("CANopenMotorDriver - %s - Homing Acceleration: %d", __FUNCTION__, homing_acceleration_iu);
00227     
00228     // Homing acceleration
00229     frame.stamp = ros::Time::now();
00230     frame.id = COB_R_SDO+id_;
00231     frame.data.resize(8);
00232     frame.data[0] = WRITE_REQUEST_4BYTE;
00233     frame.data[1] = 0x9A;
00234     frame.data[2] = 0x60;
00235     frame.data[3] = 0x00;
00236     frame.data[4] = homing_acceleration_iu;
00237     frame.data[5] = homing_acceleration_iu>>8;
00238     frame.data[6] = homing_acceleration_iu>>16;
00239     frame.data[7] = homing_acceleration_iu>>24;
00240     can_pub_.publish(frame);
00241     
00242     // Homing method
00243     frame.stamp = ros::Time::now();
00244     frame.id = COB_R_SDO+id_;
00245     frame.data.resize(8);
00246     frame.data[0] = WRITE_REQUEST_1BYTE;
00247     frame.data[1] = 0x98;
00248     frame.data[2] = 0x60;
00249     frame.data[3] = 0x00;
00250     frame.data[4] = method;
00251     frame.data[5] = 0x00;
00252     frame.data[6] = 0x00;
00253     frame.data[7] = 0x00;
00254     can_pub_.publish(frame);
00255     
00256     // Mode of operation - homing mode
00257     frame.stamp = ros::Time::now();
00258     frame.id = COB_R_SDO+id_;
00259     frame.data.resize(8);
00260     frame.data[0] = WRITE_REQUEST_1BYTE;
00261     frame.data[1] = 0x60;
00262     frame.data[2] = 0x60;
00263     frame.data[3] = 0x00;
00264     frame.data[4] = 0x06;
00265     frame.data[5] = 0x00;
00266     frame.data[6] = 0x00;
00267     frame.data[7] = 0x00;
00268     can_pub_.publish(frame);
00269     
00270     // TODO: Dontf forget to add the offset CAN frame!
00271 }
00272 
00273 
00274 
00275 /***************************************** POSITION MODE *****************************************/
00276 
00277 void canopen::CANopenMotorDriver::setupPositionTrapezoidalProfileMode(double target, double speed, double acceleration)
00278 {
00279     can_msgs::CANFrame frame;
00280     
00281     // Mode of operation - position mode
00282     frame.stamp = ros::Time::now();
00283     frame.id = COB_R_SDO+id_;
00284     frame.data.resize(8);
00285     frame.data[0] = WRITE_REQUEST_1BYTE;
00286     frame.data[1] = 0x60;
00287     frame.data[2] = 0x60;
00288     frame.data[3] = 0x00;
00289     frame.data[4] = 0x01;
00290     frame.data[5] = 0x00;
00291     frame.data[6] = 0x00;
00292     frame.data[7] = 0x00;
00293     can_pub_.publish(frame);
00294     
00295     // Motion profile type -  trapezoidal
00296     frame.stamp = ros::Time::now();
00297     frame.id = COB_R_SDO+id_;
00298     frame.data.resize(8);
00299     frame.data[0] = WRITE_REQUEST_2BYTE;
00300     frame.data[1] = 0x86;
00301     frame.data[2] = 0x60;
00302     frame.data[3] = 0x00;
00303     frame.data[4] = 0x00;
00304     frame.data[5] = 0x00;
00305     frame.data[6] = 0x00;
00306     frame.data[7] = 0x00;
00307     can_pub_.publish(frame);
00308     
00309     int speed_iu = (int)(speed/load_to_motor_speed_*65535);
00310     
00311     // Target speed
00312     frame.stamp = ros::Time::now();
00313     frame.id = COB_R_SDO+id_;
00314     frame.data.resize(8);
00315     frame.data[0] = WRITE_REQUEST_4BYTE;
00316     frame.data[1] = 0x81;
00317     frame.data[2] = 0x60;
00318     frame.data[3] = 0x00;
00319     frame.data[4] = speed_iu;
00320     frame.data[5] = speed_iu>>8;
00321     frame.data[6] = speed_iu>>16;
00322     frame.data[7] = speed_iu>>24;
00323     can_pub_.publish(frame);
00324     
00325     int acceleration_iu = (int)(acceleration/load_to_motor_acceleration_*65535);
00326     
00327     // Profile acceleration
00328     frame.stamp = ros::Time::now();
00329     frame.id = COB_R_SDO+id_;
00330     frame.data.resize(8);
00331     frame.data[0] = WRITE_REQUEST_4BYTE;
00332     frame.data[1] = 0x83;
00333     frame.data[2] = 0x60;
00334     frame.data[3] = 0x00;
00335     frame.data[4] = acceleration_iu;
00336     frame.data[5] = acceleration_iu>>8;
00337     frame.data[6] = acceleration_iu>>16;
00338     frame.data[7] = acceleration_iu>>24;
00339     can_pub_.publish(frame);
00340     
00341     int position_iu = target/load_to_motor_position_;
00342     
00343     // Target position
00344     frame.stamp = ros::Time::now();
00345     frame.id = COB_R_SDO+id_;
00346     frame.data.resize(8);
00347     frame.data[0] = WRITE_REQUEST_4BYTE;
00348     frame.data[1] = 0x7A;
00349     frame.data[2] = 0x60;
00350     frame.data[3] = 0x00;
00351     frame.data[4] = position_iu;
00352     frame.data[5] = position_iu>>8;
00353     frame.data[6] = position_iu>>16;
00354     frame.data[7] = position_iu>>24;
00355     can_pub_.publish(frame);
00356 }
00357 
00358 void canopen::CANopenMotorDriver::setQuickStopDeceleration(double deceleration)
00359 {
00360     can_msgs::CANFrame frame;
00361     
00362     int deceleration_iu = (int)(deceleration/load_to_motor_acceleration_*65535);
00363     
00364     // Quick deceleration
00365     frame.stamp = ros::Time::now();
00366     frame.id = COB_R_SDO+id_;
00367     frame.data.resize(8);
00368     frame.data[0] = WRITE_REQUEST_4BYTE;
00369     frame.data[1] = 0x85;
00370     frame.data[2] = 0x60;
00371     frame.data[3] = 0x00;
00372     frame.data[4] = deceleration_iu;
00373     frame.data[5] = deceleration_iu>>8;
00374     frame.data[6] = deceleration_iu>>16;
00375     frame.data[7] = deceleration_iu>>24;
00376     can_pub_.publish(frame);
00377 }
00378 
00379 
00380 
00381 /***************************************** VELOCITY MODE *****************************************/
00382 
00383 void canopen::CANopenMotorDriver::setupVelocityProfileMode(double target_speed, double acceleration)
00384 {
00385     can_msgs::CANFrame frame;
00386     
00387     // Mode of operation - velocity mode
00388     frame.stamp = ros::Time::now();
00389     frame.id = COB_R_SDO+id_;
00390     frame.data.resize(8);
00391     frame.data[0] = WRITE_REQUEST_1BYTE;
00392     frame.data[1] = 0x60;
00393     frame.data[2] = 0x60;
00394     frame.data[3] = 0x00;
00395     frame.data[4] = 0x03;
00396     frame.data[5] = 0x00;
00397     frame.data[6] = 0x00;
00398     frame.data[7] = 0x00;
00399     can_pub_.publish(frame);
00400     
00401     int speed_iu = (int)(target_speed/load_to_motor_speed_*65535);
00402     
00403     // Target speed
00404     frame.stamp = ros::Time::now();
00405     frame.id = COB_R_SDO+id_;
00406     frame.data.resize(8);
00407     frame.data[0] = WRITE_REQUEST_4BYTE;
00408     frame.data[1] = 0xFF;
00409     frame.data[2] = 0x60;
00410     frame.data[3] = 0x00;
00411     frame.data[4] = speed_iu;
00412     frame.data[5] = speed_iu>>8;
00413     frame.data[6] = speed_iu>>16;
00414     frame.data[7] = speed_iu>>24;
00415     can_pub_.publish(frame);
00416     
00417     int acceleration_iu = (int)(acceleration/load_to_motor_acceleration_*65535);
00418     
00419     // Profile acceleration
00420     frame.stamp = ros::Time::now();
00421     frame.id = COB_R_SDO+id_;
00422     frame.data.resize(8);
00423     frame.data[0] = WRITE_REQUEST_4BYTE;
00424     frame.data[1] = 0x83;
00425     frame.data[2] = 0x60;
00426     frame.data[3] = 0x00;
00427     frame.data[4] = acceleration_iu;
00428     frame.data[5] = acceleration_iu>>8;
00429     frame.data[6] = acceleration_iu>>16;
00430     frame.data[7] = acceleration_iu>>24;
00431     can_pub_.publish(frame);
00432 }
00433 
00434 // EOF


canopen
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:27:52