1#ifndef BITSIM_RIGID_BODIES_CHRONO_ROBOTS_DIFF_DRIVE_ROBOT_H
2#define BITSIM_RIGID_BODIES_CHRONO_ROBOTS_DIFF_DRIVE_ROBOT_H
4#include "bitsim/bitsim_config.h"
10#include "bitsim/extern/nlohmann/json/json.hpp"
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>
54 using json = nlohmann::json;
77 template <
typename Archive>
void read(
const Archive &arch);
106 template <
typename Archive>
107 void read(
const Archive &arch, std::shared_ptr<chrono::ChBody> body);
117 void add(std::shared_ptr<sensors::SensorBase> sensor) { sensor_manager_.add(sensor); }
122 std::shared_ptr<sensors::SensorBase>
get(
const std::string &name)
const;
127 std::shared_ptr<sensors::UltrasonicSensor>
front_sonar() const noexcept
135 std::shared_ptr<sensors::UltrasonicSensor>
rear_sonar() const noexcept
143 std::shared_ptr<sensors::InfraredSensor>
line_sensor() const noexcept
151 std::shared_ptr<sensors::IMUSensor>
imu() const noexcept {
return imu_; }
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_;
169 void load(
const std::filesystem::path &filename);
239 std::shared_ptr<chrono::ChBody>
get_chassis()
const {
return chassis_; }
272 std::shared_ptr<sensors::UltrasonicSensor>
get_sonar() const noexcept
274 return sensor_manager_.front_sonar();
283 return sensor_manager_.rear_sonar();
292 return sensor_manager_.line_sensor();
299 std::shared_ptr<sensors::IMUSensor>
get_imu() const noexcept {
return sensor_manager_.imu(); }
313 std::shared_ptr<sensors::DownwardCamera>
get_camera() const noexcept {
return camera_; }
330 void add_to_sys(chrono::ChSystemNSC &sys)
const;
350 void check_loaded_()
const;
351 void check_motor_index_(
uint_t m)
const;
352 void build_wheel_link_(
uint_t m);
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_;
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};
369 CHRONO_RobotPose pose_;
371 RobotSensorManager sensor_manager_;
374 std::shared_ptr<sensors::DownwardCamera> camera_;
377 bool is_loaded_{
false};
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
Definition diff_drive_robot.h:84
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
RobotSensorManager()
Definition diff_drive_robot.cpp:158
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
CHRONO_DiffDriveRobot()=default
Constructor.
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
Definition diff_drive_robot.h:69
real_t wheel_radius
[m]
Definition diff_drive_robot.h:71
real_t caster_wheel_radius
[m]
Definition diff_drive_robot.h:75
void read(const Archive &arch)
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