bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
diff_drive_robot.h
Go to the documentation of this file.
1#ifndef BITSIM_RIGID_BODIES_CHRONO_ROBOTS_DIFF_DRIVE_ROBOT_H
2#define BITSIM_RIGID_BODIES_CHRONO_ROBOTS_DIFF_DRIVE_ROBOT_H
3
4#include "bitsim/bitsim_config.h"
5
6#ifdef BITSIM_CHRONO
7
10#include "bitsim/extern/nlohmann/json/json.hpp"
19
20#include <chrono/functions/ChFunctionConst.h>
21#include <chrono/functions/ChFunctionSetpoint.h>
22#include <chrono/physics/ChBodyEasy.h>
23#include <chrono/physics/ChLinkLock.h>
24#include <chrono/physics/ChLinkMotorRotation.h>
25#include <chrono/physics/ChSystemNSC.h>
26
27#include <array>
28#include <filesystem>
29#include <memory>
30
31namespace bitsim
32{
33namespace rb::bitsim_chrono
34{
35
52{
53 public:
54 using json = nlohmann::json;
55
60 {
61 LEFT = 0,
63 };
64
68 struct RobotData
69 {
76
77 template <typename Archive> void read(const Archive &arch);
78 };
79
84 {
85
86 public:
91
106 template <typename Archive>
107 void read(const Archive &arch, std::shared_ptr<chrono::ChBody> body);
108
111 uint_t n_sensors() const noexcept { return sensor_manager_.n_sensors(); }
112
117 void add(std::shared_ptr<sensors::SensorBase> sensor) { sensor_manager_.add(sensor); }
118
122 std::shared_ptr<sensors::SensorBase> get(const std::string &name) const;
123
127 std::shared_ptr<sensors::UltrasonicSensor> front_sonar() const noexcept
128 {
129 return front_sonar_;
130 }
131
135 std::shared_ptr<sensors::UltrasonicSensor> rear_sonar() const noexcept
136 {
137 return rear_sonar_;
138 }
139
143 std::shared_ptr<sensors::InfraredSensor> line_sensor() const noexcept
144 {
145 return line_sensor_;
146 }
147
151 std::shared_ptr<sensors::IMUSensor> imu() const noexcept { return imu_; }
152
153 private:
154 bitsim::sensors::SensorManager sensor_manager_;
155 std::shared_ptr<sensors::UltrasonicSensor> front_sonar_;
156 std::shared_ptr<sensors::UltrasonicSensor> rear_sonar_;
157 std::shared_ptr<sensors::InfraredSensor> line_sensor_;
158 std::shared_ptr<sensors::IMUSensor> imu_;
159 };
160
165
169 void load(const std::filesystem::path &filename);
170
174 void load(const json &j);
175
179 uint_t n_wheel_motors() const noexcept { return 2; }
180
185 void set_motor(uint_t m, std::shared_ptr<actuators::DCMotor> motor);
186
190 std::shared_ptr<actuators::DCMotor> get_motor(uint_t m) const;
191
197 void set_motor_speed(uint_t m, real_t speed);
198
202 void set_motor_speed(real_t speed);
203
207 void set_speed(real_t speed);
208
213 void set_velocity(real_t v, real_t omega);
214
219 void update(real_t dt);
220
224 real_t wheel_angle(uint_t m) const;
225
229 real_t wheel_torque(uint_t m) const;
230
234 const CHRONO_RobotPose &get_pose() const { return pose_; }
235
239 std::shared_ptr<chrono::ChBody> get_chassis() const { return chassis_; }
240
244 std::shared_ptr<chrono::ChBody> get_wheel(uint_t m) const;
245
250 chrono::ChVector3d axle_center() const;
251
256 real_t heading() const;
257
266 void set_world_pose(real_t x, real_t y, real_t yaw);
267
272 std::shared_ptr<sensors::UltrasonicSensor> get_sonar() const noexcept
273 {
274 return sensor_manager_.front_sonar();
275 }
276
281 std::shared_ptr<sensors::UltrasonicSensor> get_rear_sonar() const noexcept
282 {
283 return sensor_manager_.rear_sonar();
284 }
285
290 std::shared_ptr<sensors::InfraredSensor> get_line_sensor() const noexcept
291 {
292 return sensor_manager_.line_sensor();
293 }
294
299 std::shared_ptr<sensors::IMUSensor> get_imu() const noexcept { return sensor_manager_.imu(); }
300
307
308#ifdef BITSIM_OPENCV
313 std::shared_ptr<sensors::DownwardCamera> get_camera() const noexcept { return camera_; }
314
319 cv::Mat camera_view(const sensors::FloorImage &floor) const;
320#endif
321
325 const RobotData &get_data() const noexcept { return data_; }
326
330 void add_to_sys(chrono::ChSystemNSC &sys) const;
331
335 RobotSensorManager &get_sensor_manager() { return sensor_manager_; }
336
341 void set_world_position(const chrono::ChVector3d &position);
342
347 void set_world_position(const std::array<real_t, 3> &position);
348
349 private:
350 void check_loaded_() const;
351 void check_motor_index_(uint_t m) const;
352 void build_wheel_link_(uint_t m);
353
355 std::shared_ptr<chrono::ChBodyEasyBox> chassis_;
356 std::array<std::shared_ptr<chrono::ChBodyEasyCylinder>, 2> wheels_;
357 std::array<chrono::ChVector3d, 2> wheel_positions_;
358 std::shared_ptr<chrono::ChBodyEasySphere> caster_wheel_;
359 std::shared_ptr<chrono::ChLinkLockSpherical> caster_link_joint_;
360
362 std::array<std::shared_ptr<actuators::DCMotor>, 2> motors_;
363 std::array<std::shared_ptr<chrono::ChLinkMotorRotation>, 2> wheel_links_;
364 std::array<std::shared_ptr<chrono::ChFunctionConst>, 2> wheel_torques_;
365 std::array<std::shared_ptr<chrono::ChFunctionSetpoint>, 2> wheel_angles_;
366 std::array<real_t, 2> angle_offsets_{0.0, 0.0};
367
369 CHRONO_RobotPose pose_;
370 RobotData data_;
371 RobotSensorManager sensor_manager_;
372
373#ifdef BITSIM_OPENCV
374 std::shared_ptr<sensors::DownwardCamera> camera_;
375#endif
376
377 bool is_loaded_{false};
378};
379} // namespace rb::bitsim_chrono
380} // namespace bitsim
381
382#endif
383
384#endif // BITSIM_RIGID_BODIES_CHRONO_ROBOTS_DIFF_DRIVE_ROBOT_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
std::shared_ptr< sensors::SensorBase > get(const std::string &name) const
Definition diff_drive_robot.cpp:227
std::shared_ptr< sensors::IMUSensor > imu() const noexcept
Definition diff_drive_robot.h:151
uint_t n_sensors() const noexcept
Definition diff_drive_robot.h:111
std::shared_ptr< sensors::InfraredSensor > line_sensor() const noexcept
Definition diff_drive_robot.h:143
std::shared_ptr< sensors::UltrasonicSensor > rear_sonar() const noexcept
Definition diff_drive_robot.h:135
void add(std::shared_ptr< sensors::SensorBase > sensor)
Add a new sensor to manage.
Definition diff_drive_robot.h:117
void read(const Archive &arch, std::shared_ptr< chrono::ChBody > body)
std::shared_ptr< sensors::UltrasonicSensor > front_sonar() const noexcept
Definition diff_drive_robot.h:127
void set_speed(real_t speed)
Drive straight at the given speed [m/s].
Definition diff_drive_robot.cpp:395
std::shared_ptr< chrono::ChBody > get_wheel(uint_t m) const
Definition diff_drive_robot.cpp:432
void set_velocity(real_t v, real_t omega)
Drive with a forward speed v [m/s] and a turning rate omega [rad/s], positive to the left.
Definition diff_drive_robot.cpp:397
Wheel
The wheels, in the order of the motors.
Definition diff_drive_robot.h:60
@ LEFT
Definition diff_drive_robot.h:61
@ RIGHT
Definition diff_drive_robot.h:62
RobotSensorManager & get_sensor_manager()
Definition diff_drive_robot.h:335
void add_to_sys(chrono::ChSystemNSC &sys) const
Definition diff_drive_robot.cpp:348
std::shared_ptr< actuators::DCMotor > get_motor(uint_t m) const
Definition diff_drive_robot.cpp:375
void set_world_pose(real_t x, real_t y, real_t yaw)
Move the whole robot so that its axle centre is at (x, y) and it faces yaw. Its height does not chang...
Definition diff_drive_robot.cpp:466
void load(const std::filesystem::path &filename)
Load the robot details form the specified filename.
Definition diff_drive_robot.cpp:239
std::shared_ptr< chrono::ChBody > get_chassis() const
Definition diff_drive_robot.h:239
uint_t n_wheel_motors() const noexcept
Definition diff_drive_robot.h:179
std::shared_ptr< sensors::UltrasonicSensor > get_rear_sonar() const noexcept
Definition diff_drive_robot.h:281
void set_motor_speed(uint_t m, real_t speed)
Set the target speed of the m-th wheel [rad/s] m=0 => left motor m=1 => right motor.
Definition diff_drive_robot.cpp:382
std::shared_ptr< sensors::DownwardCamera > get_camera() const noexcept
Definition diff_drive_robot.h:313
const RobotData & get_data() const noexcept
Definition diff_drive_robot.h:325
std::shared_ptr< sensors::InfraredSensor > get_line_sensor() const noexcept
Definition diff_drive_robot.h:290
real_t wheel_angle(uint_t m) const
Definition diff_drive_robot.cpp:445
cv::Mat camera_view(const sensors::FloorImage &floor) const
What the robot's camera sees of the given floor where the robot is now.
Definition diff_drive_robot.cpp:507
chrono::ChVector3d axle_center() const
Definition diff_drive_robot.cpp:439
const CHRONO_RobotPose & get_pose() const
Definition diff_drive_robot.h:234
void update(real_t dt)
Advance the motors by dt and pass their output to the wheels. Call it before every DoStepDynamics(dt)...
Definition diff_drive_robot.cpp:407
real_t heading() const
Definition diff_drive_robot.cpp:459
std::shared_ptr< sensors::UltrasonicSensor > get_sonar() const noexcept
Definition diff_drive_robot.h:272
std::shared_ptr< sensors::IMUSensor > get_imu() const noexcept
Definition diff_drive_robot.h:299
void set_motor(uint_t m, std::shared_ptr< actuators::DCMotor > motor)
Replace the motor of a wheel. The wheel is reconnected to the chassis to match the backend of the mot...
Definition diff_drive_robot.cpp:361
void set_world_position(const chrono::ChVector3d &position)
Explicitly set the world position of the chassis. It moves the chassis only, not the wheels; use set_...
Definition diff_drive_robot.cpp:520
nlohmann::json json
Definition diff_drive_robot.h:54
real_t wheel_torque(uint_t m) const
Definition diff_drive_robot.cpp:452
void set_floor(sensors::backends::CHRONO_InfraredBackend::Floor floor)
Set the floor that a simulated line-sensor looks at.
Definition diff_drive_robot.cpp:488
Wrapper for representing the state of a CHRONO_Robot.
Definition chrono_robot_pose.h:19
An image of the floor, centred on the world origin.
Definition downward_camera.h:31
Class for managing sensor updates.
Definition sensor_manager.h:22
std::function< real_t(real_t x, real_t y)> Floor
The reflectance of the floor at a point of the world, in [0, 1].
Definition chrono_infrared_backend.h:38
Definition chrono_robot_pose.h:11
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
real_t wheel_radius
[m]
Definition diff_drive_robot.h:71
real_t caster_wheel_radius
[m]
Definition diff_drive_robot.h:75
real_t wheel_width
[m]
Definition diff_drive_robot.h:72
real_t density
[kg/m^3]
Definition diff_drive_robot.h:74
real_t axle_length
the distance between the wheels [m]
Definition diff_drive_robot.h:73
real_t max_speed
the largest speed of a wheel on the ground [m/s]
Definition diff_drive_robot.h:70