bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
epuck_robot.h
Go to the documentation of this file.
1#ifndef EPUCK_ROBOT_H
2#define EPUCK_ROBOT_H
3
7
8#include "bitrl//bitrl_config.h"
9
10#ifdef BITRL_WEBOTS
11
12#include "rlenvs/rigid_bodies/body_rotation.h"
13#include "rlenvs/rigid_bodies/body_translation.h"
14#include "rlenvs/rlenvs_types_v2.h"
15
16#include <webots/DistanceSensor.hpp>
17#include <webots/Motor.hpp>
18#include <webots/PositionSensor.hpp>
19#include <webots/Robot.hpp>
20
21// we need a supervisor as we want to
22// be able to reset the simulation
23#include <webots/Supervisor.hpp>
24
25#include <memory>
26#include <ostream>
27
28namespace rlenvscpp
29{
30namespace rigid_bodies
31{
32namespace webots_robots
33{
34
39struct EpuckRobotProperties
40{
41
45 static constexpr uint_t DISTANCE_SENSORS_NUMBER = 8;
46
50 static constexpr uint_t LEFT_POSITION_SENSOR_ID = 0;
51
55 static constexpr uint_t RIGHT_POSITION_SENSOR_ID = 1;
56
60 static constexpr uint_t LEFT_MOTOR_ID = 0;
61
65 static constexpr uint_t RIGHT_MOTOR_ID = 0;
66
70 static constexpr real_t WHEEL_RADIUS = 0.025;
71
75 static constexpr real_t AXLE_LENGTH = 0.052;
76
80 static constexpr real_t MAX_FWD_SPEED = 0.25;
81
85 static constexpr real_t MAX_BWD_SPEED = 0.25;
86
90 static constexpr real_t MAX_ROTATION_SPEED = 6.28;
91};
92
97struct EpuckOdometry
98{
99
103 real_t dl;
104
108 real_t dr;
109
113 real_t da;
114
118 EpuckOdometry(real_t dl_, real_t dr_, real_t da_);
119
125 std::ostream &print(std::ostream &out) const noexcept;
126};
127
128inline EpuckOdometry::EpuckOdometry(real_t dl_, real_t dr_, real_t da_) : dl(dl_), dr(dr_), da(da_)
129{
130}
131
132inline std::ostream &operator<<(std::ostream &out, const EpuckOdometry &info)
133{
134 return info.print(out);
135}
136
146class EpuckRobot final : protected EpuckRobotProperties
147{
148 public:
152 static const std::string name;
153
157 static constexpr uint_t DEFAULT_SIM_TIME_STEP = 32;
158
162 using EpuckRobotProperties::AXLE_LENGTH;
163 using EpuckRobotProperties::DISTANCE_SENSORS_NUMBER;
164 using EpuckRobotProperties::LEFT_POSITION_SENSOR_ID;
165 using EpuckRobotProperties::MAX_BWD_SPEED;
166 using EpuckRobotProperties::MAX_FWD_SPEED;
167 using EpuckRobotProperties::MAX_ROTATION_SPEED;
168 using EpuckRobotProperties::RIGHT_POSITION_SENSOR_ID;
169 using EpuckRobotProperties::WHEEL_RADIUS;
170
174 EpuckRobot();
175
179 std::shared_ptr<webots::Supervisor> get_webots_robot_prt() { return robot_; }
180
184 void activate_motor(uint_t motor_id, real_t init_velocity);
185
189 void activate_position_sensor(uint_t sensor_id, uint_t time_step);
190
194 void activate_proximity_sensor(const std::string &name, uint_t time_step);
195
199 int_t step(uint_t time_step) { return robot_->step(time_step); }
200
204 int_t begin_step(uint_t time_step) { return robot_->stepBegin(time_step); }
205
209 int_t end_step() { return robot_->stepEnd(); }
210
214 void reset();
215
219 void reload() { robot_->worldReload(); }
220
224 void stop_simulation() { robot_->simulationQuit(EXIT_SUCCESS); }
225
229 void pause_simulation()
230 {
231 robot_->simulationSetMode(webots::Supervisor::SIMULATION_MODE_PAUSE);
232 }
233
237 EpuckOdometry compute_odometry() const;
238
242 uint_t get_basic_time_step() const { return robot_->getBasicTimeStep(); }
243
247 real_t get_distance_value_from_sensor(const std::string &sensor_name) const;
248
252 std::vector<real_t> read_distance_sensors() const;
253
258 real_t get_motor_velocity(uint_t mid) const { return motors_[mid]->getVelocity(); }
259
263 RBTranslation get_position() const;
264
268 RBRotation get_rotation() const;
269
270 webots::Motor *get_motor(uint_t m) { return motors_[m]; }
271 webots::PositionSensor *get_position_sensor(uint_t m) { return position_sensors_[m]; }
272
273 private:
277 std::shared_ptr<webots::Supervisor> robot_;
278
282 std::vector<webots::PositionSensor *> position_sensors_;
283
287 std::vector<webots::Motor *> motors_;
288
292 std::vector<webots::DistanceSensor *> proximity_sensors_;
293};
294
295} // namespace webots_robots
296} // namespace rigid_bodies
297} // namespace rlenvscpp
298
299#endif
300#endif
int int_t
integer type
Definition bitrl_types.h:33
double real_t
real_t
Definition bitrl_types.h:23
std::size_t uint_t
uint_t
Definition bitrl_types.h:43
std::ostream & operator<<(std::ostream &out, const A2CConfig &opts)
Definition a2c_config.h:115
info
Definition play.py:44
Definition quadcopter_sim_env.cpp:16