canopen.h
Go to the documentation of this file.
00001 
00060 #ifndef CANOPEN_H
00061 #define CANOPEN_H
00062 
00063 #include <iostream>
00064 #include <map>
00065 #include <vector>
00066 #include <string>
00067 #include <cstring>
00068 #include <chrono>
00069 #include <unordered_map>
00070 #include <functional>
00071 #include <cstdlib>
00072 #include <thread>
00073 #include <math.h>
00074 #include <libpcan.h>
00075 #include <utility>
00076 #include <fcntl.h>    // for O_RDWR
00077 #include <stdint.h>
00078 #include <inttypes.h>
00079 #include "schunkErrors.h"
00080 
00081 namespace canopen{
00082 
00083     extern std::chrono::milliseconds syncInterval;
00084     extern std::string baudRate;
00085 
00086     /***************************************************************/
00087     // Define baudrates variables for accessing as string
00088     // this overrrides the definitions from the libpcan.h
00089     /**************************************************************/
00090     //static std::map<std::string, uint16_t> baudrates;
00091 
00092     static std::map<std::string, uint32_t> baudrates = {
00093         {"1M" , CAN_BAUD_1M},
00094         {"500K" , CAN_BAUD_500K},
00095         {"250K" , CAN_BAUD_250K},
00096         {"125K" , CAN_BAUD_125K},
00097         {"100K" , CAN_BAUD_100K},
00098         {"50K" , CAN_BAUD_50K},
00099         {"20K" , CAN_BAUD_20K},
00100         {"10K" , CAN_BAUD_10K},
00101         {"5K" , CAN_BAUD_5K}
00102     };
00103 
00104     /***************************************************************/
00105     //              define classes and structs
00106     /***************************************************************/
00107 
00108     class Device{
00109 
00110         private:
00111 
00112             uint8_t CANid_;
00113             double conversion_factor_;
00114             double offset_;
00115             std::string NMTState_;
00116             std::string motorState_;
00117             std::string deviceFile_;
00118             std::string name_;
00119             std::string group_;
00120 
00121             std::vector<char> manufacturer_sw_version_;
00122             std::vector<char> manufacturer_hw_version_;
00123             std::vector<char> manufacturer_device_name_;
00124 
00125             std::vector<uint16_t> vendor_id_;
00126             std::vector<uint16_t> product_code_;
00127             uint16_t revision_number_;
00128 
00129             std::string error_register_;
00130             std::string manufacturer_error_register_;
00131 
00132             bool initialized_;
00133             bool nmt_init_;
00134             bool driveReferenced_;
00135             bool ip_mode_active_;
00136             bool homingError_;
00137             double actualPos_;          // unit = rad
00138             double desiredPos_;         // unit = rad
00139             double actualVel_;          // unit = rad/sec
00140             double desiredVel_;         // unit = rad/sec
00141             std::chrono::milliseconds timeStamp_msec_;
00142             std::chrono::microseconds timeStamp_usec_;
00143 
00144             int8_t modes_of_operation_display_;
00145 
00146             bool hardware_limit_positive_;
00147             bool hardware_limit_negative_;
00148 
00149 
00150             bool ready_switch_on_;
00151             bool switched_on_;
00152             bool op_enable_;
00153             bool fault_;
00154             bool volt_enable_;
00155             bool quick_stop_;
00156             bool switch_on_disabled_;
00157             bool warning_;
00158 
00159             bool mode_specific_;
00160             bool remote_;
00161             bool target_reached_;
00162             bool internal_limit_;
00163             bool op_specific_;
00164             bool op_specific1_;
00165             bool man_specific1_;
00166             bool man_specific2_;
00167 
00168             double temperature_;
00169 
00170         public:
00171 
00172             Device() {};
00173 
00174             Device(uint16_t CANid):
00175                 CANid_(CANid),
00176                 desiredVel_(0),
00177                 actualVel_(0),
00178                 desiredPos_(0),
00179                 actualPos_(0),
00180                 initialized_(false),
00181                 NMTState_("START_UP"),
00182                 motorState_("START_UP"),
00183                 nmt_init_(false) {};
00184 
00185             Device(uint16_t CANid, std::string name, std::string group, std::string bus):
00186                 CANid_(CANid),
00187                 name_(name),
00188                 group_(group),
00189                 deviceFile_(bus),
00190                 desiredVel_(0),
00191                 actualVel_(0),
00192                 desiredPos_(0),
00193                 actualPos_(0),
00194                 initialized_(false),
00195                 NMTState_("START_UP"),
00196                 motorState_("START_UP"),
00197                 nmt_init_(false) {};
00198 
00199             Device(uint16_t CANid, std::string name, std::string group):
00200                 CANid_(CANid),
00201                 name_(name),
00202                 group_(group),
00203                 desiredVel_(0),
00204                 actualVel_(0),
00205                 desiredPos_(0),
00206                 actualPos_(0),
00207                 NMTState_("START_UP"),
00208                 motorState_("START_UP"),
00209                 initialized_(false),
00210                 nmt_init_(false) {};
00211 
00212 
00213             Device(uint16_t CANid, std::string name, std::string group, std::string bus, double conversion_factor, double offsets):
00214                 CANid_(CANid),
00215                 name_(name),
00216                 group_(group),
00217                 deviceFile_(bus),
00218                 conversion_factor_(conversion_factor),
00219                 offset_(offsets),
00220                 desiredVel_(0),
00221                 actualVel_(0),
00222                 desiredPos_(0),
00223                 actualPos_(0),
00224                 NMTState_("START_UP"),
00225                 motorState_("START_UP"),
00226                 initialized_(false),
00227                 nmt_init_(false) {};
00228 
00229             bool getNMTInit(){
00230                 return nmt_init_;
00231             }
00232             
00233             std::string getNMTState(){
00234                 return NMTState_;
00235             }
00236             
00237             std::string getMotorState(){
00238                 return motorState_;
00239             }
00240 
00241             std::vector<char> getManufacturerSWVersion(){
00242                 return manufacturer_sw_version_;
00243             }
00244 
00245             std::vector<char> getManufacturerHWVersion(){
00246                 return manufacturer_hw_version_;
00247             }
00248 
00249             std::vector<char> getManufacturerDevName(){
00250                 return manufacturer_device_name_;
00251             }
00252 
00253            std::string getManufactureErrorRegister(){
00254                 return manufacturer_error_register_;
00255             }
00256 
00257             std::vector<uint16_t> getVendorID(){
00258                 return vendor_id_;
00259             }
00260 
00261             std::vector<uint16_t> getProdCode(){
00262                 return product_code_;
00263             }
00264 
00265             uint16_t getRevNumber(){
00266                 return revision_number_;
00267             }
00268 
00269             uint8_t getCANid(){
00270                 return CANid_;
00271             }
00272 
00273             double getConversionFactor(){
00274                 return conversion_factor_;
00275             }
00276 
00277             std::string getDeviceFile(){
00278                 return deviceFile_;
00279             }
00280             std::string getGroup(){
00281                 return group_;
00282             }
00283             std::string getName(){
00284                 return name_;
00285             }
00286 
00287             bool getInitialized(){
00288                 return initialized_;
00289             }
00290 
00291 
00292             bool getVoltageEnabled(){
00293                 return volt_enable_;
00294             }
00295 
00296             double getDriverTemperature(){
00297                 return temperature_;
00298             }
00299 
00300             bool getReadySwitchOn(){
00301                 return ready_switch_on_;
00302             }
00303 
00304             bool getSwitchOn(){
00305                 return switched_on_;
00306             }
00307 
00308             bool getOpEnabled(){
00309                 return op_enable_;
00310             }
00311 
00312             bool getQuickStop(){
00313                 return quick_stop_;
00314             }
00315 
00316             bool getSwitchOnDisabled(){
00317                 return switch_on_disabled_;
00318             }
00319 
00320             bool getWarning(){
00321                 return warning_;
00322             }
00323 
00324             bool getModeSpecific(){
00325                 return mode_specific_;
00326             }
00327 
00328             bool getRemote(){
00329                 return remote_;
00330             }
00331             bool getTargetReached(){
00332                 return target_reached_;
00333             }
00334 
00335             bool getInternalLimits(){
00336                 return internal_limit_;
00337             }
00338 
00339 
00340             bool getOpSpec0(){
00341                 return op_specific_;
00342             }
00343 
00344             bool getOpSpec1(){
00345                 return op_specific1_;
00346             }
00347 
00348             bool getManSpec1(){
00349                 return man_specific1_;
00350             }
00351 
00352             bool getmanSpec2(){
00353                 return man_specific2_;
00354             }
00355 
00356             bool getHomingError(){
00357                 return homingError_;
00358             }
00359 
00360 
00361             bool getNegativeLimit(){
00362                 return hardware_limit_negative_;
00363             }
00364 
00365             bool getPositiveLimit(){
00366                 return hardware_limit_positive_;
00367             }
00368 
00369             bool getFault(){
00370                 return fault_;
00371             }
00372 
00373 
00374             int8_t getCurrentModeofOperation()
00375             {
00376                 return modes_of_operation_display_;
00377             }
00378 
00379             std::string getErrorRegister(){
00380                 return error_register_;
00381             }
00382 
00383             bool getIPMode(){
00384                 return ip_mode_active_;
00385             }
00386 
00387             bool getDriveReferenced(){
00388                 return driveReferenced_;
00389             }
00390             double getActualPos(){
00391                 return actualPos_;
00392             }
00393             double getDesiredPos(){
00394                 return desiredPos_;
00395             }
00396 
00397             double getActualVel(){
00398                 return actualVel_;
00399             }
00400 
00401             double getDesiredVel(){
00402                 return desiredVel_;
00403             }
00404 
00405             inline std::chrono::milliseconds getTimeStamp_msec(){
00406                 return timeStamp_msec_;
00407             }
00408 
00409             inline std::chrono::microseconds getTimeStamp_usec(){
00410                 return timeStamp_usec_;
00411             }
00412 
00413             void setActualPos(double pos){
00414                 actualPos_ = pos;
00415             }
00416 
00417             void setConversionFactor(double conversion_factor){
00418                 conversion_factor_ = conversion_factor;
00419             }
00420 
00421             void setDesiredPos(double pos){
00422                 desiredPos_ = pos;
00423             }
00424 
00425 
00426             void setActualVel(double vel){
00427                 actualVel_ = vel;
00428             }
00429 
00430             void setDesiredVel(double vel){
00431                 desiredVel_ = vel;
00432             }
00433 
00434             void setMotorState(std::string nextState){
00435                 motorState_ = nextState;
00436             }
00437 
00438             void setManufacturerSWVersion(std::vector<char> ms_version){
00439                 manufacturer_sw_version_ = ms_version;
00440             }
00441 
00442             void setManufacturerHWVersion(std::vector<char> mh_version){
00443                 manufacturer_hw_version_ = mh_version;
00444             }
00445 
00446             void setManufacturerDevName(std::vector<char> dev_name){
00447                 manufacturer_device_name_ = dev_name;
00448             }
00449 
00450             void setVendorID(std::vector<uint16_t> v_id){
00451                 vendor_id_ = v_id;
00452             }
00453 
00454             void setProdCode(std::vector<uint16_t> prod_code){
00455                 product_code_ = prod_code;
00456             }
00457 
00458 
00459             void setRevNum(uint16_t rev_num){
00460                 revision_number_ = rev_num;
00461             }
00462 
00463 
00464             void setNMTState(std::string nextState){
00465                 NMTState_ = nextState;
00466             }
00467 
00468 
00469             void setVoltageEnabled(bool voltage_enabled){
00470                 volt_enable_ = voltage_enabled;
00471             }
00472 
00473             void setDriverTemperature(double temperature){
00474                 temperature_ = temperature;
00475             }
00476 
00477             void setReadySwitchON(bool r_switch_on){
00478                 ready_switch_on_ = r_switch_on;
00479             }
00480 
00481             void setSwitchON(bool switch_on){
00482                 switched_on_ = switch_on;
00483             }
00484 
00485             void setOpEnable(bool op_enable){
00486                 op_enable_ = op_enable;
00487             }
00488 
00489             void setQuickStop(bool quick_stop){
00490                 quick_stop_ = quick_stop;
00491             }
00492 
00493             void setSwitchOnDisable(bool switch_disabled){
00494                 switch_on_disabled_ = switch_disabled;
00495             }
00496 
00497             void setWarning(bool warning){
00498                 warning_ = warning;
00499             }
00500 
00501 
00502             void setModeSpec(bool modespec){
00503                 mode_specific_ = modespec;
00504             }
00505 
00506 
00507             void setRemote(bool remote){
00508                 remote_ = remote;
00509             }
00510 
00511             void setManSpec1(bool manspec1){
00512                 man_specific1_ = manspec1;
00513             }
00514 
00515             void setTargetReached(bool target_reached){
00516                 target_reached_ = target_reached;
00517             }
00518 
00519             void setInternalLimits(bool internal_limits){
00520                 internal_limit_ = internal_limits;
00521             }
00522 
00523 
00524             void setManSpec2(bool manspec2){
00525                 man_specific2_ = manspec2;
00526             }
00527 
00528             void setOpSpec1(bool opspec1){
00529                 op_specific1_ = opspec1;
00530             }
00531 
00532             void setOpSpec0(bool opspec0){
00533                 op_specific_ = opspec0;
00534             }
00535 
00536             void setPositiveLimit(bool pos_limit){
00537                 hardware_limit_positive_ = pos_limit;
00538             }
00539 
00540 
00541             void setNegativeLimit(bool neg_limit){
00542                 hardware_limit_negative_ = neg_limit;
00543             }
00544             
00545             void setNMTInit(bool nmt_limit)
00546             {
00547                 nmt_init_ = nmt_limit;
00548             }
00549 
00550             void setFault(bool fault){
00551                 fault_ = fault;
00552             }
00553 
00554             void setCurrentModeofOperation(int8_t mode_display)
00555             {
00556                 modes_of_operation_display_ = mode_display;
00557             }
00558 
00559             void setErrorRegister(std::string error_register){
00560                 error_register_ = error_register;
00561             }
00562 
00563             void setManufacturerErrorRegister(std::string manufacturer_error_register){
00564                 manufacturer_error_register_ = manufacturer_error_register;
00565             }
00566 
00567             void setIPMode(bool ip_mode){
00568                 ip_mode_active_ = ip_mode;
00569             }
00570 
00571             void setHoming(bool homing_error){
00572                 homingError_ = homing_error;
00573             }
00574 
00575             void setInitialized(bool initialized){
00576                 initialized_ = initialized;
00577             }
00578 
00579             void updateDesiredPos(){
00580                 desiredPos_ += desiredVel_ * (syncInterval.count() / 1000.0);
00581             }
00582 
00583             void setTimeStamp_msec(std::chrono::milliseconds timeStamp){
00584                 timeStamp_msec_ = timeStamp;
00585             }
00586 
00587             void setTimeStamp_usec(std::chrono::microseconds timeStamp){
00588                 timeStamp_usec_ = timeStamp;
00589             }
00590     };
00591 
00592     extern std::map<uint8_t, Device> devices;
00593 
00594     class DeviceGroup{
00595 
00596         private:
00597 
00598             std::vector<uint8_t> CANids_;
00599             std::vector<std::string> names_;
00600             bool initialized_;
00601             bool atFirstInit_;
00602 
00603         public:
00604 
00605             DeviceGroup() {};
00606 
00607             DeviceGroup(std::vector<uint8_t> CANids):
00608                 CANids_(CANids) {};
00609 
00610             DeviceGroup(std::vector<uint8_t> CANids, std::vector<std::string> names):
00611                 CANids_(CANids),
00612                 names_(names),
00613                 initialized_(false),
00614                 atFirstInit_(true) {};
00615 
00616 
00617             std::vector<uint8_t> getCANids(){
00618                 return CANids_;
00619             }
00620 
00621             std::vector<std::string> getNames(){
00622                 return names_;
00623             }
00624 
00625             void setInitialized(bool initialized){
00626                 initialized_ = initialized;
00627             }
00628 
00629             bool getInitialized(){
00630                 return initialized_;
00631             }
00632 
00633             bool getFirstInit()
00634             {
00635                 return atFirstInit_;
00636             }
00637 
00638             void setFirstInit(bool initialized)
00639             {
00640                 initialized_ = initialized;
00641             }
00642 
00643 
00644             std::vector<double> getActualPos() {
00645                     std::vector<double> actualPos;
00646                     for (uint8_t CANid : CANids_)
00647                     actualPos.push_back(devices[CANid].getActualPos());
00648                     return actualPos;
00649                 }
00650 
00651                 std::vector<double> getDesiredPos() {
00652                     std::vector<double> desiredPos;
00653                     for (auto CANid : CANids_)
00654                         desiredPos.push_back(devices[CANid].getDesiredPos());
00655                     return desiredPos;
00656                 }
00657 
00658             std::vector<double> getActualVel() {
00659                 std::vector<double> actualVel;
00660                 for (auto CANid : CANids_)
00661                     actualVel.push_back(devices[CANid].getActualVel());
00662                 return actualVel;
00663             }
00664 
00665             std::vector<double> getDesiredVel() {
00666                 std::vector<double> desiredVel;
00667                 for (auto CANid : CANids_)
00668                     desiredVel.push_back(devices[CANid].getDesiredVel());
00669                 return desiredVel;
00670             }
00671 
00672             void setVel(std::vector<double> velocities) {
00673                 for (unsigned int i=0; i<velocities.size(); i++) {
00674                     devices[CANids_[i]].setDesiredVel(velocities[i]);
00675                 }
00676                 }
00677     };
00678 
00679     struct SDOkey{
00680         uint16_t index;
00681         uint8_t subindex;
00682 
00683         inline SDOkey(TPCANRdMsg m):
00684             index((m.Msg.DATA[2] << 8) + m.Msg.DATA[1]),
00685             subindex(m.Msg.DATA[3]) {};
00686 
00687         inline SDOkey(uint16_t i, uint8_t s):
00688             index(i),
00689             subindex(s) {};
00690     };
00691 
00692     /***************************************************************/
00693     //          define global variables and functions
00694     /***************************************************************/
00695 
00696     inline bool operator<(const SDOkey &a, const SDOkey&b) {
00697             return a.index < b.index || (a.index == b.index && a.subindex < b.subindex);
00698     }
00699 
00700     inline int32_t rad2mdeg(double phi){
00701         return static_cast<int32_t>(round(phi/(2*M_PI)*360000.0));
00702     }
00703 
00704     inline double mdeg2rad(int32_t alpha){
00705         return static_cast<double>(static_cast<double>(alpha)/360000.0*2*M_PI);
00706     }
00707 
00708     void sdo_incoming(uint8_t CANid, BYTE data[8]);
00709     void errorword_incoming(uint8_t CANid, BYTE data[8]);
00710     void manufacturer_incoming(uint8_t CANid, BYTE data[8]);
00711 
00712     extern std::map<std::string, DeviceGroup> deviceGroups;     // DeviceGroup name -> DeviceGroup object
00713     extern std::vector<std::thread> managerThreads;
00714     extern HANDLE h;
00715     extern std::vector<std::string> openDeviceFiles;
00716     extern bool atFirstInit;
00717     extern int initTrials;
00718     extern std::map<SDOkey, std::function<void (uint8_t CANid, BYTE data[8])> > incomingDataHandlers;
00719     extern std::map<uint16_t, std::function<void (const TPCANRdMsg m)> > incomingPDOHandlers;
00720     extern std::map<uint16_t, std::function<void (const TPCANRdMsg m)> > incomingEMCYHandlers;
00721 
00722     /***************************************************************/
00723     //                  define state machine functions
00724     /***************************************************************/
00725 
00726     void setNMTState(uint16_t CANid, std::string targetState);
00727     void setMotorState(uint16_t CANid, std::string targetState);
00728     bool setOperationMode(uint16_t CANid, const int8_t targetMode, double timeout = 3.0);
00729 
00730     /***************************************************************/
00731     //  define get errors functions
00732     /***************************************************************/
00733     void makeRPDOMapping(std::string chainName,int object, std::vector<std::string> registers, std::vector<int> sizes, u_int8_t sync_type);
00734     void disableRPDO(std::string chainName, int object);
00735     void clearRPDOMapping(std::string chainName, int object);
00736     void enableRPDO(std::string chainName, int object);
00737 
00738     void setObjects(std::string chainName);
00739 
00740     void makeTPDOMapping(std::string chainName, int object, std::vector<std::string> registers, std::vector<int> sizes, u_int8_t sync_type);
00741     void disableTPDO(std::string chainName,int object);
00742     void clearTPDOMapping(std::string chainName,int object);
00743     void enableTPDO(std::string chainName,int object);
00744 
00745     void pdoChanged(std::string chainName);
00746 
00747     void getErrors(uint16_t CANid);
00748     std::vector<char> obtainManSWVersion(uint16_t CANid, std::shared_ptr<TPCANRdMsg> m);
00749     std::vector<char> obtainManHWVersion(uint16_t CANid, std::shared_ptr<TPCANRdMsg> m);
00750     std::vector<char> obtainManDevName(uint16_t CANid, int size_name);
00751     std::vector<uint16_t> obtainVendorID(uint16_t CANid);
00752     uint16_t obtainRevNr(uint16_t CANid, std::shared_ptr<TPCANRdMsg> m);
00753     std::vector<uint16_t> obtainProdCode(uint16_t CANid, std::shared_ptr<TPCANRdMsg> m);
00754     void readErrorsRegister(uint16_t CANid, std::shared_ptr<TPCANRdMsg> m);
00755     void readManErrReg(uint16_t CANid);
00756 
00757 
00758     /***************************************************************/
00759     //  define init and recover variables and functions
00760     /***************************************************************/
00761 
00762     extern bool sdo_protect;
00763     extern BYTE protect_msg[];
00764 
00765     extern bool atFirstInit;
00766     extern bool recover_active;
00767     extern bool no_position;
00768     extern bool halt_active;
00769 
00770     extern bool halt_positive;
00771     extern bool halt_negative;
00772 
00773     extern bool use_limit_switch;
00774 
00775     extern std::string operation_mode_param;
00776 
00777     bool openConnection(std::string devName, std::string baudrate);
00778     //Another init option
00779     bool init(std::string deviceFile, std::string chainName, std::chrono::milliseconds syncInterval);
00780     bool init(std::string deviceFile, std::string chainName, const int8_t mode_of_operation);
00781 
00782     void pre_init(std::string chainName);
00783     bool recover(std::string deviceFile, std::string chainName, std::chrono::milliseconds syncInterval);
00784     void halt(std::string deviceFile, std::string chainName, std::chrono::milliseconds syncInterval);
00785 
00786     extern std::function< void (uint16_t CANid, double positionValue) > sendPos;
00787     extern std::function< void (uint16_t CANid, double positionValue, double velocityValue) > sendPosPPMode;
00788     extern std::function< void (uint16_t CANid, double velocityValue) > sendVel;
00789     extern std::function< void (uint16_t CANid) > geterrors;
00790 
00791 
00792     /***************************************************************/
00793     //  define NMT constants, variables and functions
00794     /***************************************************************/
00795 
00796     const uint8_t NMT_START_REMOTE_NODE = 0x01;
00797     const uint8_t NMT_STOP_REMOTE_NODE = 0x02;
00798     const uint8_t NMT_ENTER_PRE_OPERATIONAL = 0x80;
00799     const uint8_t NMT_RESET_NODE = 0x81;
00800     const uint8_t NMT_RESET_COMMUNICATION = 0x82;
00801 
00802     extern TPCANMsg NMTmsg;
00803 
00804     inline void sendNMT(uint8_t CANid, uint8_t command)
00805     {
00806         TPCANMsg NMTmsg;
00807         std::memset(&NMTmsg, 0, sizeof(NMTmsg));
00808         NMTmsg.ID = 0;
00809         NMTmsg.MSGTYPE = 0x00;
00810         NMTmsg.LEN = 2;
00811 
00812         //std::cout << "Sending NMT. CANid: " << (uint16_t)CANid << "\tcommand: " << (uint16_t)command << std::endl;
00813         NMTmsg.DATA[0] = command;
00814         NMTmsg.DATA[1] = CANid;
00815         CAN_Write(h, &NMTmsg);
00816     }
00817 
00818     /***************************************************************/
00819     //  define SYNC variables and functions
00820     /***************************************************************/
00821 
00822     extern TPCANMsg syncMsg;
00823 
00824     inline void sendSync() {
00825         TPCANMsg syncMsg;
00826         std::memset(&syncMsg, 0, sizeof(syncMsg));
00827         syncMsg.ID = 0x80;
00828         syncMsg.MSGTYPE = 0x00;
00829 
00830         syncMsg.LEN = 0x00;
00831 
00832         CAN_Write(h, &syncMsg);
00833     }
00834 
00835     /***************************************************************/
00836     //          define NMT error control constants
00837     /***************************************************************/
00838 
00839     const SDOkey HEARTBEAT(0x1017,0x0);
00840 
00841     const uint16_t HEARTBEAT_TIME = 1500;
00842 
00843     /***************************************************************/
00844     //          Error Constants for Error Register
00845     /***************************************************************/
00846 
00847     static unsigned char const EMC_k_1001_GENERIC        = 0x01;
00848     static unsigned char const EMC_k_1001_CURRENT        = 0x02;
00849     static unsigned char const EMC_k_1001_VOLTAGE        = 0x04;
00850     static unsigned char const EMC_k_1001_TEMPERATURE    = 0x08;
00851     static unsigned char const EMC_k_1001_COMMUNICATION  = 0x10;
00852     static unsigned char const EMC_k_1001_DEV_PROF_SPEC  = 0x20;
00853     static unsigned char const EMC_k_1001_RESERVED       = 0x40;
00854     static unsigned char const EMC_k_1001_MANUFACTURER   = 0x80;
00855 
00856     /***************************************************************/
00857     //          define motor state constants
00858     /***************************************************************/
00859 
00860     const std::string MS_NOT_READY_TO_SWITCH_ON = "NOT_READY_TO_SWITCH_ON";
00861     const std::string MS_FAULT = "FAULT";
00862     const std::string MS_SWITCHED_ON_DISABLED = "SWITCHED_ON_DISABLED";
00863     const std::string MS_READY_TO_SWITCH_ON = "READY_TO_SWITCH_ON";
00864     const std::string MS_SWITCHED_ON = "SWITCHED_ON";
00865     const std::string MS_OPERATION_ENABLED = "OPERATION_ENABLED";
00866     const std::string MS_QUICK_STOP_ACTIVE = "QUICK_STOP_ACTIVE";
00867     const std::string MS_FAULT_REACTION_ACTIVE = "FAULT_REACTION_ACTIVE";
00868 
00869     /***************************************************************/
00870     //          define SDO protocol constants and functions
00871     /***************************************************************/
00872 
00873     const SDOkey STATUSWORD(0x6041, 0x0);
00874     const SDOkey ERRORWORD(0x1001, 0x0);
00875     const SDOkey DRIVERTEMPERATURE(0x22A2, 0x0);
00876     const SDOkey MANUFACTURER(0x1002, 0x0);
00877     const SDOkey MANUFACTURERDEVICENAME(0x1008, 0x0);
00878     const SDOkey MANUFACTURERHWVERSION(0x1009, 0x0);
00879     const SDOkey MANUFACTURERSOFTWAREVERSION(0x100A, 0x0);
00880 
00881     const SDOkey IDENTITYVENDORID(0x1018, 0x01);
00882     const SDOkey IDENTITYPRODUCTCODE(0x1018, 0x02);
00883     const SDOkey IDENTITYREVNUMBER(0x1018, 0x03);
00884 
00885     /*************************
00886      * Specific for schunk hardware
00887      ************************/
00888     const SDOkey SCHUNKLINE(0x200b, 0x1);
00889     const SDOkey SCHUNKDETAIL(0x200b, 0x3);
00890     /****************************************
00891      */
00892 
00893     const SDOkey CONTROLWORD(0x6040, 0x0);
00894     const SDOkey MODES_OF_OPERATION(0x6060, 0x0);
00895     const SDOkey MODES_OF_OPERATION_DISPLAY(0x6061, 0x0);
00896     const SDOkey SYNC_TIMEOUT_FACTOR(0x200e, 0x0);
00897     const SDOkey IP_TIME_UNITS(0x60C2, 0x1);
00898     const SDOkey IP_TIME_INDEX(0x60C2, 0x2);
00899     const SDOkey ERROR_CODE(0x603F, 0x0);
00900     const SDOkey ABORT_CONNECTION(0x6007, 0x0);
00901     const SDOkey QUICK_STOP(0x605A, 0x0);
00902     const SDOkey SHUTDOWN(0x605B, 0x0);
00903     const SDOkey DISABLE_CODE(0x605C, 0x0);
00904     const SDOkey HALT(0x605D, 0x0);
00905     const SDOkey FAULT(0x605E, 0x0);
00906     const SDOkey MODES(0x6060, 0x0);
00907 
00908     /* Constants for the PDO mapping */
00909     const int TPDO1_msg = 0x180;
00910     const int TPDO2_msg = 0x280;
00911     const int TPDO3_msg = 0x380;
00912     const int TPDO4_msg = 0x480;
00913 
00914     const int RPDO1_msg = 0x200;
00915     const int RPDO2_msg = 0x300;
00916     const int RPDO3_msg = 0x400;
00917     const int RPDO4_msg = 0x500;
00918 
00919     const int TSDO = 0x580;
00920     const int RSDO = 0x600;
00921 
00922     //TPDO PARAMETERS
00923     const SDOkey TPDO(0x1800, 0x0);
00924 
00925     //RPDO PARAMETERS
00926     const SDOkey RPDO(0x1400, 0x0);
00927 
00928     //TPDO MAPPING
00929     const SDOkey TPDO_map(0x1A00, 0x0);
00930 
00931     //RPDO MAPPING
00932     const SDOkey RPDO_map(0x1600, 0x0);
00933 
00934     const uint16_t CONTROLWORD_SHUTDOWN = 6;
00935     const uint16_t CONTROLWORD_QUICKSTOP = 2;
00936     const uint16_t CONTROLWORD_SWITCH_ON = 7;
00937     const uint16_t CONTROLWORD_ENABLE_OPERATION = 15;
00938     const uint16_t CONTROLWORD_ENABLE_MOVEMENT = 31;
00939     const uint16_t CONTROLWORD_START_HOMING = 16;
00940     const uint16_t CONTROLWORD_ENABLE_IP_MODE = 16;
00941     const uint16_t CONTROLWORD_DISABLE_INTERPOLATED = 7;
00942     const uint16_t CONTROLWORD_DISABLE_OPERATION = 7;
00943     const uint16_t CONTROL_WORD_DISABLE_VOLTAGE = 0x7D;
00944     const uint16_t CONTROLWORD_FAULT_RESET_0 = 0x00; //0x00;
00945     const uint16_t CONTROLWORD_FAULT_RESET_1 = 0x80;
00946     const uint16_t CONTROLWORD_HALT = 0x100;
00947 
00948     const int8_t MODES_OF_OPERATION_HOMING_MODE = 0x6;
00949     const int8_t MODES_OF_OPERATION_PROFILE_POSITION_MODE = 0x1;
00950     const int8_t MODES_OF_OPERATION_VELOCITY_MODE = 0x2;
00951     const int8_t MODES_OF_OPERATION_PROFILE_VELOCITY_MODE = 0x3;
00952     const int8_t MODES_OF_OPERATION_TORQUE_PROFILE_MODE = 0x4;
00953     const int8_t MODES_OF_OPERATION_INTERPOLATED_POSITION_MODE = 0x7;
00954 
00955     static const char * const modesDisplay[] =
00956     {"NO_MODE", "PROFILE_POSITION_MODE", "VELOCITY", "PROFILE_VELOCITY_MODE",
00957                               "TORQUE_PROFILED_MODE", "RESERVED", "HOMING_MODE", "INTERPOLATED_POSITION_MODE",
00958                               "CYCLIC_SYNCHRONOUS_POSITION"};
00959 
00960     const int8_t IP_TIME_INDEX_MILLISECONDS = 0xFD;
00961     const int8_t IP_TIME_INDEX_HUNDREDMICROSECONDS = 0xFC;
00962     const uint8_t SYNC_TIMEOUT_FACTOR_DISABLE_TIMEOUT = 0;
00963 
00964     void uploadSDO(uint8_t CANid, SDOkey sdo);
00965     void controlPDO(uint8_t CANid, u_int16_t control1, u_int16_t control2);
00966     void processSingleSDO(uint8_t CANid, std::shared_ptr<TPCANRdMsg> message);
00967     void requestDataBlock1(uint8_t CANid);
00968     void requestDataBlock2(uint8_t CANid);
00969 
00970     void sendSDO(uint8_t CANid, SDOkey sdo, uint32_t value);
00971     void sendSDO(uint8_t CANid, SDOkey sdo, int32_t value);
00972     void sendSDO_unknown(uint8_t CANid, SDOkey sdo, int32_t value);
00973     void sendSDO(uint8_t CANid, SDOkey sdo, uint16_t value);
00974     void sendSDO(uint8_t CANid, SDOkey sdo, uint8_t value);
00975 
00976     /***************************************************************/
00977     //          define PDO protocol functions
00978     /***************************************************************/
00979 
00980     void initDeviceManagerThread(std::string chainName, std::function<void (std::string)> const& deviceManager);
00981     void deviceManager(std::string chainName);
00982 
00983 
00984     void defaultPDOOutgoing(uint16_t CANid, double positionValue);
00985     void defaultPDOOutgoing_interpolated(uint16_t CANid, double positionValue);
00986     void defaultPDO_incoming(uint16_t CANid, const TPCANRdMsg m);
00987     void defaultPDO_incoming_status(uint16_t CANid, const TPCANRdMsg m);
00988     void defaultPDO_incoming_pos(uint16_t CANid, const TPCANRdMsg m);
00989     void defaultEMCY_incoming(uint16_t CANid, const TPCANRdMsg m);
00990 
00991     /***************************************************************/
00992     //          define functions for receiving data
00993     /***************************************************************/
00994 
00995     void initListenerThread(std::function<void ()> const& listener);
00996     void defaultListener();
00997 }
00998 
00999 #endif


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