JayThree Balancing Car Project — Part 3/5

A Awesome project based on Your Arduino Balancing Robot (YABR) by Joop Brokking.

J3
Jungletronics
9 min readSep 16, 2017

--

In this thread, I intend to step by step describing my attemp to …

Wow, how many themes!

Here are some questions that I’m trying to respond:

1- A gyroscope sensor is hooked up on arduino and working… I have no idea what the units of the data it’s giving me are in though;

2- I try to spin the board in my hand at what should be roughly 90 deg/sec but I’m getting numbers from the gyro of 2000–5000 range;

3- I’ve just tried scaling it down and calibrating the scale factor by visually rotating the board 90 deg/sec. I know there has to be a better way to do this;

4- I’ve been over the data sheet many times and I’m still not getting it. It says the units are mdps/digit which I’ve never heard of before;

5- How to apply PID to control to the robot that balances itself?

6- Kalman filter and Complementary filter? I would like to know more about those concepts!

Here we go again!

My GY-85 Board sensor is the IMU (Inertial Measurement Unit) 9DOF (9 Degrees Of Freedom).

GY-85 Board — IMU

This mean that there are inside the unit These Chips:
- ADXL345 a 3-axis Accelerometer — 7-bit address (0x53)
- HMC5883L a 3-axis Magnetometer — 7-bit address (0x1E)
- ITG3205 a 3-axis Gyroscope — 7-bit address (0x68)

In my github you’ll find all the codes for this board and project. Click Here!

Thinking about Gyroscope

Let’s begin with Gyroscope or gyros. This is ITG3205's data sheet.

Gyros detect rate of rotation in degrees per second around the three axes (pitch, yaw and roll).

Gyros are used in handheld electronics for image stabilization, Drone Stabilization, GPS assist, Flight Controller, motion-capture and user interface, and the market for these devices is expanding rapidly.

ITG-3205 is a newer version of InvenSense’s ITG-3200 triple-axis digital output gyroscope chips.

The ITG-3205 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyro outputs, a user-selectable internal low-pass filter bandwidth, a Fast-Mode I2C (400kHz) interface, a wide VDD supply voltage range of 2.1V to 3.6V., a low 6.5mA operating current consumption for long battery life, a standby current: 5μA. Additional features include an embedded temperature sensor and a 2% accurate internal oscillator.

It says the range can be set to 2000 DPS, which I think is the maximum and unique angular speed the device can measure.

I prospected the concept and discovered that DPS stands for Degrees Per Second.

So let’s say 2000 DPS devided by 360 and multiply by 60, gives me 333.334 RPM (revolutions per minute). This is the maximum turn my project can have.

Good! Now the minimum range…

250 DPS devided by 360 and multiply by 60, gives me 41.667 RPM (revolutions per minute).

So if I’m going to measure a Gadget that rotates, or tilts…these are my…

For my Project, both serve … I will choose 2000 DPS, following Joop Brokking’s experience.

Kalman or Complemetary filters?

I read that Kalman filter is great, very complex to understand and implement though.

So we will opt for Complemetary filters for the sake of simplification. I´m a hobbyist, Not a Mathematician…

There are issues tought:

A gyroscopes is great at measuring rotation, but has no understanding of orientation. And an accelerometers is good at determining orientation, but has no ability to keep track during movement and rotation.

The problem with gyroscopes is it has the tendency to drift, not returning to zero when the system went back to its original position. The gyroscope data is reliable only on the short term, as it starts to drift on the long term.

The problem with accelerometers it will also see a lot more than just the gravity vector. Every small force working on the object will disturb our measurement completely. The accelerometer data is reliable only on the long term, so a low pass filter has to be used.

The complementary filter gives us a simple and elegant solution to our dilemma. On the short term, we use the data from the gyroscope, because it is very precise and not susceptible to external forces. On the long term, we use the data from the accelerometer, as it does not drift. In it’s most simple form, the filter looks as follows:

The gyroscope data (gyro) is integrated every timestep with the current angle value. After this it is combined with the low-pass data from the accelerometer (accel ).

