test.cpp
Go to the documentation of this file.
00001 #include <canopen_master/canopen.h>
00002 #include <canopen_master/master.h>
00003 #include <boost/make_shared.hpp>
00004 #include <iostream>
00005 
00006 #include <socketcan_interface/dispatcher.h>
00007 #include <boost/unordered_set.hpp>
00008 #include <socketcan_interface/socketcan.h>
00009 #include <socketcan_interface/threading.h>
00010 
00011 #include <boost/thread.hpp>
00012 
00013 using namespace can;
00014 using namespace canopen;
00015 
00016 boost::shared_ptr<ThreadedInterface<SocketCANInterface> > driver = boost::make_shared<ThreadedInterface<SocketCANInterface> > ();
00017 
00018 void print_frame(const Frame &f){
00019     LOG( "in: " << std:: hex << f.id << std::dec);
00020 }
00021 void print_tpdo(const Frame &f){
00022     LOG( "TPDO: " << std:: hex << f.id << std::dec);
00023 }
00024 
00025 void print_state(const State &f){
00026     LOG("STATE");
00027 }
00028 
00029 void print_node_state(const Node::State &s){
00030     LOG("NMT:" << s);
00031 }
00032 
00033 int main(int argc, char *argv[]){
00034 
00035     if(argc < 3){
00036         std::cout << "Usage: " << argv[0] << " DEVICE EDS/DCF [sync_ms]" << std::endl;
00037         return -1;
00038     }
00039 
00040     // Interface::FrameListener::Ptr printer = driver->createMsgListener(print_frame); // printer for all incoming messages
00041     // Interface::FrameListener::Ptr tprinter = driver->createMsgListener(Header(0x181), print_tpdo); // printer for all incoming messages
00042     StateInterface::StateListener::Ptr sprinter = driver->createStateListener(print_state); // printer for all incoming messages
00043 
00044     int sync_ms = 10;
00045     if(argc > 3) sync_ms = atol(argv[3]);
00046 
00047     if(!driver->init(argv[1],true)){
00048         std::cout << "init failed" << std::endl;
00049         return -1;
00050     }
00051 
00052     sleep(1.0);
00053 
00054     boost::shared_ptr<canopen::ObjectDict>  dict = canopen::ObjectDict::fromFile(argv[2]);
00055 
00056     LocalMaster master(driver);
00057     boost::shared_ptr<SyncLayer> sync = master.getSync(SyncProperties(can::MsgHeader(0x80), sync_ms, 0));
00058 
00059     Node node(driver, dict, 1, sync);
00060 
00061     canopen::ObjectStorage::Entry<canopen::ObjectStorage::DataType<0x006>::type >  sw;
00062     canopen::ObjectStorage::Entry<canopen::ObjectStorage::DataType<0x006>::type >  cw;
00063     canopen::ObjectStorage::Entry<int8_t >  op_mode;
00064     canopen::ObjectStorage::Entry<int8_t >  op_mode_disp;
00065 
00066     node.getStorage()->entry(sw, 0x6041);
00067     node.getStorage()->entry(op_mode, 0x6060);
00068     node.getStorage()->entry(op_mode_disp, 0x6061);
00069     node.getStorage()->entry(cw, 0x6040);
00070 
00071     canopen::ObjectStorage::Entry<int32_t > actual_vel = node.getStorage()->entry<int32_t>(0x606C);
00072     Node::StateListener::Ptr nsl = node.addStateListener(print_node_state);
00073 
00074     node.reset();
00075     node.start();
00076 
00077     //LOG("Homing: " << (int) node.getStorage()->entry<int8_t>(0x6098).get());
00078 
00079     sleep(1.0);
00080 
00081     op_mode.set(3);
00082     LOG("Mode: " << (int) op_mode_disp.get());
00083     cw.set(0x6); // ready to switch on
00084     cw.set(0x7); // switch on
00085     cw.set(0xf); // enable operation
00086 
00087     node.getStorage()->entry<int32_t>(0x60ff).set(360000); // set target velocity
00088 
00089     while(true){
00090         boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
00091         //LOG("Status: " << sw.get());
00092         LOG("Velocity: " <<  actual_vel.get());
00093     }
00094     driver->run();
00095 
00096 
00097     return 0;
00098 }


canopen_test_utils
Author(s): Mathias Lüdtke
autogenerated on Fri Feb 12 2016 02:43:15