kobuki.hpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10  ** Ifdefs
11  *****************************************************************************/
12 
13 #ifndef KOBUKI_HPP_
14 #define KOBUKI_HPP_
15 
16 /*****************************************************************************
17  ** Includes
18  *****************************************************************************/
19 
20 #include <string>
21 #include <iomanip>
22 #include <ecl/config.hpp>
23 #include <ecl/threads.hpp>
24 #include <ecl/devices.hpp>
25 #include <ecl/threads/mutex.hpp>
28 #include "version_info.hpp"
29 #include "parameters.hpp"
30 #include "event_manager.hpp"
31 #include "command.hpp"
32 #include "modules.hpp"
33 #include "packets.hpp"
35 #include "macros.hpp"
36 
37 /*****************************************************************************
38 ** Extern Templates
39 *****************************************************************************/
40 
41 #ifdef ECL_IS_WIN32
42  /* Help windows create common instances of sigslots across kobuki dll
43  * and end user program (otherwise it creates two separate variables!) */
49 #endif
50 
51 /*****************************************************************************
52  ** Namespaces
53  *****************************************************************************/
54 
55 namespace kobuki
56 {
57 
58 /*****************************************************************************
59  ** Definitions
60  *****************************************************************************/
61 
62 union union_sint16
63 {
64  short word;
65  unsigned char byte[2];
66 };
67 
68 /*****************************************************************************
69 ** Parent Interface
70 *****************************************************************************/
71 
72 class PacketFinder : public PacketFinderBase
73 {
74 public:
75  virtual ~PacketFinder() {}
76  bool checkSum();
77 };
78 
79 /*****************************************************************************
80  ** Interface [Kobuki]
81  *****************************************************************************/
88 {
89 public:
90  Kobuki();
91  ~Kobuki();
92 
93  /*********************
94  ** Configuration
95  **********************/
96  void init(Parameters &parameters);
97  bool isAlive() const { return is_alive; }
98  bool isShutdown() const { return shutdown_requested; }
99  bool isEnabled() const { return is_enabled; }
100  bool enable();
101  bool disable();
102  void shutdown() { shutdown_requested = true; }
104  /******************************************
105  ** Packet Processing
106  *******************************************/
107  void spin();
108  void fixPayload(ecl::PushAndPop<unsigned char> & byteStream);
109 
110  /******************************************
111  ** Getters - Data Protection
112  *******************************************/
113  void lockDataAccess();
114  void unlockDataAccess();
115 
116  /******************************************
117  ** Getters - User Friendly Api
118  *******************************************/
119  /* Be sure to lock/unlock the data access (lockDataAccess and unlockDataAccess)
120  * around any getXXX calls - see the doxygen notes for lockDataAccess. */
121  ecl::Angle<double> getHeading() const;
122  double getAngularVelocity() const;
123  VersionInfo versionInfo() const { return VersionInfo(firmware.version(), hardware.data.version, unique_device_id.data.udid0, unique_device_id.data.udid1, unique_device_id.data.udid2); }
124  Battery batteryStatus() const { return Battery(core_sensors.data.battery, core_sensors.data.charger); }
125 
126  /******************************************
127  ** Getters - Raw Data Api
128  *******************************************/
129  /* Be sure to lock/unlock the data access (lockDataAccess and unlockDataAccess)
130  * around any getXXX calls - see the doxygen notes for lockDataAccess. */
131  CoreSensors::Data getCoreSensorData() const { return core_sensors.data; }
132  DockIR::Data getDockIRData() const { return dock_ir.data; }
133  Cliff::Data getCliffData() const { return cliff.data; }
134  Current::Data getCurrentData() const { return current.data; }
135  Inertia::Data getInertiaData() const { return inertia.data; }
136  GpInput::Data getGpInputData() const { return gp_input.data; }
137  ThreeAxisGyro::Data getRawInertiaData() const { return three_axis_gyro.data; }
138  ControllerInfo::Data getControllerInfoData() const { return controller_info.data; }
139 
140  /*********************
141  ** Feedback
142  **********************/
143  void getWheelJointStates(double &wheel_left_angle, double &wheel_left_angle_rate,
144  double &wheel_right_angle, double &wheel_right_angle_rate);
145  void updateOdometry(ecl::LegacyPose2D<double> &pose_update,
146  ecl::linear_algebra::Vector3d &pose_update_rates);
147 
148  /*********************
149  ** Soft Commands
150  **********************/
151  void resetOdometry();
152 
153  /*********************
154  ** Hard Commands
155  **********************/
156  void setBaseControl(const double &linear_velocity, const double &angular_velocity);
157  void setLed(const enum LedNumber &number, const enum LedColour &colour);
158  void setDigitalOutput(const DigitalOutput &digital_output);
159  void setExternalPower(const DigitalOutput &digital_output);
160  void playSoundSequence(const enum SoundSequences &number);
161  bool setControllerGain(const unsigned char &type, const unsigned int &p_gain,
162  const unsigned int &i_gain, const unsigned int &d_gain);
163  bool getControllerGain();
164 
165  /*********************
166  ** Debugging
167  **********************/
168  void printSigSlotConnections() const;
169 
170 private:
171  /*********************
172  ** Thread
173  **********************/
174  ecl::Thread thread;
175  bool shutdown_requested; // helper to shutdown the worker thread.
176 
177  /*********************
178  ** Odometry
179  **********************/
180  DiffDrive diff_drive;
181  bool is_enabled;
182 
183  /*********************
184  ** Inertia
185  **********************/
186  double heading_offset;
187 
188  /*********************
189  ** Driver Paramters
190  **********************/
191  Parameters parameters;
192  bool is_connected;
193 
194  /*********************
195  ** Acceleration Limiter
196  **********************/
197  AccelerationLimiter acceleration_limiter;
198 
199  /*********************
200  ** Packet Handling
201  **********************/
202  CoreSensors core_sensors;
203  Inertia inertia;
204  DockIR dock_ir;
205  Cliff cliff;
206  Current current;
207  GpInput gp_input;
208  Hardware hardware; // requestable
209  Firmware firmware; // requestable
210  UniqueDeviceID unique_device_id; // requestable
211  ThreeAxisGyro three_axis_gyro;
212  ControllerInfo controller_info; // requestable
213 
214  ecl::Serial serial;
215  PacketFinder packet_finder;
216  PacketFinder::BufferType data_buffer;
217  bool is_alive; // used as a flag set by the data stream watchdog
218 
219  int version_info_reminder;
220  int controller_info_reminder;
221 
222  /*********************
223  ** Commands
224  **********************/
225  void sendBaseControlCommand();
226  void sendCommand(Command command);
227  ecl::Mutex command_mutex; // protection against the user calling the command functions from multiple threads
228  // data_mutex is protection against reading and writing data structures simultaneously as well as
229  // ensuring multiple get*** calls are synchronised to the same data update
230  // refer to https://github.com/yujinrobot/kobuki/issues/240
231  ecl::Mutex data_mutex;
232  Command kobuki_command; // used to maintain some state about the command history
233  Command::Buffer command_buffer;
234  std::vector<short> velocity_commands_debug;
235 
236  /*********************
237  ** Events
238  **********************/
239  EventManager event_manager;
240 
241  /*********************
242  ** Logging
243  **********************/
244  std::vector<std::string> log(std::string msg) { return log("", "", msg); }
245  std::vector<std::string> log(std::string level, std::string msg) { return log(level, "", msg); }
246  std::vector<std::string> log(std::string level, std::string name, std::string msg) {
247  std::vector<std::string> ret;
248  if( level != "" ) ret.push_back(level);
249  if( name != "" ) ret.push_back(name);
250  if( msg != "" ) ret.push_back(msg);
251  return ret;
252  }
253 
254  /*********************
255  ** Signals
256  **********************/
257  ecl::Signal<> sig_stream_data, sig_controller_info;
258  ecl::Signal<const VersionInfo&> sig_version_info;
259  ecl::Signal<const std::string&> sig_debug, sig_info, sig_warn, sig_error;
261  ecl::Signal<Command::Buffer&> sig_raw_data_command; // should be const, but pushnpop is not fully realised yet for const args in the formatters.
262  ecl::Signal<PacketFinder::BufferType&> sig_raw_data_stream; // should be const, but pushnpop is not fully realised yet for const args in the formatters.
263  ecl::Signal<const std::vector<short>&> sig_raw_control_command;
264 };
265 
266 } // namespace kobuki
267 
268 #endif /* KOBUKI_HPP_ */
EXP_TEMPLATE
#define EXP_TEMPLATE
Definition: macros.hpp:41
event_manager.hpp
The event manager - sigslot interface.
threads.hpp
kobuki::LedNumber
LedNumber
Definition: led_array.hpp:39
kobuki::UniqueDeviceID
Definition: unique_device_id.hpp:40
kobuki::AccelerationLimiter
An acceleration limiter for the kobuki.
Definition: acceleration_limiter.hpp:56
parameters.hpp
Parameter configuration for the kobuki.
kobuki::Hardware
Definition: hardware.hpp:40
kobuki
Definition: command.hpp:31
kobuki::DigitalOutput
Definition: digital_output.hpp:33
kobuki::LedColour
LedColour
Definition: led_array.hpp:44
kobuki::Inertia::Data
Definition: inertia.hpp:52
modules.hpp
Convenience header for modules.
packet_finder.hpp
Simple packet finder.
kobuki::PacketFinderBase
Provides simple packet finder which may be consist of stx, etx, payload, ...
Definition: packet_finder.hpp:78
ecl::LegacyPose2D< double >
ecl::SigSlotsManager
command.hpp
Command structure.
version_info.hpp
Version info for the kobuki driver.
legacy_pose2d.hpp
macros.hpp
Macros for kobuki_driver.
mutex.hpp
kobuki::Cliff
Definition: cliff.hpp:41
ecl::Signal
kobuki::EventManager
Definition: event_manager.hpp:118
kobuki::GpInput
Definition: gp_input.hpp:41
packets.hpp
Packets convenience header.
kobuki::Cliff::Data
Definition: cliff.hpp:54
kobuki::PacketFinder
Definition: kobuki.hpp:80
kobuki_PUBLIC
#define kobuki_PUBLIC
Definition: macros.hpp:39
kobuki::Parameters
Parameter list and validator for the kobuki.
Definition: parameters.hpp:42
config.hpp
kobuki::ThreeAxisGyro
Definition: three_axis_gyro.hpp:42
kobuki::CoreSensors
Definition: core_sensors.hpp:41
kobuki::Kobuki
The core kobuki driver class.
Definition: kobuki.hpp:95
kobuki::Current::Data
Definition: current.hpp:57
kobuki::DockIR
Definition: dock_ir.hpp:40
kobuki::Firmware
Definition: firmware.hpp:42
standard_exception.hpp
kobuki::Current
Definition: current.hpp:45
ecl::PushAndPop< unsigned char >
kobuki::Command
Definition: command.hpp:40
kobuki::DiffDrive
Definition: diff_drive.hpp:44
kobuki::Inertia
Definition: inertia.hpp:40
devices.hpp
ecl::Angle
kobuki::ControllerInfo
Definition: controller_info.hpp:40
kobuki::SoundSequences
SoundSequences
Definition: sound.hpp:36


kobuki_driver
Author(s): Daniel Stonier , Younghun Ju , Jorge Santos Simon
autogenerated on Wed Mar 2 2022 00:26:14