This technique is surprisingly simple. The constants add up to unity! The constants (0.98 and 0.02) can be changed to tune the filter properly. If it works, great for me!`o´

Let’s Hardware!

Now let’s put all this Hardware_v2 to the test! In my Google-Drive you can upload all files for this project.

But first let’s solve a problem: GY-85 waits for 3v3 and my Arduino Pro Mini offers only 5v. How to solve?

I’ve supplied my 3.3V from a 5V ATMega328 Pro Mini by using two 1N4001 diodes in series. It was not an optimal solution at 3.0V, but it was cheap and I had the diodes laying around.

The same solution may possibly work as a temporary hack if you need something immediately and don’t want to wait on an order from a supplier.

You’ll need:

1 x Arduino UNO
1 x Pro Mini Atmega328 16MHz 5V Compatível com Arduino
1 x Mini DC 7~28V to DC 5V Step-Down Converter Power Supply Module
1 x TTL to USB Serial Converter — TTL-232R — FTDI Chip
1 x TTL to USB Serial Converter (UartSBee v5 or similar)
1 x 11.1 volt battery
1 x 150 Ohms Resistor
2 x 2.4 G WirelessSerial Radios
1 x GY-85 IMU Board (ADXL345, ITG3205)
1 x wiiNunch with adapter and

2 x 1N4001 Diode

Let’s go through 7 functional Arduino sketches always adding new functionality as much as possible and walking towards our final project! all runs on the deck mounted on the breadboard above in case you really want to replicate these experiments…It’s very nice to see things working, if you know what I mean…use the Serial-Oscilloscope (commented later). You can find all codes in my github repository. Look for Prefix _35 Project .

1>One for accelerometer… _35_GY85_ADXL345_Accelerometer_06.ino

This code simplifies sensor’s data acquisition and does this simultaneously, as recommended by the data sheet! forms a code pattern for the other sensors. It deserves increased attention!

2>another for gyroscope… _35_GY85__ITG3205_Gyroscope_07.ino

Notice the definitions. They follow the names of data sheet’s real registers.

3>and another for i2c bus test… _35_J3BC_Hardware_Scanner_10.ino.in

Based on the Arduino Scanner code, it rotates at the I2C addresses to discover the components of the GY-85 board and Wii Nunchuk. It should be adapted if you use another IMU (Inertial Measurement Unit) board other than GY-85 .

4>one testing wiichunk… _35_ArduinoNunchukDemo_09.ino.in

Follow the improved Wii Nunchuk library for Arduino of GabrielBianconi/arduino-nunchuk. It works like a breeze!

5>another for sending data from 2.4 G radio… _35_Sending_Serial_Counter_12.ino

This code simply sends a count from Arduino UNO for initial tests of the radio transmitting.

6> and receiving data…_35_Receiving_Serial_Data_11.ino

This code receives the data that arrives at the serial of the Arduino Pro Mini.

7> _35_GY85_HMC5883L_Magnetometer_08.ino

and for this documentation to be complete, I also offer the code for the magnetometer, although not used in this project (who knows in another version?;)

Phew still good… Things are getting pretty long… But it’s fun, you know?

We are evolving and learning during this journey, right?:) …

For quick apprehension if you want to watch my video, please take this time!

I will not go into much details because those codes are very rich in comments and with a little interest you can even improve them;)

The only code I will comment for is the gyroscope, because the importance and ‘cause others follow the pattern established by it.

Let’s right jump into it!

Here is the sequence:

1> On top we declare all registers needed, including I2C address of this chip. Note: some programmers do the harder way (0xD0>>1 that resolves to 0B11010000>>1 = 0B01101000), but the code is less readable. I prefer to be clear: (0x68 or 0B01101000) . Please, consult this data sheet. We use the same names as data sheet do;

2> In getGyroscopeData() we make a simultaneous reading (BUFFER of 6 axis to read) as recommended by the data sheet. Cool!

3>In initGyro() we use writeTo() method to be concise and precise. The registers needed are (consult data sheet):

. PWR_MGM (Register 62 — Power Management) — to put the ITG3200 in normal mode;

. SMPLRT_DIV (Register 21 — Sample Rate Divider) — to put ADXL345 Sample rate divider to 7d;

. DLPF_FS(Register 22 — DLPF, Full Scale) — Digital low pass filter configuration and internal sampling rate configuration;

. INT_CFG (Register 23 — INT_CFG, Interrupt Configuration) — logic high, push-pull, no latch, no interruption.

4>In loop() we recover all the axes at once and present to the serial.

Notice that the buffer takes all data transparently. I love the way this code do thing:D

Serial Oscilloscope

Here I made extensive use of Serial-Oscilloscope . The application functions as a basic serial terminal or osciloscope.

As above said the gyroscopes has the tendency to drift. You can see clearly it in this image:

And all the moviment that gyros can provide. It’s awesome toll. See reference below.

That’s it for now!

In the next post let’s deal with PID (Proportional, Integral and Derivative) Controls, Interruptions issues and the like…Let’s make the connections on the proper board for the final design and prepare for great fun!

Wow, this highway goes far though:)


Is there errors in my writing ? This presentation is rather time consuming. English is not my native language (you may have notice).

Do not hesitate to point me out the errors so I can fix them! I will pursue if sufficient interest is shown…

I invented nothing! I read A LOT and went by trial and error.

This is a great time to unwind with your favorite pastime or hobby and I really love electronics!

This documentation is a way of keeping my sanity in the face of information chaos….

If you got this far and/or can also collaborate with this project, correcting it, improving it…I just wanted to say THANK YOU!

Here is my twitter, or face, or G+ , youtube or email, oops, enough

Download All Files for This Project

Part 1 — — Part 2 — — Part3 — Part 4 — -Part5

References & Credits:

Your Arduino Balancing Robot (YABR)- By J. Brokking

GabrielBianconi/arduino-nunchuk

Serial-Oscilloscope — The application functions as a basic serial or osc_ope

Arduino and GY-85 9DOF

Geeetech Wiki- an awesome & rich technical documentation

Sparkfun Product-ArduinoProMini

--

--

J3
Jungletronics

Hi, Guys o/ I am J3! I am just a hobby-dev, playing around with Python, Django, Ruby, Rails, Lego, Arduino, Raspy, PIC, AI… Welcome! Join us!