4 using namespace controller;
10 : io_(),serial_(io_),timer_(io_),is_canceled_(false)
23 boost::system::error_code error_code;
29 serial_.set_option(serial_port_base::baud_rate(_baud_rate));
44 boost::asio::write(
serial_,buffer(_send_data));
52 usleep(_wait_time*1000);
54 boost::asio::streambuf buffer;
55 boost::asio::read_until(
serial_,buffer,
"\r");
56 const std::string result = boost::asio::buffer_cast<
const char*>(buffer.data());
58 buffer.consume(buffer.size());
67 if (_error && _error != boost::asio::error::eof) {
69 std::cout <<
"receive failed: " << std::endl;
96 boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
97 timer_.expires_from_now(boost::posix_time::milliseconds(_timeout));
106 ::tcflush(
serial_.lowest_layer().native_handle(),TCIOFLUSH);
112 : check_sum_(0),count_(0),length_(0),serial_com_()
140 std::vector<char> send_char;
144 fill(send_char.begin(),send_char.end(),0);
155 std::vector<uint8_t> receive_data;
162 std::vector<char> send_char;
166 fill(send_char.begin(),send_char.end(),0);
178 std::vector<char> send_char;
181 sprintf(convert,
"%01X",_id);
185 fill(send_char.begin(),send_char.end(),0);
190 send_char[3] = convert[0];
193 send_char[6] = convert[0];
197 for(uint8_t i=0;i<6;i++){
198 sprintf(convert,
"%02X",_data[i]);
199 send_char[9+i*2] = convert[0];
200 send_char[10+i*2] = convert[1];
203 send_char[21] =
'\r';
209 std::cout <<
"send :";
210 for (uint8_t i=0;i<
length_-1;i++) std::cout << send_char[i] ;
211 std::cout << std::endl;
219 std::vector<char> send_char;
222 sprintf(convert,
"%01X",_id);
226 fill(send_char.begin(),send_char.end(),0);
231 send_char[3] = convert[0];
234 send_char[6] = convert[0];
236 sprintf(convert,
"%02X",_line);
237 send_char[7] = convert[0];
238 send_char[8] = convert[1];
240 for(uint8_t i=0;i<6;i++){
241 sprintf(convert,
"%02X",_data[i]);
242 send_char[9+i*2] = convert[0];
243 send_char[10+i*2] = convert[1];
246 send_char[21] =
'\r';
252 std::cout <<
"send :";
253 for (uint8_t i=0;i<
length_-1;i++) std::cout << send_char[i] ;
254 std::cout << std::endl;
261 std::string data_length =
"";
262 _receive_data.clear();
268 data_length = _receive_data[4];
270 std::cout <<
"receive :" ;
271 for(
auto itr = _receive_data.begin(); itr != _receive_data.end(); ++itr) std::cout << *itr ;
272 std::cout << std::endl;
274 if((
int)_receive_data.size() < (5 + std::atoi(data_length.c_str()) * 2 + 1)){
276 std::cout <<
"size error" << std::endl;
284 std::cout <<
"not CAN commnd" << std::endl;
292 return std::strtol(_data.c_str(),NULL,16);
305 send_data_[1] =
static_cast<int>(_type[0]);
306 send_data_[2] =
static_cast<int>(_type[1]);
307 send_data_[3] =
static_cast<int>(_type[2]);
308 send_data_[4] =
static_cast<int>(_type[3]);
309 send_data_[5] =
static_cast<int>(_type[4]);
317 uint64_t data,hex_ver;
320 sscanf(_ver,
"%lx", &data);
335 uint64_t data,hex_ver;
338 sscanf(_ver,
"%lx", &data);
353 uint64_t data,hex_ver;
356 sscanf(_ver,
"%lx", &data);
374 send_data_[1] = _type >>16;
375 send_data_[2] = _type >> 8;
376 send_data_[3] = _type;
377 send_data_[4] = _volt >> 8;
378 send_data_[5] = _volt;
388 send_data_[1] = _mode;
389 send_data_[2] = _feedback;
390 send_data_[3] = 0x00;
391 send_data_[4] = 0x00;
392 send_data_[5] = 0x00;
402 send_data_[1] = _driver_max >> 8;
403 send_data_[2] = _driver_max;
404 send_data_[3] = _motor_max >> 8;
405 send_data_[4] = _motor_max;
406 send_data_[5] = _current_conversion;
416 send_data_[1] = _max >> 8;
417 send_data_[2] = _max;
418 send_data_[3] = _time >> 8;
419 send_data_[4] = _time;
420 send_data_[5] = 0x00;
430 send_data_[1] = _encoder_pulse >> 8;
431 send_data_[2] = _encoder_pulse;
432 send_data_[3] = _motor_pulse >> 8;
433 send_data_[4] = _motor_pulse;
434 send_data_[5] = 0x00;
444 send_data_[1] = 0x00;
445 send_data_[2] = 0x00;
446 send_data_[3] = 0x00;
447 send_data_[4] = 0x00;
448 send_data_[5] = 0x00;
461 send_data_[1] = _re_id;
462 send_data_[2] = 0x00;
463 send_data_[3] = 0x00;
464 send_data_[4] = 0x00;
465 send_data_[5] = 0x00;
475 send_data_[1] = _mode;
476 send_data_[2] = _io_no;
478 send_data_[4] = _reset;
479 send_data_[5] = 0x00;
489 send_data_[1] = _motor;
490 send_data_[2] = _script;
491 send_data_[3] = 0x00;
492 send_data_[4] = 0x00;
493 send_data_[5] = 0x00;
503 send_data_[1] = _auto_run;
504 send_data_[2] = _script;
505 send_data_[3] = _point_go;
506 send_data_[4] = _motor;
517 send_data_[1] = _mode;
518 send_data_[2] = _minus;
519 send_data_[3] = _plus;
521 send_data_[5] = 0x00;
531 send_data_[1] = _temperature;
532 send_data_[2] = _motor;
534 send_data_[4] = _voltage;
535 send_data_[5] = 0x00;
545 send_data_[1] = _mode;
546 send_data_[2] = 0x00;
547 send_data_[3] = 0x00;
548 send_data_[4] = 0x00;
549 send_data_[5] = 0x00;
559 send_data_[1] = _io0;
560 send_data_[2] = _io1;
561 send_data_[3] = _io2;
562 send_data_[4] = _io3;
563 send_data_[5] = 0x00;
573 send_data_[1] = _ad0;
574 send_data_[2] = _ad1;
575 send_data_[3] = _ad2;
576 send_data_[4] = _ad3;
577 send_data_[5] = 0x00;
589 send_data_[1] = _max >> 8;
590 send_data_[2] = _max;
591 send_data_[3] = _min;
592 send_data_[4] = _min_time >> 8;
593 send_data_[5] = _min_time;
602 send_data_[1] = _speed >> 8;
603 send_data_[2] = _speed;
604 send_data_[3] = 0x00;
605 send_data_[4] = 0x00;
606 send_data_[5] = 0x00;
615 send_data_[1] = _back_surge_a;
616 send_data_[2] = _back_surge_b;
617 send_data_[3] = _back_surge_c;
619 send_data_[5] = _fst;
628 send_data_[1] = _i_gain;
629 send_data_[2] = _d_gain;
630 send_data_[3] = _switch;
631 send_data_[4] = 0x00;
632 send_data_[5] = 0x00;
641 send_data_[1] = _value >> 8;
642 send_data_[2] = _value;
643 send_data_[3] = 0x00;
644 send_data_[4] = 0x00;
645 send_data_[5] = 0x00;
654 send_data_[1] = _acc >> 8;
655 send_data_[2] = _acc;
656 send_data_[3] = _dec >> 8;
657 send_data_[4] = _dec;
658 send_data_[5] = 0x00;
667 send_data_[1] = _initial_speed >> 8;
668 send_data_[2] = _initial_speed;
669 send_data_[3] = _p_gain >> 8;
670 send_data_[4] = _p_gain;
671 send_data_[5] = _correcting_gain;
680 send_data_[1] = _limit >> 16;
681 send_data_[2] = _limit >> 8;
682 send_data_[3] = _limit;
691 send_data_[1] = _limit >> 16;
692 send_data_[2] = _limit >> 8;
693 send_data_[3] = _limit;
698 void SeedCommand::setMotorRotation(uint8_t _id, uint8_t _pulse_division, uint8_t _encoder_division, uint8_t _motor_inverted, uint8_t _encoder_inverted, uint8_t _dc_inverted)
702 send_data_[1] = _pulse_division;
703 send_data_[2] = _encoder_division;
704 send_data_[3] = _motor_inverted;
705 send_data_[4] = _encoder_inverted;
706 send_data_[5] = _dc_inverted;
715 send_data_[1] = _time >> 8;
716 send_data_[2] = _time;
717 send_data_[3] = _pulse >> 16;
718 send_data_[4] = _pulse >> 8;
719 send_data_[5] = _pulse;
728 send_data_[1] = _temerature;
729 send_data_[2] = _minimum_voltage;
730 send_data_[3] = _maximum_voltage;
731 send_data_[4] = _current;
732 send_data_[5] = 0x00;
744 send_data_[1] = _number;
745 send_data_[2] = _start_line;
746 send_data_[3] = _end_line;
747 send_data_[4] = 0x00;
748 send_data_[5] = 0x00;
755 uint64_t data,hex_command;
758 sscanf(_command,
"%lx", &data);
779 send_data_[1] = _type;
780 send_data_[2] = _type;
781 send_data_[3] = 0xFF;
782 send_data_[4] = 0x00;
783 send_data_[5] = 0x00;
786 std::vector<uint8_t> receive_data;
787 std::string data =
"" ;
792 for(
int i=13;i<17;++i) data += receive_data[i];
793 if(_type == 0x1E)
lock_code_info_[
"base_parameters"] = std::strtol(data.c_str(),NULL,16);
794 else if(_type == 0x2E)
lock_code_info_[
"motor_settings"] = std::strtol(data.c_str(),NULL,16);
795 else if(_type == 0x3E)
lock_code_info_[
"script_data"] = std::strtol(data.c_str(),NULL,16);
808 send_data_[1] = 0x01;
809 send_data_[2] = _code >> 8;
810 send_data_[3] = _code;
811 send_data_[4] = 0x00;
812 send_data_[5] = 0x00;
821 for(
int i=1;i<15;++i){
832 send_data_[1] = 0x00;
833 send_data_[2] = 0x00;
834 send_data_[3] = 0x00;
835 send_data_[4] = 0x00;
836 send_data_[5] = 0x00;
842 if(_parameter.find(
"標準")!= std::string::npos)
return 0;
843 else if(_parameter.find(
"プルアップ")!= std::string::npos)
return 1;
844 else if(_parameter.find(
"プルダウン")!= std::string::npos)
return 2;
845 else if(_parameter.find(
"出力(外部変更可)")!= std::string::npos)
return 3;
846 else if(_parameter.find(
"不可")!= std::string::npos)
return 4;
847 if(_parameter.find(
"入力")!= std::string::npos)
return 5;
863 std::vector<uint8_t> receive_data;
865 std::string command =
"";
866 std::string velocity =
"" ;
867 std::string position =
"" ;
870 return {
false, 0, 0};
873 id = receive_data[5];
874 for(
int i=9;i<11;++i) command += receive_data[i];
877 for(
int i=11;i<15;++i) velocity += receive_data[i];
879 if(receive_data[15] ==
'F') position =
"FF";
881 for(
int i=15;i<21;++i) position += receive_data[i];
884 else return {
false, 0, 0};
896 std::vector<uint8_t> receive_data;
898 std::string command =
"";
899 std::string current =
"" ;
900 std::string position_error =
"" ;
903 return {
false, 0, 0};
906 id = receive_data[5];
907 for(
int i=9;i<11;++i) command += receive_data[i];
910 for(
int i=11;i<15;++i) current += receive_data[i];
912 if(receive_data[15] ==
'F') position_error =
"FF";
913 else position_error =
"";
914 for(
int i=15;i<21;++i) position_error += receive_data[i];
918 else return {
false, 0, 0};
930 std::vector<uint8_t> receive_data;
932 std::string command =
"";
933 std::string data =
"" ;
936 return {
false, 0, 0, 0, 0, 0};
939 id = receive_data[5];
940 for(
int i=9;i<11;++i) command += receive_data[i];
943 for(
int i=11;i<20;++i) data += receive_data[i];
945 operational_info_[
"motor_state"] = std::strtol(data.substr(2,2).c_str(),NULL,16);
946 operational_info_[
"running_script_number"] = std::strtol(data.substr(4,2).c_str(),NULL,16);
947 operational_info_[
"running_script_row"] = std::strtol(data.substr(6,2).c_str(),NULL,16);
948 operational_info_[
"running_point_number"] = std::strtol(data.substr(8,2).c_str(),NULL,16);
950 return {
true, operational_info_[
"status"],operational_info_[
"motor_state"],
951 operational_info_[
"running_script_number"], operational_info_[
"running_script_row"], operational_info_[
"running_point_number"]};
953 else return {
false, 0, 0, 0, 0, 0};
963 send_data_[2] = _state;
980 send_data_[2] = _script_no;
982 if ((_script_no > 0x00) && (_script_no < 0x0F))
writeSerialCommand(_id,send_data_.data());
988 int number_of_end = 0;
990 while( number_of_end < _number){
991 std::vector<uint8_t> receive_data;
993 std::string command =
"";
994 std::string data =
"" ;
997 id = receive_data[5];
998 for(
int i=9;i<11;++i) command += receive_data[i];
999 for(
int i=13;i<15;++i) data += receive_data[i];
1003 std::cout <<
"Script of ID " <<
id <<
" is the end." << std::endl;
1014 send_data_[1] = _time >> 8;
1015 send_data_[2] = _time;
1016 send_data_[3] = _position >> 16;
1017 send_data_[4] = _position >> 8;
1018 send_data_[5] = _position;
1027 send_data_[1] = _speed >> 8;
1028 send_data_[2] = _speed;
1029 send_data_[3] = _position >> 16;
1030 send_data_[4] = _position >> 8;
1031 send_data_[5] = _position;
1040 send_data_[1] = _time >> 8;
1041 send_data_[2] = _time;
1042 send_data_[3] = _position >> 16;
1043 send_data_[4] = _position >> 8;
1044 send_data_[5] = _position;
1053 send_data_[1] = _speed >> 8;
1054 send_data_[2] = _speed;
1055 send_data_[3] = _position >> 16;
1056 send_data_[4] = _position >> 8;
1057 send_data_[5] = _position;
1066 send_data_[1] = _time >> 8;
1067 send_data_[2] = _time;
1068 send_data_[3] = _position >> 16;
1069 send_data_[4] = _position >> 8;
1070 send_data_[5] = _position;
1079 send_data_[1] = _time >> 8;
1080 send_data_[2] = _time;
1081 send_data_[3] = _position >> 16;
1082 send_data_[4] = _position >> 8;
1083 send_data_[5] = _position;
1095 send_data_[1] = (-1 * _speed) >> 8;
1096 send_data_[2] = (-1 * _speed);
1101 send_data_[1] = _speed >> 8;
1102 send_data_[2] = _speed;
1113 send_data_[1] = _position;
1114 send_data_[2] = _state;
void writeSerialCommand(uint8_t _id, uint8_t *_data)
void setMotorCurrent(uint8_t _id, uint16_t _max, uint8_t _min, uint16_t _min_time)
int io2int(std::string _parameter)
void setReleaseLock(uint8_t _id, uint8_t _type, uint16_t _code)
void stopMotor(uint8_t _id)
void actuateRelativePositionByTime(uint8_t _id, int16_t _time, int32_t _position)
int str2int(std::string _data)
void actuateAbsolutePositionByTime(uint8_t _id, uint16_t _time, int32_t _position)
void setEditorVersion(uint8_t _id, const char *_ver)
SerialCommunication serial_com_
bool openPort(std::string _port, unsigned int _baud_rate)
void actuateBySpeed(uint8_t _id, int16_t _speed)
void actuateAbsolutePositionBySpeed(uint8_t _id, int16_t _speed, int32_t _position)
std::string receive_buffer_
void setScriptData(uint8_t _id, uint8_t _number, uint8_t _start_line, uint8_t _end_line)
void runScript(uint8_t _id, uint8_t _script_no)
void setSerialVersion(uint8_t _id, const char *_ver)
void setMotorError(uint8_t _id, uint16_t _time, uint32_t _pulse)
void setMotorParam(uint8_t _id, uint8_t _mode, uint8_t _feedback)
void setIdParam(uint8_t _id, uint8_t _re_id)
void readBufferAsync(std::string _delim, uint16_t _timeout=50)
void setMotorErrorLimit(uint8_t _id, uint8_t _temerature, uint8_t _minimum_voltage, uint8_t _maximum_voltage, uint8_t _current)
bool getLockCode(uint8_t _id, uint8_t _mode)
void setErrorMotionParam(uint8_t _id, uint8_t _temperature, uint8_t _motor, uint8_t _ot, uint8_t _voltage)
boost::asio::streambuf stream_buffer_
std::unordered_map< std::string, int32_t > operational_info_
void writeRom(uint8_t _id, uint8_t type)
void writeScriptLine(uint8_t _id, uint8_t _line, const char *_command)
void setCurrentInstantaneous(uint8_t _id, uint16_t _max, uint16_t _time)
void actuateContinuousAbsolutePosition(uint8_t _id, uint16_t _time, int32_t _position)
std::array< int, 3 > getCurrent(uint8_t _id)
void waitForScriptEnd(int _number)
void setOvertravelParam(uint8_t _id, uint8_t _mode, uint8_t _minus, uint8_t _plus, uint8_t _io)
void setMotorRotation(uint8_t _id, uint8_t _pulse_division, uint8_t _encoder_division, uint8_t _motor_inverted, uint8_t _encoder_inverted, uint8_t _dc_inverted)
void setEncoderParam(uint8_t _id, uint16_t _encoder_pulse, uint16_t _motor_pulse)
void setMotorCurrentParam(uint8_t _id, uint16_t _driver_max, uint16_t _motor_max, uint8_t _current_conversion)
std::vector< uint8_t > connected_id_
std::vector< uint8_t > send_data_
void setMotorAdaptation(uint8_t _id, uint32_t _type, uint16_t _volt)
void setDummy(uint8_t _id, uint8_t _cmd)
std::string readBuffer(uint16_t _wait_time=0)
void setUpperSoftwareLimit(uint8_t _id, int32_t _limit)
void setFirmwareVersion(uint8_t _id, const char *_ver)
void onServo(uint8_t _id, uint8_t _state)
void setTypeNumber(uint8_t _id, const char *_type)
void setMotorControlParameter2(uint8_t _id, uint16_t _initial_speed, uint16_t _p_gain, uint8_t _correcting_gain)
std::array< int, 6 > getOperationalInfo(uint8_t _id)
void setStopModeParam(uint8_t _id, uint8_t _motor, uint8_t _script)
bool readSerialCommand(std::vector< uint8_t > &_receive_data, uint16_t _timeout=50)
void setResponseParam(uint8_t _id, uint8_t _mode)
void setDioParam(uint8_t _id, uint8_t _io0, uint8_t _io1, uint8_t _io2, uint8_t _io3)
void onTimer(const boost::system::error_code &_error)
void setAcDecelerationRate(uint8_t _id, uint16_t _acc, uint16_t _dec)
void setEmergencyParam(uint8_t _id, uint8_t _mode, uint8_t _io_no, uint8_t _io, uint8_t _reset)
std::unordered_map< std::string, int16_t > lock_code_info_
void actuateRelativePositionBySpeed(uint8_t _id, int16_t _speed, int32_t _position)
void writeBuffer(std::vector< char > &_send_data)
void setPosition(uint8_t _id, uint8_t _position, uint8_t _state)
void onReceive(const boost::system::error_code &_error, size_t _bytes_transferred)
void actuateContinuousRelativePosition(uint8_t _id, uint16_t _time, int32_t _position)
void setOperationParam(uint8_t _id, uint8_t _auto_run, uint8_t _script, uint8_t _point_go, uint8_t _motor, uint8_t _io)
void setAdParam(uint8_t _id, uint8_t _ad0, uint8_t _ad1, uint8_t _ad2, uint8_t _ad3)
void setMotorMaxSpeed(uint8_t _id, uint16_t _speed)
std::vector< uint8_t > getConnectedId()
void setLowerSoftwareLimit(uint8_t _id, int32_t _limit)
bool openPort(std::string _port, unsigned int _baud_rate)
void setMotorControlParameter1(uint8_t _id, uint8_t _back_surge_a, uint8_t _back_surge_b, uint8_t _back_surge_c, uint8_t _oc, uint8_t _fst)
void setInPosition(uint8_t _id, uint16_t _value)
std::array< int, 3 > getPosition(uint8_t _id)