seed3_command.cpp
Go to the documentation of this file.
2 #include <iostream> // for cout/cerr
3 using namespace seed;
4 using namespace controller;
5 
6 //#define DEBUG
7 
10 : io_(),serial_(io_),timer_(io_),is_canceled_(false)
11 {
12 }
13 
16 {
17  if(serial_.is_open())serial_.close();
18 }
19 
21 bool SerialCommunication::openPort(std::string _port, unsigned int _baud_rate)
22 {
23  boost::system::error_code error_code;
24  serial_.open(_port,error_code);
25  if(error_code){
26  return false;
27  }
28  else{
29  serial_.set_option(serial_port_base::baud_rate(_baud_rate));
30  return true;
31  }
32 }
33 
36 {
37  if(serial_.is_open())serial_.close();
38 }
39 
41 void SerialCommunication::writeBuffer(std::vector<char>& _send_data)
42 {
43  serial_mtx_.lock();
44  boost::asio::write(serial_,buffer(_send_data));
45  serial_mtx_.unlock();
46 }
47 
49 std::string SerialCommunication::readBuffer(uint16_t _wait_time)
50 {
51  serial_mtx_.lock();
52  usleep(_wait_time*1000);
53 
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());
57 
58  buffer.consume(buffer.size());
59 
60  serial_mtx_.unlock();
61  return result;
62 }
63 
65 void SerialCommunication::onReceive(const boost::system::error_code& _error, size_t _bytes_transferred)
66 {
67  if (_error && _error != boost::asio::error::eof) {
68 #if DEBUG
69  std::cout << "receive failed: " << std::endl;
70 #endif
71  }
72  else {
73  const std::string data(boost::asio::buffer_cast<const char*>(stream_buffer_.data()), stream_buffer_.size());
74  receive_buffer_ = data;
75 
76  stream_buffer_.consume(stream_buffer_.size());
77  timer_.cancel();
78  is_canceled_ = true;
79  }
80 }
81 
83 void SerialCommunication::onTimer(const boost::system::error_code& _error)
84 {
85  if (!_error && !is_canceled_) serial_.cancel();
86 }
87 
89 void SerialCommunication::readBufferAsync(std::string _delim="\r", uint16_t _timeout)
90 {
91  receive_buffer_.clear();
92  is_canceled_ = false;
93 
94  boost::asio::async_read_until(serial_,stream_buffer_,_delim,
95  boost::bind(&SerialCommunication::onReceive, this,
96  boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
97  timer_.expires_from_now(boost::posix_time::milliseconds(_timeout));
98  timer_.async_wait(boost::bind(&SerialCommunication::onTimer, this, _1));
99  io_.reset();
100  io_.run();
101 }
102 
105 {
106  ::tcflush(serial_.lowest_layer().native_handle(),TCIOFLUSH);
107 }
108 //*******************************************************************
109 //*******************************************************************
112 : check_sum_(0),count_(0),length_(0),serial_com_()
113 {
114  send_data_.resize(6);
115 }
116 
119 {
120  closeCom();
121 }
122 
124 bool SeedCommand::openPort(std::string _port, unsigned int _baud_rate){
125  if(serial_com_.openPort(_port, _baud_rate)) is_open_ = true;
126  else is_open_ = false;
127 
128  return is_open_;
129 }
130 
134  is_open_ = false;
135 }
136 
139 {
140  std::vector<char> send_char;
141 
142  length_ = 5;
143  send_char.resize(length_);
144  fill(send_char.begin(),send_char.end(),0);
145 
146  send_char[0] = 'Z';
147  send_char[1] = '0';
148  send_char[2] = '\r';
149  send_char[3] = 'O';
150  send_char[4] = '\r';
151 
153  serial_com_.writeBuffer(send_char);
154 
155  std::vector<uint8_t> receive_data;
156  readSerialCommand(receive_data);
157 }
158 
161 {
162  std::vector<char> send_char;
163 
164  length_ = 2;
165  send_char.resize(length_);
166  fill(send_char.begin(),send_char.end(),0);
167 
168  send_char[0] = 'C';
169  send_char[1] = '\r';
170 
172  serial_com_.writeBuffer(send_char);
173 }
174 
176 void SeedCommand::writeSerialCommand(uint8_t _id, uint8_t *_data)
177 {
178  std::vector<char> send_char;
179  char convert[3]={0};
180 
181  sprintf(convert,"%01X",_id);
182 
183  length_ = 22;
184  send_char.resize(length_);
185  fill(send_char.begin(),send_char.end(),0);
186 
187  send_char[0] = 't';
188  send_char[1] = '3';
189  send_char[2] = '0';
190  send_char[3] = convert[0];
191  send_char[4] = '8';
192  send_char[5] = 'F';
193  send_char[6] = convert[0];
194  send_char[7] = '0';
195  send_char[8] = '0';
196 
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];
201  }
202 
203  send_char[21] = '\r';
204 
206  serial_com_.writeBuffer(send_char);
207 
208 #if DEBUG
209  std::cout << "send :";
210  for (uint8_t i=0;i<length_-1;i++) std::cout << send_char[i] ;
211  std::cout << std::endl;
212 #endif
213 
214 }
215 
217 void SeedCommand::writeSerialCommand(uint8_t _id, uint8_t _line, uint8_t *_data)
218 {
219  std::vector<char> send_char;
220  char convert[3]={0};
221 
222  sprintf(convert,"%01X",_id);
223 
224  length_ = 22;
225  send_char.resize(length_);
226  fill(send_char.begin(),send_char.end(),0);
227 
228  send_char[0] = 't';
229  send_char[1] = '3';
230  send_char[2] = '0';
231  send_char[3] = convert[0];
232  send_char[4] = '8';
233  send_char[5] = 'F';
234  send_char[6] = convert[0];
235 
236  sprintf(convert,"%02X",_line);
237  send_char[7] = convert[0];
238  send_char[8] = convert[1];
239 
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];
244  }
245 
246  send_char[21] = '\r';
247 
249  serial_com_.writeBuffer(send_char);
250 
251 #if DEBUG
252  std::cout << "send :";
253  for (uint8_t i=0;i<length_-1;i++) std::cout << send_char[i] ;
254  std::cout << std::endl;
255 #endif
256 }
257 
259 bool SeedCommand::readSerialCommand(std::vector<uint8_t>& _receive_data, uint16_t _timeout)
260 {
261  std::string data_length = "";
262  _receive_data.clear();
263 
264  serial_com_.readBufferAsync("\r",_timeout);
265 
266  if(serial_com_.receive_buffer_.find("t") != std::string::npos){
267  for(size_t i = serial_com_.receive_buffer_.find("t");i<serial_com_.receive_buffer_.size();++i) _receive_data.push_back(serial_com_.receive_buffer_[i]);
268  data_length = _receive_data[4];
269 #if DEBUG
270  std::cout << "receive :" ;
271  for(auto itr = _receive_data.begin(); itr != _receive_data.end(); ++itr) std::cout << *itr ;
272  std::cout << std::endl;
273 #endif
274  if((int)_receive_data.size() < (5 + std::atoi(data_length.c_str()) * 2 + 1)){
275 #if DEBUG
276  std::cout << "size error" << std::endl;
277 #endif
278  return false;
279  }
280  else return true;
281  }
282  else{
283 #if DEBUG
284  std::cout << "not CAN commnd" << std::endl;
285 #endif
286  return false;
287  }
288 }
289 
290 int SeedCommand::str2int(std::string _data)
291 {
292  return std::strtol(_data.c_str(),NULL,16);
293 }
294 
295 /*******************
296  Fixed Parameters
297 ********************/
298 
300 void SeedCommand::setTypeNumber(uint8_t _id, const char* _type)
301 {
302  fill(send_data_.begin(),send_data_.end(),0);
303 
304  send_data_[0] = 0x01;
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]);
310  writeSerialCommand(_id,send_data_.data());
311 }
312 
313 
315 void SeedCommand::setSerialVersion(uint8_t _id, const char* _ver)
316 {
317  uint64_t data,hex_ver;
318  fill(send_data_.begin(),send_data_.end(),0);
319 
320  sscanf(_ver,"%lx", &data);
321  hex_ver = data;
322 
323  send_data_[0] = 0x02;
324  send_data_[1] = hex_ver >> 32;
325  send_data_[2] = hex_ver >> 24;
326  send_data_[3] = hex_ver >> 16;
327  send_data_[4] = hex_ver >> 8;
328  send_data_[5] = hex_ver;
329  writeSerialCommand(_id,send_data_.data());
330 }
331 
333 void SeedCommand::setFirmwareVersion(uint8_t _id, const char* _ver)
334 {
335  uint64_t data,hex_ver;
336  fill(send_data_.begin(),send_data_.end(),0);
337 
338  sscanf(_ver,"%lx", &data);
339  hex_ver = data;
340 
341  send_data_[0] = 0x03;
342  send_data_[1] = hex_ver >> 32;
343  send_data_[2] = hex_ver >> 24;
344  send_data_[3] = hex_ver >> 16;
345  send_data_[4] = hex_ver >> 8;
346  send_data_[5] = hex_ver;
347  writeSerialCommand(_id,send_data_.data());
348 }
349 
351 void SeedCommand::setEditorVersion(uint8_t _id, const char* _ver)
352 {
353  uint64_t data,hex_ver;
354  fill(send_data_.begin(),send_data_.end(),0);
355 
356  sscanf(_ver,"%lx", &data);
357  hex_ver = data;
358 
359  send_data_[0] = 0x04;
360  send_data_[1] = hex_ver >> 32;
361  send_data_[2] = hex_ver >> 24;
362  send_data_[3] = hex_ver >> 16;
363  send_data_[4] = hex_ver >> 8;
364  send_data_[5] = hex_ver;
365  writeSerialCommand(_id,send_data_.data());
366 }
367 
369 void SeedCommand::setMotorAdaptation(uint8_t _id, uint32_t _type, uint16_t _volt)
370 {
371  fill(send_data_.begin(),send_data_.end(),0);
372 
373  send_data_[0] = 0x05;
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;
379  writeSerialCommand(_id,send_data_.data());
380 }
381 
383 void SeedCommand::setMotorParam(uint8_t _id, uint8_t _mode, uint8_t _feedback)
384 {
385  fill(send_data_.begin(),send_data_.end(),0);
386 
387  send_data_[0] = 0x06;
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;
393  writeSerialCommand(_id,send_data_.data());
394 }
395 
397 void SeedCommand::setMotorCurrentParam(uint8_t _id, uint16_t _driver_max, uint16_t _motor_max, uint8_t _current_conversion)
398 {
399  fill(send_data_.begin(),send_data_.end(),0);
400 
401  send_data_[0] = 0x07;
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;
407  writeSerialCommand(_id,send_data_.data());
408 }
409 
411 void SeedCommand::setCurrentInstantaneous(uint8_t _id, uint16_t _max, uint16_t _time)
412 {
413  fill(send_data_.begin(),send_data_.end(),0);
414 
415  send_data_[0] = 0x08;
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;
421  writeSerialCommand(_id,send_data_.data());
422 }
423 
425 void SeedCommand::setEncoderParam(uint8_t _id, uint16_t _encoder_pulse, uint16_t _motor_pulse)
426 {
427  fill(send_data_.begin(),send_data_.end(),0);
428 
429  send_data_[0] = 0x09;
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;
435  writeSerialCommand(_id,send_data_.data());
436 }
437 
439 void SeedCommand::setDummy(uint8_t _id, uint8_t _cmd)
440 {
441  fill(send_data_.begin(),send_data_.end(),0);
442 
443  send_data_[0] = _cmd;
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;
449  writeSerialCommand(_id,send_data_.data());
450 }
451 
452 /*******************
453  Base Parameters
454 ********************/
456 void SeedCommand::setIdParam(uint8_t _id, uint8_t _re_id)
457 {
458  fill(send_data_.begin(),send_data_.end(),0);
459 
460  send_data_[0] = 0x10;
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;
466  writeSerialCommand(_id,send_data_.data());
467 }
468 
470 void SeedCommand::setEmergencyParam(uint8_t _id, uint8_t _mode, uint8_t _io_no, uint8_t _io, uint8_t _reset)
471 {
472  fill(send_data_.begin(),send_data_.end(),0);
473 
474  send_data_[0] = 0x11;
475  send_data_[1] = _mode;
476  send_data_[2] = _io_no;
477  send_data_[3] = _io;
478  send_data_[4] = _reset;
479  send_data_[5] = 0x00;
480  writeSerialCommand(_id,send_data_.data());
481 }
482 
484 void SeedCommand::setStopModeParam(uint8_t _id, uint8_t _motor, uint8_t _script)
485 {
486  fill(send_data_.begin(),send_data_.end(),0);
487 
488  send_data_[0] = 0x12;
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;
494  writeSerialCommand(_id,send_data_.data());
495 }
496 
498 void SeedCommand::setOperationParam(uint8_t _id, uint8_t _auto_run, uint8_t _script, uint8_t _point_go, uint8_t _motor, uint8_t _io)
499 {
500  fill(send_data_.begin(),send_data_.end(),0);
501 
502  send_data_[0] = 0x13;
503  send_data_[1] = _auto_run;
504  send_data_[2] = _script;
505  send_data_[3] = _point_go;
506  send_data_[4] = _motor;
507  send_data_[5] = _io;
508  writeSerialCommand(_id,send_data_.data());
509 }
510 
512 void SeedCommand::setOvertravelParam(uint8_t _id, uint8_t _mode, uint8_t _minus, uint8_t _plus, uint8_t _io)
513 {
514  fill(send_data_.begin(),send_data_.end(),0);
515 
516  send_data_[0] = 0x14;
517  send_data_[1] = _mode;
518  send_data_[2] = _minus;
519  send_data_[3] = _plus;
520  send_data_[4] = _io;
521  send_data_[5] = 0x00;
522  writeSerialCommand(_id,send_data_.data());
523 }
524 
526 void SeedCommand::setErrorMotionParam(uint8_t _id, uint8_t _temperature, uint8_t _motor, uint8_t _ot, uint8_t _voltage)
527 {
528  fill(send_data_.begin(),send_data_.end(),0);
529 
530  send_data_[0] = 0x15;
531  send_data_[1] = _temperature;
532  send_data_[2] = _motor;
533  send_data_[3] = _ot;
534  send_data_[4] = _voltage;
535  send_data_[5] = 0x00;
536  writeSerialCommand(_id,send_data_.data());
537 }
538 
540 void SeedCommand::setResponseParam(uint8_t _id, uint8_t _mode)
541 {
542  fill(send_data_.begin(),send_data_.end(),0);
543 
544  send_data_[0] = 0x16;
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;
550  writeSerialCommand(_id,send_data_.data());
551 }
552 
554 void SeedCommand::setDioParam(uint8_t _id, uint8_t _io0, uint8_t _io1, uint8_t _io2, uint8_t _io3)
555 {
556  fill(send_data_.begin(),send_data_.end(),0);
557 
558  send_data_[0] = 0x17;
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;
564  writeSerialCommand(_id,send_data_.data());
565 }
566 
568 void SeedCommand::setAdParam(uint8_t _id, uint8_t _ad0, uint8_t _ad1, uint8_t _ad2, uint8_t _ad3)
569 {
570  fill(send_data_.begin(),send_data_.end(),0);
571 
572  send_data_[0] = 0x18;
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;
578  writeSerialCommand(_id,send_data_.data());
579 }
580 
581 /*******************
582  Motor Settings
583 ********************/
585 void SeedCommand::setMotorCurrent(uint8_t _id, uint16_t _max, uint8_t _min, uint16_t _min_time)
586 {
587  fill(send_data_.begin(),send_data_.end(),0);
588  send_data_[0] = 0x20;
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;
594  writeSerialCommand(_id,send_data_.data());
595 }
596 
598 void SeedCommand::setMotorMaxSpeed(uint8_t _id, uint16_t _speed)
599 {
600  fill(send_data_.begin(),send_data_.end(),0);
601  send_data_[0] = 0x21;
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;
607  writeSerialCommand(_id,send_data_.data());
608 }
609 
611 void SeedCommand::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)
612 {
613  fill(send_data_.begin(),send_data_.end(),0);
614  send_data_[0] = 0x22;
615  send_data_[1] = _back_surge_a;
616  send_data_[2] = _back_surge_b;
617  send_data_[3] = _back_surge_c;
618  send_data_[4] = _oc;
619  send_data_[5] = _fst;
620  writeSerialCommand(_id,send_data_.data());
621 }
622 
624 void SeedCommand::setMotorControlParameter1(uint8_t _id, uint16_t _i_gain, uint8_t _d_gain, uint8_t _switch)
625 {
626  fill(send_data_.begin(),send_data_.end(),0);
627  send_data_[0] = 0x22;
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;
633  writeSerialCommand(_id,send_data_.data());
634 }
635 
637 void SeedCommand::setInPosition(uint8_t _id, uint16_t _value)
638 {
639  fill(send_data_.begin(),send_data_.end(),0);
640  send_data_[0] = 0x23;
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;
646  writeSerialCommand(_id,send_data_.data());
647 }
648 
650 void SeedCommand::setAcDecelerationRate(uint8_t _id, uint16_t _acc, uint16_t _dec)
651 {
652  fill(send_data_.begin(),send_data_.end(),0);
653  send_data_[0] = 0x24;
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;
659  writeSerialCommand(_id,send_data_.data());
660 }
661 
663 void SeedCommand::setMotorControlParameter2(uint8_t _id, uint16_t _initial_speed, uint16_t _p_gain, uint8_t _correcting_gain)
664 {
665  fill(send_data_.begin(),send_data_.end(),0);
666  send_data_[0] = 0x25;
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;
672  writeSerialCommand(_id,send_data_.data());
673 }
674 
676 void SeedCommand::setUpperSoftwareLimit(uint8_t _id, int32_t _limit)
677 {
678  fill(send_data_.begin(),send_data_.end(),0);
679  send_data_[0] = 0x26;
680  send_data_[1] = _limit >> 16;
681  send_data_[2] = _limit >> 8;
682  send_data_[3] = _limit;
683  writeSerialCommand(_id,send_data_.data());
684 }
685 
687 void SeedCommand::setLowerSoftwareLimit(uint8_t _id, int32_t _limit)
688 {
689  fill(send_data_.begin(),send_data_.end(),0);
690  send_data_[0] = 0x27;
691  send_data_[1] = _limit >> 16;
692  send_data_[2] = _limit >> 8;
693  send_data_[3] = _limit;
694  writeSerialCommand(_id,send_data_.data());
695 }
696 
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)
699 {
700  fill(send_data_.begin(),send_data_.end(),0);
701  send_data_[0] = 0x28;
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;
707  writeSerialCommand(_id,send_data_.data());
708 }
709 
711 void SeedCommand::setMotorError(uint8_t _id, uint16_t _time, uint32_t _pulse)
712 {
713  fill(send_data_.begin(),send_data_.end(),0);
714  send_data_[0] = 0x29;
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;
720  writeSerialCommand(_id,send_data_.data());
721 }
722 
724 void SeedCommand::setMotorErrorLimit(uint8_t _id, uint8_t _temerature , uint8_t _minimum_voltage, uint8_t _maximum_voltage, uint8_t _current)
725 {
726  fill(send_data_.begin(),send_data_.end(),0);
727  send_data_[0] = 0x2A;
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;
733  writeSerialCommand(_id,send_data_.data());
734 }
735 
736 /*******************
737  Script Settings
738 ********************/
740 void SeedCommand::setScriptData(uint8_t _id, uint8_t _number , uint8_t _start_line, uint8_t _end_line)
741 {
742  fill(send_data_.begin(),send_data_.end(),0);
743  send_data_[0] = 0x32;
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;
749  writeSerialCommand(_id,send_data_.data());
750 }
751 
753 void SeedCommand::writeScriptLine(uint8_t _id, uint8_t _line , const char* _command)
754 {
755  uint64_t data,hex_command;
756  fill(send_data_.begin(),send_data_.end(),0);
757 
758  sscanf(_command,"%lx", &data);
759  hex_command = data;
760 
761  send_data_[0] = hex_command >> 40;
762  send_data_[1] = hex_command >> 32;
763  send_data_[2] = hex_command >> 24;
764  send_data_[3] = hex_command >> 16;
765  send_data_[4] = hex_command >> 8;
766  send_data_[5] = hex_command;
767  writeSerialCommand(_id,_line, send_data_.data());
768 }
769 
770 /*******************
771  Other
772 ********************/
773 
775 bool SeedCommand::getLockCode(uint8_t _id, uint8_t _type)
776 {
777  fill(send_data_.begin(),send_data_.end(),0);
778  send_data_[0] = 0x40;
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;
784  writeSerialCommand(_id,send_data_.data());
785 
786  std::vector<uint8_t> receive_data;
787  std::string data = "" ;
788  if(!readSerialCommand(receive_data)) return false;
789  else{
790  lock_code_info_["status"] = std::strtol(data.c_str(),NULL,16);
791 
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);
796 
797  return true;
798  }
799 
800  //comand_mtx_.unlock();
801 }
802 
804 void SeedCommand::setReleaseLock(uint8_t _id, uint8_t _type, uint16_t _code)
805 {
806  fill(send_data_.begin(),send_data_.end(),0);
807  send_data_[0] = _type;
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;
813  writeSerialCommand(_id,send_data_.data());
814 }
815 
817 std::vector<uint8_t> SeedCommand::getConnectedId()
818 {
819  connected_id_.clear();
820 
821  for(int i=1;i<15;++i){
822  if(getOperationalInfo(i)[0]) connected_id_.push_back(i);
823  }
824  return connected_id_;
825 }
826 
828 void SeedCommand::writeRom(uint8_t _id, uint8_t _type)
829 {
830  fill(send_data_.begin(),send_data_.end(),0);
831  send_data_[0] = _type;
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;
837  writeSerialCommand(_id,send_data_.data());
838 }
839 
840 int SeedCommand::io2int(std::string _parameter)
841 {
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;
848  else return 0;
849 }
850 
851 /*******************
852  get or actuate
853 ********************/
854 
856 std::array<int, 3> SeedCommand::getPosition(uint8_t _id)
857 {
858  fill(send_data_.begin(),send_data_.end(),0);
859  send_data_[0] = 0x42;
860  send_data_[1] = _id;
861  writeSerialCommand(_id,send_data_.data());
862 
863  std::vector<uint8_t> receive_data;
864  std::string id = "";
865  std::string command = "";
866  std::string velocity = "" ;
867  std::string position = "" ;
868 
869  if(!readSerialCommand(receive_data)){
870  return {false, 0, 0};
871  }
872  else{
873  id = receive_data[5];
874  for(int i=9;i<11;++i) command += receive_data[i];
875 
876  if(str2int(command) == send_data_[0] && str2int(id) == _id){
877  for(int i=11;i<15;++i) velocity += receive_data[i];
878 
879  if(receive_data[15] == 'F') position = "FF"; //in case of negative value
880  else position = "";
881  for(int i=15;i<21;++i) position += receive_data[i];
882  return {true, str2int(velocity), str2int(position)};
883  }
884  else return {false, 0, 0};
885  }
886 }
887 
889 std::array<int, 3> SeedCommand::getCurrent(uint8_t _id)
890 {
891  fill(send_data_.begin(),send_data_.end(),0);
892  send_data_[0] = 0x43;
893  send_data_[1] = _id;
894  writeSerialCommand(_id,send_data_.data());
895 
896  std::vector<uint8_t> receive_data;
897  std::string id = "";
898  std::string command = "";
899  std::string current = "" ;
900  std::string position_error = "" ;
901 
902  if(!readSerialCommand(receive_data)){
903  return {false, 0, 0};
904  }
905  else{
906  id = receive_data[5];
907  for(int i=9;i<11;++i) command += receive_data[i];
908 
909  if(str2int(command) == send_data_[0] && str2int(id) == _id){
910  for(int i=11;i<15;++i) current += receive_data[i];
911 
912  if(receive_data[15] == 'F') position_error = "FF"; //in case of negative value
913  else position_error ="";
914  for(int i=15;i<21;++i) position_error += receive_data[i];
915 
916  return {true, str2int(current), str2int(position_error)};
917  }
918  else return {false, 0, 0};
919  }
920 }
921 
923 std::array<int, 6> SeedCommand::getOperationalInfo(uint8_t _id)
924 {
925  fill(send_data_.begin(),send_data_.end(),0);
926  send_data_[0] = 0x44;
927  send_data_[1] = _id;
928  writeSerialCommand(_id,send_data_.data());
929 
930  std::vector<uint8_t> receive_data;
931  std::string id = "";
932  std::string command = "";
933  std::string data = "" ;
934 
935  if(!readSerialCommand(receive_data)){
936  return {false, 0, 0, 0, 0, 0};
937  }
938  else{
939  id = receive_data[5];
940  for(int i=9;i<11;++i) command += receive_data[i];
941 
942  if(str2int(command) == send_data_[0] && str2int(id) == _id){
943  for(int i=11;i<20;++i) data += receive_data[i];
944  operational_info_["status"] = std::strtol(data.substr(0,2).c_str(),NULL,16);
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);
949 
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"]};
952  }
953  else return {false, 0, 0, 0, 0, 0};
954  }
955 }
956 
958 void SeedCommand::onServo(uint8_t _id, uint8_t _state)
959 {
960  fill(send_data_.begin(),send_data_.end(),0);
961  send_data_[0] = 0x50;
962  send_data_[1] = _id;
963  send_data_[2] = _state;
964  writeSerialCommand(_id,send_data_.data());
965 }
967 void SeedCommand::stopMotor(uint8_t _id)
968 {
969  fill(send_data_.begin(),send_data_.end(),0);
970  send_data_[0] = 0x51;
971  send_data_[1] = _id;
972  writeSerialCommand(_id,send_data_.data());
973 }
975 void SeedCommand::runScript(uint8_t _id, uint8_t _script_no)
976 {
977  fill(send_data_.begin(),send_data_.end(),0);
978  send_data_[0] = 0x5F;
979  send_data_[1] = _id;
980  send_data_[2] = _script_no;
981 
982  if ((_script_no > 0x00) && (_script_no < 0x0F)) writeSerialCommand(_id,send_data_.data());
983 }
984 
987 {
988  int number_of_end = 0;
989 
990  while( number_of_end < _number){
991  std::vector<uint8_t> receive_data;
992  std::string id = "";
993  std::string command = "";
994  std::string data = "" ;
995  if(!readSerialCommand(receive_data,10000)); //10[sec]
996  else{
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];
1000 
1001  if(str2int(data) == 0xFF){
1002  number_of_end += 1;
1003  std::cout << "Script of ID " << id << " is the end." << std::endl;
1004  }
1005  }
1006  }
1007 }
1008 
1010 void SeedCommand::actuateRelativePositionByTime(uint8_t _id, int16_t _time, int32_t _position)
1011 {
1012  fill(send_data_.begin(),send_data_.end(),0);
1013  send_data_[0] = 0x61;
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;
1019  writeSerialCommand(_id,send_data_.data());
1020 }
1021 
1023 void SeedCommand::actuateRelativePositionBySpeed(uint8_t _id, int16_t _speed, int32_t _position)
1024 {
1025  fill(send_data_.begin(),send_data_.end(),0);
1026  send_data_[0] = 0x63;
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;
1032  writeSerialCommand(_id,send_data_.data());
1033 }
1034 
1036 void SeedCommand::actuateAbsolutePositionByTime(uint8_t _id, uint16_t _time, int32_t _position)
1037 {
1038  fill(send_data_.begin(),send_data_.end(),0);
1039  send_data_[0] = 0x64;
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;
1045  writeSerialCommand(_id,send_data_.data());
1046 }
1047 
1049 void SeedCommand::actuateAbsolutePositionBySpeed(uint8_t _id, int16_t _speed, int32_t _position)
1050 {
1051  fill(send_data_.begin(),send_data_.end(),0);
1052  send_data_[0] = 0x66;
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;
1058  writeSerialCommand(_id,send_data_.data());
1059 }
1060 
1062 void SeedCommand::actuateContinuousRelativePosition(uint8_t _id, uint16_t _time, int32_t _position)
1063 {
1064  fill(send_data_.begin(),send_data_.end(),0);
1065  send_data_[0] = 0x67;
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;
1071  writeSerialCommand(_id,send_data_.data());
1072 }
1073 
1075 void SeedCommand::actuateContinuousAbsolutePosition(uint8_t _id, uint16_t _time, int32_t _position)
1076 {
1077  fill(send_data_.begin(),send_data_.end(),0);
1078  send_data_[0] = 0x68;
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;
1084  writeSerialCommand(_id,send_data_.data());
1085 }
1086 
1088 void SeedCommand::actuateBySpeed(uint8_t _id, int16_t _speed)
1089 {
1090  fill(send_data_.begin(),send_data_.end(),0);
1091 
1092  send_data_[0] = 0x6C;
1093  if(_speed < 0)
1094  {
1095  send_data_[1] = (-1 * _speed) >> 8;
1096  send_data_[2] = (-1 * _speed);
1097  send_data_[3] = 1;
1098  }
1099  else
1100  {
1101  send_data_[1] = _speed >> 8;
1102  send_data_[2] = _speed;
1103  send_data_[3] = 0;
1104  }
1105  writeSerialCommand(_id,send_data_.data());
1106 }
1107 
1109 void SeedCommand::setPosition(uint8_t _id, uint8_t _position, uint8_t _state)
1110 {
1111  fill(send_data_.begin(),send_data_.end(),0);
1112  send_data_[0] = 0x6F;
1113  send_data_[1] = _position;
1114  send_data_[2] = _state;
1115  writeSerialCommand(_id,send_data_.data());
1116 }
1117 
1119 
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 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)
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_
Definition: seed3_command.h:43
std::unordered_map< std::string, int32_t > operational_info_
Definition: seed3_command.h:64
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_
Definition: seed3_command.h:66
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_
Definition: seed3_command.h:65
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)


seed_smartactuator_sdk
Author(s):
autogenerated on Mon Nov 2 2020 03:39:20