Kobuki 1.0.0
C++ API for the Kobuki research robot
Loading...
Searching...
No Matches
kobuki.hpp
Go to the documentation of this file.
1
9/*****************************************************************************
10 ** Ifdefs
11 *****************************************************************************/
12
13#ifndef KOBUKI_CORE_HPP_
14#define KOBUKI_CORE_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>
26#include <ecl/exceptions/standard_exception.hpp>
27// Version 3.4.0 of Eigen in Ubuntu 22.04 has a bug that causes -Wclass-memaccess warnings on
28// aarch64. Upstream Eigen has already fixed this in
29// https://gitlab.com/libeigen/eigen/-/merge_requests/645 . The Debian fix for this is in
30// https://salsa.debian.org/science-team/eigen3/-/merge_requests/1 .
31// However, it is not clear that that fix is going to make it into Ubuntu 22.04 before it
32// freezes, so disable the warning here.
33#if defined(__GNUC__) && !defined(__clang__)
34#pragma GCC diagnostic push
35#pragma GCC diagnostic ignored "-Wclass-memaccess"
36#endif
37#include <ecl/geometry.hpp>
38#if defined(__GNUC__) && !defined(__clang__)
39#pragma GCC diagnostic pop
40#endif
41
42#include "version_info.hpp"
43
44#include "logging.hpp"
45#include "parameters.hpp"
46#include "event_manager.hpp"
47#include "command.hpp"
48#include "modules.hpp"
49#include "packets.hpp"
51#include "macros.hpp"
52
53/*****************************************************************************
54** Extern Templates
55*****************************************************************************/
56
57#ifdef ECL_IS_WIN32
58 /* Help windows create common instances of sigslots across kobuki dll
59 * and end user program (otherwise it creates two separate variables!) */
60 EXP_TEMPLATE template class kobuki_PUBLIC ecl::SigSlotsManager<>;
61 EXP_TEMPLATE template class kobuki_PUBLIC ecl::SigSlotsManager<const kobuki::VersionInfo&>;
62 EXP_TEMPLATE template class kobuki_PUBLIC ecl::SigSlotsManager<const std::string&>;
63 EXP_TEMPLATE template class kobuki_PUBLIC ecl::SigSlotsManager<kobuki::Command::Buffer&>;
64 EXP_TEMPLATE template class kobuki_PUBLIC ecl::SigSlotsManager<kobuki::PacketFinderBase::BufferType&>;
65#endif
66
67/*****************************************************************************
68 ** Namespaces
69 *****************************************************************************/
70
71namespace kobuki
72{
73/*****************************************************************************
74 ** Definitions
75 *****************************************************************************/
76
78{
79 short word;
80 unsigned char byte[2];
81};
82
83/*****************************************************************************
84** Parent Interface
85*****************************************************************************/
86
88{
89public:
90 virtual ~PacketFinder() {}
91 bool checkSum();
92};
93
94/*****************************************************************************
95 ** Interface [Kobuki]
96 *****************************************************************************/
102class kobuki_PUBLIC Kobuki
103{
104public:
105 /*********************
106 ** C&D
107 **********************/
108 Kobuki();
109 ~Kobuki();
110
111 /*********************
112 ** Configuration
113 **********************/
114 void init(Parameters &parameters);
115 bool isAlive() const { return is_alive; }
116 bool isShutdown() const { return shutdown_requested; }
117 bool isEnabled() const { return is_enabled; }
118 bool enable();
119 bool disable();
120 void shutdown() { shutdown_requested = true; }
122 /******************************************
123 ** Packet Processing
124 *******************************************/
125 void spin();
126 void fixPayload(ecl::PushAndPop<unsigned char> & byteStream);
127
128 /******************************************
129 ** Getters - Data Protection
130 *******************************************/
131 void lockDataAccess();
132 void unlockDataAccess();
133
134 /******************************************
135 ** Getters - User Friendly Api
136 *******************************************/
137 /* Be sure to lock/unlock the data access (lockDataAccess and unlockDataAccess)
138 * around any getXXX calls - see the doxygen notes for lockDataAccess. */
139 ecl::Angle<double> getHeading() const;
140 double getAngularVelocity() const;
141 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); }
142 Battery batteryStatus() const { return Battery(core_sensors.data.battery, core_sensors.data.charger); }
143
144 /******************************************
145 ** Getters - Raw Data Api
146 *******************************************/
147 /* Be sure to lock/unlock the data access (lockDataAccess and unlockDataAccess)
148 * around any getXXX calls - see the doxygen notes for lockDataAccess. */
149 CoreSensors::Data getCoreSensorData() const { return core_sensors.data; }
150 DockIR::Data getDockIRData() const { return dock_ir.data; }
151 Cliff::Data getCliffData() const { return cliff.data; }
152 Current::Data getCurrentData() const { return current.data; }
153 Inertia::Data getInertiaData() const { return inertia.data; }
154 GpInput::Data getGpInputData() const { return gp_input.data; }
155 ThreeAxisGyro::Data getRawInertiaData() const { return three_axis_gyro.data; }
156 ControllerInfo::Data getControllerInfoData() const { return controller_info.data; }
157
158 /*********************
159 ** Feedback
160 **********************/
161 void getWheelJointStates(double &wheel_left_angle, double &wheel_left_angle_rate,
162 double &wheel_right_angle, double &wheel_right_angle_rate);
163 void updateOdometry(ecl::linear_algebra::Vector3d &pose_update,
164 ecl::linear_algebra::Vector3d &pose_update_rates);
165
166 /*********************
167 ** Soft Commands
168 **********************/
169 void resetOdometry();
170
171 /*********************
172 ** Hard Commands
173 **********************/
174 void setBaseControl(const double &linear_velocity, const double &angular_velocity);
175 void setLed(const enum LedNumber &number, const enum LedColour &colour);
176 void setDigitalOutput(const DigitalOutput &digital_output);
177 void setExternalPower(const DigitalOutput &digital_output);
178 void playSoundSequence(const enum SoundSequences &number);
179 bool setControllerGain(const unsigned char &type, const unsigned int &p_gain,
180 const unsigned int &i_gain, const unsigned int &d_gain);
181 bool getControllerGain();
182
183 /*********************
184 ** Debugging
185 **********************/
186 void printSigSlotConnections() const;
187
188private:
189 /*********************
190 ** Thread
191 **********************/
192 ecl::Thread thread;
193 bool shutdown_requested; // helper to shutdown the worker thread.
194
195 /*********************
196 ** Odometry
197 **********************/
198 DiffDrive diff_drive;
199 bool is_enabled;
200
201 /*********************
202 ** Inertia
203 **********************/
204 double heading_offset;
205
206 /*********************
207 ** Driver Paramters
208 **********************/
209 Parameters parameters;
210 bool is_connected;
211
212 /*********************
213 ** Acceleration Limiter
214 **********************/
215 AccelerationLimiter acceleration_limiter;
216
217 /*********************
218 ** Packet Handling
219 **********************/
220 CoreSensors core_sensors;
221 Inertia inertia;
222 DockIR dock_ir;
223 Cliff cliff;
224 Current current;
225 GpInput gp_input;
226 Hardware hardware; // requestable
227 Firmware firmware; // requestable
228 UniqueDeviceID unique_device_id; // requestable
229 ThreeAxisGyro three_axis_gyro;
230 ControllerInfo controller_info; // requestable
231
232 ecl::Serial serial;
233 PacketFinder packet_finder;
234 PacketFinder::BufferType data_buffer;
235 bool is_alive; // used as a flag set by the data stream watchdog
236
237 int version_info_reminder;
238 int controller_info_reminder;
239
240 /*********************
241 ** Commands
242 **********************/
243 void sendBaseControlCommand();
244 void sendCommand(Command command);
245 ecl::Mutex command_mutex; // protection against the user calling the command functions from multiple threads
246 // data_mutex is protection against reading and writing data structures simultaneously as well as
247 // ensuring multiple get*** calls are synchronised to the same data update
248 // refer to https://github.com/yujinrobot/kobuki/issues/240
249 ecl::Mutex data_mutex;
250 Command kobuki_command; // used to maintain some state about the command history
251 Command::Buffer command_buffer;
252 std::vector<short> velocity_commands_debug;
253
254 /*********************
255 ** Events
256 **********************/
257 EventManager event_manager;
258
259 /*********************
260 ** Logging
261 **********************/
262 std::vector<std::string> log(std::string msg) { return log("", "", msg); }
263 std::vector<std::string> log(std::string level, std::string msg) { return log(level, "", msg); }
264 std::vector<std::string> log(std::string level, std::string name, std::string msg) {
265 std::vector<std::string> ret;
266 if( level != "" ) ret.push_back(level);
267 if( name != "" ) ret.push_back(name);
268 if( msg != "" ) ret.push_back(msg);
269 return ret;
270 }
271
272 /*********************
273 ** Signals
274 **********************/
275 ecl::Signal<> sig_stream_data, sig_controller_info;
276 ecl::Signal<const VersionInfo&> sig_version_info;
277 ecl::Signal<const std::string&> sig_debug, sig_info, sig_warn, sig_error;
278 ecl::Signal<Command::Buffer&> sig_raw_data_command; // should be const, but pushnpop is not fully realised yet for const args in the formatters.
279 ecl::Signal<PacketFinder::BufferType&> sig_raw_data_stream; // should be const, but pushnpop is not fully realised yet for const args in the formatters.
280 ecl::Signal<const std::vector<short>&> sig_raw_control_command;
281
282 /*********************
283 ** Slots
284 **********************/
285 ecl::Slot<const std::string&> slot_log_debug, slot_log_info, slot_log_warning, slot_log_error;
286
287};
288
289} // namespace kobuki
290
291#endif /* KOBUKI_CORE_HPP_ */
The core kobuki driver class.
Definition kobuki.hpp:103
bool isShutdown() const
Definition kobuki.hpp:116
bool isEnabled() const
Definition kobuki.hpp:117
void shutdown()
Definition kobuki.hpp:120
bool isAlive() const
Definition kobuki.hpp:115
Provides simple packet finder which may be consist of stx, etx, payload, ...
Definition packet_finder.hpp:73
Definition kobuki.hpp:88
Parameter list and validator for the kobuki.
Definition parameters.hpp:39
Definition version_info.hpp:40
Command structure.
The event manager - sigslot interface.
Log levels and simple logging to screen.
Macros for kobuki_core.
Convenience header for modules.
Simple packet finder.
Packets convenience header.
Parameter configuration for the kobuki.
Definition kobuki.hpp:78
Version info for the kobuki driver.