"Step 1: Theory
The problem of self balancing robot is the problem of inverted pendulum. In order to counteract the force of the robot falling forward or backward, we need a mechanism to keep its center of gravity directly above its pivot point. This pivot point will be our axle. Our reaction strategy will be carried out by driving the wheel of the robot along its descending direction.
However, the problem is to stop there. If we have a simple feedback loop to check which direction the robot is descending and drive the wheels in that direction, our robot will oscillate and collapse inherently.
Therefore, our strategy will involve implementing a PID controller to drive the wheel back and forth in a controlled mathematical manner, responding to the falling direction of the robot, its falling speed, the amount of inclination so far, and the relationship between all three variables.
Details on how to implement this will be further explained in the PID section of this tutorial.
Step 2: build chasis for axis and control center
Chassis:
Cut the plywood into three pieces, each 9 cm wide and 14.5 cm long, which will become the three platforms of the robot chassis.
Drill 6mm holes at the corner of each platform, 10mm from the edge (see Figure).
Mark three platforms: top, middle and bottom.
Drill holes in the Arduino uno and L298N drive plates on the top platform.
Measure the center point of the middle platform and mark the area of the small bread board and the position for the connector (see chart).
Drill holes for the motor installed on the bottom platform and wire holes on the motor (see Figure).
Cut the M6 threaded rod into four small pieces of 25 cm.
Slide each component into the corresponding hole on the platform and secure both sides of each hole with M6 washers and M6 nuts.
Your platform should be approximately 9 cm apart and the remaining length should extend from the top platform.
Measure the tilt angle of each platform and adjust each nut so that it is all flush with the ground.
Cut one of the kitchen sponges longitudinally and use each rubber to the two halves of the top platform as a bumper. This step is for testing purposes only.
Axis:
Drill a 6mm hole in your center wheel.
Slide the 30 mm M6 threaded rod into the hole. Secure the outer end with M6 washer and M6 nut and the inner end with another M6 nut.
Install the coupling to the inner end of the M6 threaded rod and tighten the set screw to fix it in place.
Connect the open end of the coupler to the motor shaft and tighten the set screw to secure it in place. Make sure that both wheels are at the same distance from the motor itself.
Cut two 40 cm long power lines from the drum and weld them to the motor terminals.
Pick up the 5mm wood screw and connect the motor to the bottom platform according to the guide hole made in the chassis construction stage.
Pass the motor through the center hole of the bottom platform.
Control center:
Download the DXF file (bug lounge cut file. DXF) at the top of this section.
Laser Cut 2mm plexiglass documents.
Assemble the parts according to the chart at the top of this section.
Place the kitchen sponge on the bottom platform (glue or double-layer tape if necessary).
Place the control center on the sponge.
Squeeze the sponge and place two small wooden blocks (about 15x15mm) between the control center and the middle platform. It is an easy to disassemble way to put and release our control box. We implemented it on the above picture.
Step 3: build the circuit
Motor driver
The enable pin of L298N is used to control the motor speed using PWM (pulse width modulation), while the in1-4 pin of the driver is used to switch the direction of the motor. The following is a description of the fritzing diagram at the top of this section.
Connect the ENA pin of L298N to the digital pin 6 of Arduino
Connect in1 pin to digital pin 5 and in2 pin to digital pin 3
Connect the ENB pin of L298N to the digital pin 11 of Arduino
Connect in3 pin to digital pin 13 and in4 pin to digital pin 12
Remove 5V_ EN jumper to power the Arduino drive.
Connect the 5V screw terminal from L298N to the VIN pin of Arduino.
Connect the positive and negative power lines of one of the motors to the motora screw terminals.
Connect the positive and negative power lines of other motors to the motorb screw terminals.
Cut off the other power line and connect the red line to the VMS pin of L298N and the black line to the GND pin of L298N. The other end of the red wire shall be connected to the Wago connector.
Place the screw terminal on the end of the mini bread board.
Cut off the other power cord and connect it to the female socket. Then the red end should be connected to the Wago connector to complete the circuit all the way to the VMS pin of L298N, while the black end will enter the screw terminal we put into the mini bread board before.
Connect the GND pin of Arduino to the same line as the female socket in the mini bread board. This will ensure that our system infrastructure is fully connected.
Place another screw terminal on the mini bread board and connect the other end of the GND pin we put in L298N to this terminal. Make sure it is also connected to the ground wire we established in the previous step. Our grounding circuit should be complete by now( If this part is confusing, please check the image.)
Bno055 absolute orientation sensor
Bno055 is a 9-DOF sensor. It fuses data from accelerometers, gyroscopes and magnetometers into absolute 3D directions. Bno055 uses I2C communication, so we connect it to the A5 and A4 pins of Arduino UNO. This will change depending on the type of Arduino you choose to use.
Weld the title bar into the junction board of IMU.
Place the IMU on the mini bread board.
Use a jumper cable to connect the 5V pin of Arduino to the mini bread board.
Connect the VIN pin of the IMU in series with the 5V cable from Arduino on the mini bread board.
Connect the GND pin of the IMU in series with the GND pin from Arduino on the mini bread board.
Use a longer jumper cable to connect it from SCL pin of IMU to A5 pin of Arduino (it also serves as SCL pin).
Use a long jumper cable to connect it from the SDA pin of IMU to the A4 pin of Arduino (also used as SDA pin).
Hc-sr04 ultrasonic sensor
Hc-sr04 sensor is an ultrasonic ranging module, which provides 2cm to 400cm measurement function with an accuracy of 3mm. Its working principle is to send pulses and detect the time required to receive pulses. The distance measured by this pulse can be decomposed into a simple equation: distance = (high level time * sound velocity) / 2
Connect the VCC pin of hc-sr04 in series with the 5V cable from Arduino on the mini bread board.
Connect the GND pin of hc-sr04 in series with the GND cable from Arduino on the mini bread board.
Connect the trig pin of hc-sr04 to the digital 4 pin of Arduino.
Connect the echo pin of hc-sr04 to the digital 2 pin of Arduino.
Repeat steps 1 to 4 with the second hc-sr04, but this time use digital pin 7 as trig and digital pin 8 as echo.
Power Supply
O your motor needs 12V and about 2 amps each, so we will use an external power supply to provide this power. The Arduino itself will be powered by the 5V output of the motor driver.
Cut out a 5m long chain from the power spool.
Strip off both ends. Connect the other end of the power screw terminal to the other end.
assembling
Assembling the electronics to the chassis is simple. Just follow the guide holes you made in the chassis construction steps.
Connect the Arduino to the top platform using 5mm wood screws and mounting holes on plastic.
Take 7mm shims, place them under the L298N motor driver, and then pass the M4 bolt through the mounting hole and through the shims.
There should be a piece of double-sided tape under the mini bread board. Remove the cover of this patch and stick the mini bread board in the center of the middle platform. Make sure the IMU is in the center of the platform, and you may need to adjust the bread board to do so.
Take another piece of double-sided tape and connect the Wago connector to the edge of the middle platform.
Fix the female cylinder on one of the screws with a tie.
For testing purposes, cut the cleaning sponge in half and attach each half to both sides of the top platform and fix it in place with a rubber band. You can remove the robot immediately after it becomes independent, but until then, it will protect our electronic equipment from damage.
Step 4: Code: set monster class
In order to program our monster in a way that can be easily built by other developers, we implement it as a class / library. A class consists of a header file (. H) and a source file (. CPP). The header file defines everything in the class, and the source file contains the actual code implementation.
We'll start from scratch:
#ifndef Monstro_ h
#define Monstro_ h
#include “Arduino.h”
class Monstro {
public:
Monstro(int leftForward, int leftBackward, int leftSpeedPin,
int rightForward, int rightBackward, int rightSpeedPin,
int trigA, int echoA, int trigB, int echoB);
// Behavior
bool Update();
void Initialize();
private:
};
#endif
What we do here is set up a header file with a constructor that receives the pins we will use to interact with our sensors and drivers. We'll add this later when we introduce each component.
#The include Arduino. H statement just ensures that we can access the constants and types provided by the Arduino language.
We will use the update() function to call some behavior during the main loop, and the initialize() function to ensure that our sensors and motors are ready. There are more related contents in the later steps.
Our source file will reflect this header file:
#include “Arduino.h”
#include “monstro.h”
Monstro::Monstro(int leftForward, int leftBackward, int leftSpeedPin,
int rightForward, int rightBackward, int rightSpeedPin,
int trigA, int echoA, int trigB, int echoB)
{
}
// Behavior
void Monstro::Initialize() {
}
bool Monstro::Update() {
}
Thirdly, all we do here is to set the skeleton of the raw source file, and ensure that we also include the Arduino. H reference and the reference to the header file so that we can also access its definition.
Step 5: measure inclination (IMU)
Due to the library written by adafruit programmers, the code to implement bno055 is very simple. We will use adafruit_ Bno055 driver library and adafruit unified sensor library.
Let's first update our header file to interact with the IMU.
#ifndef Monstro_ h
#define Monstro_ h
#include “Arduino.h”
#include
#include
#include
class Monstro {
public:
Monstro(int leftForward, int leftBackward, int leftSpeedPin,
int rightForward, int rightBackward, int rightSpeedPin,
int trigA, int echoA, int trigB, int echoB);
// Behavior
bool Update();
void Initialize();
// IMU
volatile double xTilt;
volatile double yTilt;
volatile double zTilt;
private:
// IMU
Adafruit_ BNO055 _ bno;
void initializeIMU();
void readIMU();
};
#endif
We added something to the header file.
First, you will notice three include statements that ensure that we can access the adafruit library and imupaths. H library. They will need their functions when implementing IMU reading.
We also added the public variables xtilt, ytilt and ztilt. These are the places where we will store the data retrieved from IMU in each update cycle. Note that we have marked them volatile because we will use them in timer interrupts later in this tutorial.
We also added a bno055 object (_ bn
Our other product: