Self-Balancing PT (homemade Segway)

65,848

705

95

Posted in TechnologyElectronics

Introduction: Self-Balancing PT (homemade Segway)

This Instructable is intended to share my hobbyist project: a Self-Balancing Personal Transportation (SBPT), also known as "Homemade Segway". It is an interesting project requiring a minimal knowledge in electronics and some handcraft skills. All the components can be bought in internet and the mechanical parts assembly can be done in your home workshop.

The SBPT consists in a platform to stand on vertically with 2 side electrical motors powered by batteries. The controller algorithm ensure the balanced position to not fall forward or backward. The 2-wheeler motion is controlled by the rider with the tilt and the handle for the direction. Therefore additional electronical components like a controller, motor drive and acceleration/gyroscope sensor are required. The mechanical framework is made with material like wood because it is lightweight, electrically isolated and very easy to work for prototyping purpose. The description reflects the current status and will be updated with the coming optimizations and enhancement according to the readers’ comments. The way the hardware configuration and the software purpose are documented is to give you a reference which one you can modify to meet your needs. Finally, it is your homemade project!

Adrian Kundert

Step 1: Application’s Target Definition

The goal here is to state what we want to achieve with the SBPT. This will lead what and how the vehicle has to be build up. In my case, I wanted the following features:

- Enough powerful and rugged to ride outdoors even on gravel path

- At least 1 hour operation range

- Total cost less than 500€

- Wireless communication at debugging to get all motion freedom

- Data logging on a SD card for troubleshooting over a long period

Step 2: System Design

The schematic above shows the wiring of the main components as well the worm gear motor. The Arduino controller can be the Uno, Nano or the ATmega328 as single IC. The batteries are connected serial to get 24V which supplies the dual H Bridge for motors. This one is only powered as long the rider holds the ready button. In emergency case, the rider just release the push button to get unpowered the motors. The Arduino controller uses the serial communication in the “Packetized” mode at 38400 baud with the H-Bridge and the wireless module XBee. The tilt and steer are measured by an acceleration/gyroscope InvenSense MPU-6050 sensor in a “GY-521” on 2 separated breakout board and communicate with the Arduino by I2C. The tilt sensor (address 0x68) which is the most important is programmed to sense at every 20 ms and gives an interrupt to the Arduino. The second sensor (address 0x69) is pulled by the Arduino. Finally, the rider loaded limit switch is aimed to detect when the rider stands on the SBPT in order to activate the balancing algorithm.

Step 3: Framework Construction

The wheels and gearboxed-motors are hold by screws into 3 pieces of wood assembled in a U form box. The handle is a wood stick hold by a bolt in the front wood piece. Very important is that the weight distribution SBPT is balanced when held vertically, which is the riding position. For this verification, the heavy parts like the batteries must be considered, otherwise it will be more difficult for the balancing algorithm.

In my case, the batteries are placed rear in the wood box to compensate the motor weight which in centered. At the remaining space between the batteries and the front of the wood box is placed the electronics components.

On handle is taped the “rider ready” push button. Sorry about the very prototype status with a lot of tape and Vise Grip usage.

Step 4: Electrical Wiring

The wiring in the wood box is done according to the system schematic. The Arduino pinout table gives you the wire connection to the sensors, h-bridge and switches.

The tilt sensor is mounted flat horizontally to get the x axe angle. However, the steer sensor is mounted vertically to get the left/right steer angle with the y axis.

Step 5: Testing and Tuning

Don’t forget: the motors can be powerful! Ensure the trial is done in a wide and safe area to not cause damage or injuries. It is recommended to wear protection pads and a helmet.

Important is to proceed step by step. Begin by programming the Arduino (download the source code), then verify the communication with the sensors and afterward with the H-Bridge. The Arduino Terminal can be used for debugging and to verify the working status. For example, the PID gains must be tuned because it depends of the motor mechanical and electrical proprietries.

The gains can be tuned with this typical method:

1. Kp is mainly for the balancing. Increase Kp till the balancing become instable, Ki and Kp remain 0. Reduce Kp a bit to get stable again.

