Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <canopen/CANopenMotorDriver.h>
00039
00041
00042
00043
00044
00045
00046
00047
00048 canopen::CANopenMotorDriver::CANopenMotorDriver(unsigned char axis_id, ros::NodeHandle * 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
00068 if(msg->id & ID_MASK != id_) return;
00069 }
00070
00071
00072
00073
00074
00075 void canopen::CANopenMotorDriver::setupDCmotorWithEncoder(unsigned int num_of_encoder_lines, double Tr, double T)
00076 {
00077
00078 T_ = T;
00079
00080
00081 load_to_motor_position_ = 2*M_PI/(4*num_of_encoder_lines*Tr);
00082
00083
00084 load_to_motor_speed_ = 2*M_PI/(4*num_of_encoder_lines*Tr*T);
00085
00086
00087 load_to_motor_acceleration_ = 2*M_PI/(4*num_of_encoder_lines*Tr*T*T);
00088
00089
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
00096 T_ = T;
00097
00098
00099 load_to_motor_position_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr);
00100
00101
00102 load_to_motor_speed_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T);
00103
00104
00105 load_to_motor_acceleration_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T*T);
00106
00107
00108 load_to_motor_jerk_ = 2*M_PI/(num_of_steps*num_of_usteps*Tr*T*T*T);
00109 }
00110
00111
00112
00113
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
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
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
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
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
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
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
00271 }
00272
00273
00274
00275
00276
00277 void canopen::CANopenMotorDriver::setupPositionTrapezoidalProfileMode(double target, double speed, double acceleration)
00278 {
00279 can_msgs::CANFrame frame;
00280
00281
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
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
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
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
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
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
00382
00383 void canopen::CANopenMotorDriver::setupVelocityProfileMode(double target_speed, double acceleration)
00384 {
00385 can_msgs::CANFrame frame;
00386
00387
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
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
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