|
bitsim
Model, simulate, estimate and control robots in C++
|
This example accompanies the sixth lesson of the robotics course. It brings together the differential drive robot of A differential drive robot in Chrono and the motor model of A brushless DC motor. The two wheel motors are now real: two DC gear motors with encoders, driven by an Arduino. The robot they drive is still simulated in Chrono. The simulation sends the board the speed each wheel should turn at, and the board sends back the distance each wheel has covered. The simulation turns the wheels by that much, and the robot moves.
You need bitsim configured with Chrono and MQTT support, and an MQTT broker such as Mosquitto. The hardware is optional: the example can simulate the motors as well, or run a fake board on the computer.
Testing a robot only in simulation can hide problems the hardware has, and testing it only on the hardware is slow and risky. Hardware in the loop (HIL) is the middle ground: some parts are real and the rest is simulated. Here the motors, their encoders and their speed controllers are real, and the robot body, its wheels and the floor are simulated. The loop is:
The example adds three things to bitsim.
A DC motor. bitsim::actuators::DCMotor is a motor run in speed control. You set the target speed of its output shaft, step it, and read its angle and speed. What the motor is depends on its backend, in the same way as the sensors of the robot in Following a line with infrared sensors and avoiding obstacles, as follows.
A board. One board drives both motors over one connection, so the motors share it. bitsim::boards::DCMotorBoardBase is a board that drives DC motors: it takes a target speed per motor and reports what its encoders measure. bitsim::boards::arduino::DCArduinoBoardImpl is an Arduino that does it over MQTT.
A robot that uses them. CHRONO_DiffDriveRobot now has a DCMotor on each wheel. The motors come from the new motors section of robots/bitsim_diff_drive_robot.json, and set_motor replaces them. Before every Chrono step, update steps the motors and passes their output to the wheels:
set_velocity turns a forward speed \(v\) and a turning rate \(\omega\) into the wheel speeds of the differential drive, \((v \mp \omega L / 2) / r\) for the left and the right wheel, with \(L\) the distance between the wheels and \(r\) their radius.
A simulated motor and a real one connect to Chrono in different ways.
A simulated motor produces a torque. Its electrical part is the one of bldc_motor,
\[L \frac{di}{dt} = V - R i - K_e N \omega, \]
with \(\omega\) the speed of the wheel and \(N\) the gear ratio. It puts the torque \(N K_e i\) on the wheel, through a ChLinkMotorRotationTorque. Chrono moves the wheel, and the motor reads back how fast it turns, so the motor feels the weight of the robot.
A real motor has already turned by the time we hear from it. So the wheel is connected through a ChLinkMotorRotationAngle, and Chrono turns it to the angle the board reported. Chrono then tells us the torque that took.
The robot chooses the link from the backend, so the rest of the code does not need to know which motors it has.
The board runs a PI controller on each motor every 10 ms. It measures the speed from the change of the encoder counts and filters it. A feedforward gives the duty cycle that balances the back-EMF at the target speed, and the PI controller corrects the rest:
SIM_DCMotorBackend runs the same controller with the same gains. The constants are those of a 12 V JGA25-370 gear motor with a 30:1 gearbox, about 300 rpm at the output. They are in the robot file:
| parameter | value | |
|---|---|---|
| resistance | 7 | Ohm |
| inductance | 0.002 | H |
| emf_constant | 0.0127 | V s/rad, at the rotor |
| gear_ratio | 30 | |
| supply_voltage | 12 | V |
| speed_kp, speed_ki | 0.03, 0.1 | duty per rad/s, duty per rad |
| control_period | 0.01 | s |
| encoder_counts | 660 | per wheel turn |
The gains are small on purpose. The same controller has to work on a free motor, which has almost no inertia, and on a motor that pushes the robot, which has a lot of it. Larger gains make the free motor oscillate.
The board and the simulation exchange JSON messages on two topics, 50 times a second:
| topic | from | message |
|---|---|---|
| bitsim/dc_board/command | simulation | {"seq": 12, "left_speed": 2.5, "right_speed": 2.5} |
| bitsim/dc_board/state | board | {"ack": 12, "left_distance": 0.53, "right_distance": 0.52, "left_speed": 2.49, "right_speed": 2.51, "t_ms": 81234} |
Speeds are wheel speeds in rad/s and distances are in m, counted from when the board started. The board turns encoder counts into distance with WHEEL_RADIUS, which must be the wheel radius of the simulated robot, 0.1 m. ack is the number of the last command the board received, which gives the round trip time. If the commands stop for half a second, the board stops the motors.
The broker must accept connections from the network. With Mosquitto 2 that needs these two lines in its configuration:
Anonymous access is fine on a home network for a lesson. On a shared network, set up users and passwords.
The sketch is in real_dc_motors_board. It is written for:
| left motor | right motor | |
|---|---|---|
| PWM | 5 | 6 |
| direction | 8, 9 | 10, 11 |
| encoder A (interrupt) | 2 | 3 |
| encoder B | 4 | 7 |
Standby of the driver is on pin 12. The right motor faces the other way on the robot, so the sketch reverses it. The pins, the motor constants and the gains are at the top of the sketch. It needs the ArduinoMqttClient library from the library manager, and has been compiled for the UNO R4 WiFi but not yet run on real hardware.
Copy arduino_secrets.example.h to arduino_secrets.h and fill in your WiFi name, password and the address of the computer with the broker. That file is ignored by git, so the password stays on your computer.
The board reports every 20 ms, while Chrono steps every 10 ms. In between, the MQTT backend predicts the angle from the last report and the speed. A wheel that follows an angle is differentiated twice by Chrono, so every small correction of the prediction, and the noise of the encoder counts, would come out as a spike of torque. So the wheel follows a smooth estimate instead, a second-order filter pulled towards the predicted angle and the reported speed:
The bandwidth \(w\) is 10 rad/s. Faster, and more of the encoder noise reaches Chrono. Slower, and the wheels lag behind the motors.
The broker defaults to tcp://localhost:1883. The robot drives a square with sides of 1 m: forward at 0.25 m/s for 4 s, stop for 1 s, turn left on the spot at 45 deg/s for 2 s, stop for 1 s, four times. Each second it prints the targets and speeds of the wheels, the distances they covered, the pose of the robot as Chrono has it, and the pose from odometry: where the robot thinks it is, from the wheel distances alone.
--fake-board stands in for the Arduino. It uses the same topics and messages, and two SIM_DCMotorBackend motors that turn freely, as the real ones do with the wheels off the ground.
With --render, the robot drives the square over and over in an Irrlicht window. The white line is the plan, the yellow line is where the robot went, and the red line is where odometry thinks it went.
The motors carry the robot, so they are slow to change speed. They overshoot the target at the start of each side, and keep turning after each stop: the first turn ends at 103 degrees instead of 90. The right wheel then starts faster than the left, which turns the robot back towards 90 degrees. The square still closes within about 1 cm, partly by luck.
Odometry agrees with Chrono to the millimetre, because the wheels do not slip on this floor. On a real floor they slip a little, and the error of odometry grows with the distance driven.
With free motors the speeds follow the targets to within about 0.1 rad/s, and each turn ends within two degrees of the plan. The round trip is mostly waiting: both sides talk every 20 ms, so a command waits for up to 20 ms before the board reads it. On real WiFi, add a few milliseconds more.
The torque line is the most important result. To follow the free motors, the simulated wheels needed up to 1.1 N m, at the starts and stops, while the motors stall at 0.65 N m. The free motors change speed far faster than they could with the robot on them. Finding that out before building the robot is what hardware in the loop is for. A slower ramp of the speed targets, or stronger motors, would fix it.