RokubiminiSerialImpl.hpp
Go to the documentation of this file.
1 /*
2  * RokubiminiSerialImpl.hpp
3  * Created on: April, 2020
4  * Author(s): Mike Karamousadakis, Ilias Patsiaouras
5  *
6  * and is based on:
7  *
8  * ForceTorqueSensor.hpp
9  * Created on: Dec 19, 2016
10  * Author(s): Christian Gehring, Vassilios Tsounis, Klajd Lika
11  */
12 
13 #pragma once
14 
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <termios.h>
19 #include <boost/thread.hpp>
20 #include <chrono>
21 #include <string>
22 #include <iostream>
23 #include <fstream>
24 #include <string>
25 
27 // #include "rokubimini_serial/RokubiminiSerialImplOptions.hpp"
29 
30 #include <rokubimini/Reading.hpp>
36 
37 namespace rokubimini
38 {
39 namespace serial
40 {
50 {
51  struct __attribute__((__packed__))
52  {
53  uint16_t app_took_too_long : 1;
54  uint16_t overrange : 1;
55  uint16_t invalid_measurements : 1; // saturation, short, open circuit
56  uint16_t raw_measurements : 1; // gages instead of forces
57  uint16_t : 12; // reserved
58  };
59  uint16_t byte;
60 };
61 
70 union AppOutput
71 {
72  struct __attribute__((__packed__))
73  {
74  DataStatus status;
75  float forces[6];
76  uint32_t timestamp;
77  float temperature;
78  // float volts;
79  };
80  uint8_t bytes[1];
81 };
82 
91 union RxFrame
92 {
93  struct __attribute__((__packed__))
94  {
95  uint8_t header;
96  AppOutput data;
97  uint16_t crc16_ccitt;
98  };
99  uint8_t bytes[1];
100 };
101 
114 {
115 public:
124  RokubiminiSerialImpl() = delete;
125 
137  RokubiminiSerialImpl(const std::string& name, const std::string& port, const std::uint32_t& baudRate);
138 
139  ~RokubiminiSerialImpl() = default;
140 
150  bool init();
151 
162  bool startup();
163 
173  void updateRead(){};
174 
184  void updateWrite(){};
185 
195  void shutdown();
196 
208  void setState(const uint16_t state){};
209 
220  bool waitForState(const uint16_t state)
221  {
222  return false;
223  }
224 
234  std::string getName() const
235  {
236  return name_;
237  }
238 
239  // Methods for all the SDOs
240 
252  bool getSerialNumber(unsigned int& serialNumber)
253  {
254  return false;
255  }
256 
269  bool getForceTorqueSamplingRate(int& samplingRate)
270  {
271  return false;
272  }
273 
286  bool setForceTorqueFilter(const configuration::ForceTorqueFilter& filter);
287 
299  bool setAccelerationFilter(const unsigned int filter)
300  {
301  return false;
302  }
303 
315  bool setAngularRateFilter(const unsigned int filter)
316  {
317  return false;
318  }
319 
331  bool setAccelerationRange(const unsigned int range)
332  {
333  return false;
334  }
335 
347  bool setAngularRateRange(const unsigned int range)
348  {
349  return false;
350  }
351 
363  bool setForceTorqueOffset(const Eigen::Matrix<double, 6, 1>& forceTorqueOffset);
364 
376  bool setSensorConfiguration(const configuration::SensorConfiguration& sensorConfiguration);
377 
389  bool setSensorCalibration(const calibration::SensorCalibration& sensorCalibration);
390 
400  void getReading(rokubimini::Reading& reading);
401 
409  bool setConfigMode();
410 
418  bool setRunMode();
419 
428  bool setHardwareReset();
429 
438  bool setInitMode();
439 
452  bool setCommunicationSetup(const configuration::SensorConfiguration& sensorConfiguration, const uint8_t& dataFormat,
453  const uint32_t& baudRate);
463  bool saveConfigParameter();
464 
472  bool loadConfig();
473 
481  bool printUserConfig();
482 
490  bool firmwareUpdate(const std::string& filePath);
491 
498  bool isRunning()
499  {
500  return isRunning_;
501  }
502 
509  void setPollingTimeStep(double timeStep)
510  {
511  pollingThreadTimeStep_ = timeStep;
512  }
513 
521  {
522  return frameSync_;
523  }
524 
525 private:
536  bool startPollingThread();
537 
547  bool writeSerial(const std::string& str);
548 
560  bool sendCalibrationMatrixEntry(const uint8_t subId, const double entry)
561  {
562  return false;
563  }
564 
565  using timespec = struct timespec;
566 
577  inline double diffSec(timespec a, timespec b)
578  {
579  return (static_cast<double>(b.tv_sec - a.tv_sec) + static_cast<double>(b.tv_nsec - a.tv_nsec) / NSEC_PER_SEC);
580  }
581 
591  inline double timespecToSec(timespec t)
592  {
593  return (static_cast<double>(t.tv_sec) + static_cast<double>(t.tv_nsec) / NSEC_PER_SEC);
594  }
595 
607  {
608  timespec result;
609  if (static_cast<uint64_t>(a.tv_nsec + b.tv_nsec) >= NSEC_PER_SEC)
610  {
611  result.tv_sec = a.tv_sec + b.tv_sec + 1;
612  result.tv_nsec = a.tv_nsec + b.tv_nsec - NSEC_PER_SEC;
613  }
614  else
615  {
616  result.tv_sec = a.tv_sec + b.tv_sec;
617  result.tv_nsec = a.tv_nsec + b.tv_nsec;
618  }
619  return result;
620  }
621 
632  bool connect();
633 
645  bool connect(const std::string& port);
646 
658  bool readDevice(RxFrame& frame);
660 
670  bool isConnected() const;
672 
682  bool isConnecting() const;
683 
693  bool hasError() const;
694 
704  bool isInConfigMode() const;
705 
707 
717  ConnectionState getConnectionState() const;
718 
728  ErrorState getErrorState() const;
729 
739  std::string getErrorString() const;
740 
742 
755  bool initSensorCommunication(bool keepConnecting);
756 
767  std::uint32_t getBaudRateDefinition(const uint32_t& baudRate);
768 
781  bool initSerialPort(const std::string& port);
782 
794  uint16_t calcCrc16X25(uint8_t* data, int len);
795 
807  uint16_t crcCcittUpdate(uint16_t crc, uint8_t data);
808 
815  void connectionWorker();
817 
824  void pollingWorker();
826 
827  /************ rokubimini_interface methods **************/
828 
836  std::string name_;
837 
848 
856  std::string port_;
857 
865  std::uint32_t baudRate_;
866 
874  mutable std::recursive_mutex readingMutex_;
875 
883  mutable std::recursive_mutex serialMutex_;
884 
885  /************ rokubimini_interface variables **************/
886  /*
887  * Device USB communication inteface
888  */
889  // int deviceId_;
890  // boost::atomic<int> productId_;
891 
899  boost::atomic<int> usbFileDescriptor_;
900 
908  boost::atomic<bool> frameSync_;
909 
917  std::ifstream usbStreamIn_;
918 
926  std::ofstream usbStreamOut_;
927 
935  uint8_t frameHeader = 0xAA;
936 
946 
947  /*
948  * Device measurements and data buffers
949  */
950 
958 
967 
977 
987 
995  boost::thread connectionThread_;
996 
1004  boost::thread pollingThread_;
1005 
1013  boost::atomic<ConnectionState> connectionState_;
1014 
1021  boost::atomic<ErrorState> errorState_;
1022 
1030  boost::atomic<ModeState> modeState_;
1037  boost::atomic<bool> isRunning_;
1039  // boost::atomic<bool> isReceivingOffset_;
1040 
1047  unsigned long pollingSyncErrorCounter_;
1049 
1056  unsigned long frameReceivedCounter_;
1057 
1065  unsigned long frameSuccessCounter_;
1066 
1074  unsigned long frameCrcErrorCounter_;
1075 
1083 
1097  const static uint64_t NSEC_PER_SEC = 1000000000;
1098 };
1099 
1100 using RokubiminiSerialImplPtr = std::shared_ptr<RokubiminiSerialImpl>;
1101 
1102 } // namespace serial
1103 } // namespace rokubimini
bool setAccelerationRange(const unsigned int range)
Sets an acceleration range to the device.
boost::atomic< bool > frameSync_
Flag that indicates if the frame is synced.
void setPollingTimeStep(double timeStep)
Sets the time step of the polling thread in seconds. timeStep The time step in seconds.
The Rokubimini Serial Implementation class.
void updateWrite()
This method is called by the BusManager. Each device attached to the bus writes its data to the buffe...
std::ifstream usbStreamIn_
Input stream for the USB file descriptor.
void updateRead()
This method is called by the BusManager. Each device attached to this bus reads its data from the buf...
boost::atomic< ModeState > modeState_
Mode state of the sensor.
bool setAngularRateRange(const unsigned int range)
Sets an angular rate range to the device.
double timespecToSec(timespec t)
Converts a timespec to seconds.
void init(const M_string &remappings)
unsigned long frameCrcErrorCounter_
Counter for frames with CRC errors.
unsigned long frameReceivedCounter_
Received frame counter.
bool setAccelerationFilter(const unsigned int filter)
Sets an acceleration filter to the device.
void setState(const uint16_t state)
Sets the state of the device in the bus (unused).
std_msgs::Header * header(M &m)
double pollingThreadTimeStep_
If setup, the sensor polling thread will poll at this rate.
std::string getName() const
Accessor for device name.
std::recursive_mutex readingMutex_
Mutex prohibiting simultaneous access the internal Reading variable.
unsigned long frameSuccessCounter_
Correct frames counter.
Reading serialImplReading_
The internal reading variable. It&#39;s used for providing to client code the sensor readings, through the getReading () function.
bool useDeviceTimeStamps_
Flag to indicate whether time-stamps should be generated using sensor or system time.
std::string port_
The serial port to connect to.
bool isRunning()
Returns if the serial driver is running.
bool setAngularRateFilter(const unsigned int filter)
Sets an angular rate filter to the device.
unsigned int frameSyncErrorCounter_
Frame sync errors.
timespec timespecAdd(timespec a, timespec b)
Calculates the sum of two timespecs.
bool getForceTorqueSamplingRate(int &samplingRate)
Gets the force torque sampling rate of the device.
bool sendCalibrationMatrixEntry(const uint8_t subId, const double entry)
Sends a calibration matrix entry to device.
The status of the sensor data.
RxFrame frame_
The internal variable for the receiving frame. This variable represents the raw data received from th...
std::ofstream usbStreamOut_
Output stream for the USB file descriptor.
std::shared_ptr< RokubiminiSerialImpl > RokubiminiSerialImplPtr
bool waitForState(const uint16_t state)
Wait for a state to be reached (unused).
bool runInThreadedMode_
Flag to indicate whether the driver should setup worker threads at startup.
The frame transmitted and received via the serial bus.
unsigned int maxFrameSyncErrorCounts_
Maximum acceptable frame sync errors.
bool getSerialNumber(unsigned int &serialNumber)
Accessor for device serial number.
ROSCONSOLE_DECL void shutdown()
boost::atomic< ConnectionState > connectionState_
Internal connection state.
bool hasFrameSync()
Returns if there is frame synchronization with the device.
boost::atomic< ErrorState > errorState_
Internal error state.
std::recursive_mutex serialMutex_
Mutex prohibiting simultaneous access to Serial device.
The main output from the sensors in the device.
double diffSec(timespec a, timespec b)
Calculates the difference (b-a) of two timespecs in seconds.
boost::thread connectionThread_
Connection thread handle.
boost::atomic< int > usbFileDescriptor_
The USB file descriptor.
std::uint32_t baudRate_
The baud rate of the serial communication.
System dependencies.
boost::thread pollingThread_
Polling thread handle.


rokubimini_serial
Author(s):
autogenerated on Wed Mar 3 2021 03:09:18