2. Ki is for the acceleration/deceleration by the tilt. Increase Ki to get the right acceleration to avoid falling when tilting forward, Kp remain 0. The balancing should be now is stable.

3. Kd is used to compensate the integration and get again a stable the balancing.

In Terminal, you can get the differents commands by the command "?".

________________________________________________________________________________________

? - Help for the commands

p,i,d [value] - Set/Get PID gain, value between 0 to 255

r [value] - Force the motor speed, value between -127 to 127

v - Software version

________________________________________________________________________________________

With the command "p", you get the Kp. With the command "p 10", you set the Kp to 10.

Once the Arduino controller is powered, the sensors are initialized and finally the waiting state is reached. Pressing the push button will signal to the controller the SBPT has to be prepared by getting the vertical position by activating the motors forward or backward dependant the initial position. From this point, this button has to be held otherwise the motors are unpowered and the controller goes back in the waiting state. Once the vertical reached, the controller is waiting the signal from the “Rider Loaded” limit switch, normally pressed by the foot when get on the platform. When this happen, the balancing algorithm is launched and will activate the motors forward or backward to stay in a vertical position. Leaning forward will create a motion forward, and vice versa. Keep leaning will increase the motion speed. Leaning in the opposite side of the motion will decrease the speed. About the direction left or right, just move the handle in the desired direction.

Step 6: Demo

Here you can watch a short video as a demo to see the dynamical behaviour of my SBPT. This should be the funniest step of all!

2 People Made This Project!

Recommendations

  • Casting Contest

    Casting Contest
  • Make it Move Contest

    Make it Move Contest
  • Microcontroller Contest

    Microcontroller Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

95 Comments

Great job

Hello Adrian plzz mail me connection diagram for the relay module u have installed at Tushar.soni38@gmail.com regards tushar

8 replies

Bonsoir

J ai vu tes commentaires et je pense que tu es français

J habite en Isere mais je parle écrit tres mal l anglais

As tu pu expérimenter ce projet ?

Je suis un projet aussi de segway et j aurais besoin d aide au niveau de la programmation et des calculs de pid

Tu peux m envoyer un mail sur georges.lombard3@icloud.com

Merci d avance et bonne soirée

Gh lombard

bonsoir Gh

En effet, je comprend le français. Ai-je dèja répondu en francais sur ce site?

Dis moi plus précisement ce que tu as deja fait et voudrais savoir. Ca sera plus facile de te guider.

Adrian

bonsoir et merci pour ton contact

Bravo pour ton projet !!!

J envisage de réaliser le même mais je ne maitrise pas la notion de pid pour contrôler la vitesse des moteurs pour rester en équilibre

Je ne sais pas comment le réaliser sous arduino pour faire auto balancer le gyropode

La partir mécanique est ok , pour la direction gauche/droite j ai prévu d'intégrer un capteur effet hall 5v qui tournera en fonction de l orientation du guidon qui sera monter sur un axe.

c est la partie programmation qui me pose probleme , au niveau des capteurs gyroscopique j ai un mpu 6050 mais je pense qu il faut filtrer les données

Merci de m aider sur le pid et le mpu 6050

Giloris

comme j ai deja ecrit, il faut une base en electronique et programmation.

As tu deja fait un essai/tutorial avec un arduino? Essayé de communiquer avec le module MPU050 par I2C?

bonjour Adrian

J ai telecharge le code source mais je ne comprends pas le fichier SelfBalancing PT.ino ; Je ne sais pas ou trouver le code intégral (mpu sabertooth , contrôle des moteurs ...)

Merci pour ton aide.

Giloris

le MPU fait des interrruptions hardware periodique,ligne 82 de SelfBalancing PT.ino.

le calcul du PID se trouve à la ligne 60 du fichier controller.cpp

ligne 80 est le traitement du calcul PID selon l'etat.

control de moteur par el Sabertooth de la ligne 110 à 174

Le lien dessous est un bon tutoriel de PID.

http://ozzmaker.com/success-with-a-balancing-robot...

bonjour

Merci pour ces renseignements , je vais étudier le Tuto qui va sans doute m'aider à comprendre le pid et l utilisation du mpu.

