|
bitsim
Model, simulate, estimate and control robots in C++
|
bitsim's actuators are the motors that move a robot. Like its sensors, see Sensors, a motor can be simulated, a model of the motor that runs with the simulation, or real, a motor on a board that the application talks to over MQTT. The application drives both the same way, so the code that moves a simulated robot moves one with real motors too.
| Actuator | What it is | Simulated by | Real, over | Robot file |
|---|---|---|---|---|
| DCMotor | a brushed DC gear motor with an encoder, in speed control | SIM_DCMotorBackend | MQTT_DCMotorBackend, on a DCMotorBoardBase | motors |
| BLDCMotor | the electrical model of a three-phase brushless motor with six-step commutation from Hall sensors | itself |
What each needs when bitsim is built; Installing bitsim says how to build it:
| Class | Needs |
|---|---|
| DCMotor with SIM_DCMotorBackend, BLDCMotor | nothing; a robot that the motors drive needs ENABLE_CHRONO |
| MQTT_DCMotorBackend, DCArduinoBoardImpl | ENABLE_MQTT |
The examples that use them:
| Example | Actuators |
|---|---|
| A brushless DC motor | a brushless motor in the shoulder of an arm, with its commutation and its current |
| A differential drive robot in Chrono | the two simulated wheel motors of the differential drive robot |
| A differential drive robot with real motors | two real DC gear motors on an Arduino, driving the simulated robot: hardware in the loop |
A DCMotor runs in speed control: the application sets the speed its output shaft should turn at, and reads back the angle and the speed it turns at. What the motor is depends on its backend, a DCMotorBackendBase, as a sensor depends on its backend.
The two kinds of backend couple to a physics engine differently, and output_type() says which:
| Output type | Backend | What the engine does |
|---|---|---|
| TORQUE | the simulated motor computes the torque it produces | applies torque() to the wheel, and the wheel turns as the physics says |
| ANGLE | the real motor has already turned | turns the wheel to the motor's angle() |
CHRONO_DiffDriveRobot connects each wheel to its motor in the way the motor's output type asks for, so the same robot runs with either kind.
SIM_DCMotorBackend models a DC gear motor, its encoder and its speed controller. The electrical part is
\[L \frac{di}{dt} = V - R i - K_e \omega_r , \qquad \omega_r = N \omega , \]
with the rotor speed \(\omega_r\) the gear ratio \(N\) times the speed of the output shaft \(\omega\), and the motor puts the torque \(N K_e i\) on the output shaft. Its controller runs every control_period, as a board would: it measures the speed from the encoder counts, filters it, and sets the duty cycle of the voltage with a PI controller and a feedforward of the back-EMF. The parameters, DCMotorParameters, and their defaults:
| Parameter | Default | What |
|---|---|---|
| resistance | 7 Ω | armature resistance \(R\) |
| inductance | 2 mH | armature inductance \(L\) |
| emf_constant | 0.0127 V s/rad | back-EMF and torque constant \(K_e\), at the rotor |
| rotor_inertia | 10⁻⁶ kg m² | the inertia of the rotor |
| friction | 10⁻⁶ N m s/rad | viscous friction at the rotor |
| gear_ratio | 30 | rotor turns per output turn, \(N\) |
| supply_voltage | 12 V | \(V\) at full duty cycle |
| speed_kp, speed_ki | 0.03, 0.1 | the gains of the speed controller, duty cycle per rad/s and per rad of error |
| speed_filter | 0.02 s | the time constant of the filter on the measured speed |
| control_period | 0.01 s | how often the controller runs |
| encoder_counts | 660 | encoder counts per output turn |
With the defaults the motor turns at most \(V / (N K_e) \approx 31.5\) rad/s with no load, and its stall torque is \(N K_e V / R \approx 0.65\) N m; no_load_speed() and stall_torque() give them.
What the model leaves out: the voltage is limited to the supply, but the current is not, and the driver has no dead band; the gears have no backlash and no losses; the encoder is ideal apart from its counts.
MQTT_DCMotorBackend is a motor on a board, a DCMotorBoardBase. One board usually drives several motors over one connection, so the motors share it, each on its own channel:
The target speed goes to the board, and the angle comes back from its encoder. The board reports every few tens of milliseconds, while a simulation steps every millisecond or two, so in between the backend predicts the angle from the last report and the speed. A physics engine that follows the angle differentiates it twice, so every correction of the prediction would be a spike of torque; angle() is instead a smooth estimate, a second-order filter, 30 rad/s wide by default, pulled towards the predicted angle and the reported speed. encoder_angle() and reported_angle() give what the board last reported. Angles count from the first report, so the shaft starts at zero.
DCArduinoBoardImpl is an Arduino that drives the motors and talks MQTT. The application publishes the target speeds on the command topic, bitsim/dc_board/command by default, and the board publishes on the state topic, bitsim/dc_board/state, the distance each wheel has covered:
Speeds are wheel speeds in rad/s, and distances are in m. The keys start with the names of the channels, left and right by default. The board turns its encoder counts into distances with its wheel radius, so the wheel_radius of the Config must match the one in the board's sketch. ack is the number of the last command the board received, which gives the round trip of the messages, and a board that has not reported for timeout, 0.5 s by default, is taken to be gone. A differential drive robot with real motors builds the whole loop, with the sketch for the board.
CHRONO_DiffDriveRobot reads its two wheel motors from the motors section of its robot file, robots/bitsim_diff_drive_robot.json. Both are alike: the section gives the backend, SIMULATION, and the parameters of the table above. For real motors, make MQTT motors as above and give them to the robot with set_motor(m, motor); the robot reconnects the wheel in the way the new motor's output type asks for. See Robots.
BLDCMotor is the electrical model of a three-phase brushless motor with six-step commutation from three Hall sensors, the motor of most robot joints and drones. The angle and the speed of its rotor come from outside, so the same model works with a simple rotor, BLDCRotor, and with a rigid body in a physics engine. Every step it works out the sector from the Hall sensors, connects the two phases of the commutation table in series, advances their current and returns the torque:
The back-EMF of each phase is sinusoidal, scaled so that its average over a sector matches the DC-equivalent model, DCEquivalentState, which treats the motor as a brushed one. The hall_offset of the constructor misplaces the Hall sensors, to show what a controller that sees the wrong sector does. A brushless DC motor explains the motor, compares the two models and puts one in the shoulder of an arm.