Par contre il utilise des moteurs avec encodeurs , est ce indispensable ?

Merci

Giloris

bonjour

Je connais un peu l arduino j ai deja fait plusieurs petits projets

(Automatisation de poulailler, contrôle électrovanne, boîtes à lettres connectée ...)

Mais c est surtout le pid qui me pose probleme

Pour asservir les moteurs pour que le plateau auto balance

Si tu pouvais m'aider sur ce sujet .

Merci

Giloris

Hello Adrian. I want to assemble a Segway according to your drawing. Please help. Can't deal with the connection of the relay and connecting the gyro to the arduino. Send me please circuit diagram your device for email. tolikhoroshev@mail.ru

Can you share where to order the motors and wheels?

Thanks,
Mike

You use state-space control? ie state-feedback? Or is it PID.

1 reply

it is a simple PID.

Dear readers

Here an update after a few month of trial and testing.

- The mechanics, motors and power electronics work like a charm.

- The balancing algorithm is limited when the motor reach is
maximum speed. Pretty simple to understand: if you lean forward or backward,
the speed has to be increased to compensated, but if the motor is already a
max, it is not possible anymore, so you fall! I was aware of that and without
knowing the motor speed, it is always a guest if I can still lean or not.
Therefore, I added a buzzer (or a vibration motor) in the handle activated by the ARDUINO when the motor speed is
over 90%.

- It happened a few time that my ARDUINO NANO had to be reprogrammed
because it lost his program!. This happens when the motors regenerate current
while driving down a hill.

I would say thank you for all shared comments.

Oh, just one more thing. You can see my temporary grey cable...it wires to (3) 10k 10-turn pots glued together, I adjusted the code to read on startup via analog inputs, the pot settings, these are translated to my PID settings. Allows a simple adjustment in the field using your fingernail even, a quick reset and you're off with fresh PID settings to test. When done, I'll tuck the cable down inside.

Notes on the pics I added....I installed 12v strip leds on the underside and front. Shines the surrounding area really well operating at night. I didn't want to hold a button down all the time, so added the emergency red cord, it's tied to the operator, and wired to disconnect battery power in the event of an "unplanned departure". Because the motor controller is capable of running on forever with its last speed command if the arduino craps out for some reason. I also added a line of code to include the "rider loaded" switch along with the button press switch (bypassed here) so that if you step off, it shuts down via computer. Works great. Added a dc meter I had lying around, it monitors the 2 lithium polymer series 12v 13AH packs I built for the project, there's a switch to shut it off, as it requires its own 9v battery source. Installed a 12v switching power supply and AC cord in the underside to charge, wired through a DPDT switch wired to parallel the 2 12v batteries for charging. So down position is charging, middle is off, up is ready. I used an old Hoveround for the motors and chassis, they work great. I machined some adapters to mate the small Hoveround hubs to a 4-bolt pattern standard US-style trailer hub, after machining off the unnecessary steel to minimize the weight. Used 2 spare trailer tires I had lying around. This thing is fast! Way too fast to allow it up to full speed, unless you like the risk of faceplanting on the asphalt when something goes wrong, like a pothole or a computer crash. Very happy overall with the project, highly recommended for those with the skillset.

Logging some of my build notes here for others' benefit. One: don't forget the address pins on the MPU6050 devices. Figured that out the hard way. Second, and not sure the answer yet, the PID settings are tricky. Currently I am using 1,28,6 for the PID constants. I revised the code to incorporate (3) 10-turn pots for PID settings. I think it best to be able to revise things on the fly, while out in the street or parking lot without hassling with other devices. Bench testing indicates my PID settings are on the money. A little tweaking once test running for real, should be good to go.

Another interesting note. The code spits out plot data, you can open the serial plotter(instead of the serial monitor in the Arduino IDE) and it plots what appears to be PID gain and some other variables. Interesting to watch.

For the PID settings, I think I would add some active control, whether by mechanical pots or an lcd and joystick, to allow on the fly adjusting. I mean once the code is loaded, that's all that needs adjusting right?