DIY Self Driving - Part 7. Bringing it all together

Please Note:  Unlike most of my projects which are fully complete before they are published, this project is as much a build diary as anything else.  Whilst I am attempting to clearly document the processes, this is not a step-by-step guide, and later articles may well contradict earlier instructions.

If you decide you want to duplicate my build, please read through all the relevant articles before you start.

The theory is done.  How does it all come together?  If you have been reading from the beginning you will see that lots of details have changed as the project has progressed.  More often than not this was because either something didn't work out as expected or I figured a better way of doing things.

One thing which may surprise you is just how much wiring there actually is.  To make the process less frightening, I will break it down into tasks.

Power Distribution

I noted earlier that the car has two distinct battery supplies.  The "main" battery which powers the motor, steering and lighting is located under the seat.  This is a six-volt sealed lead-acid (SLA) battery.  The second is a Lithium-Ion (Li-Ion) pack in the front compartment.  It is used only for powering the electronics.

Distributing power for the motors is easy since they are conveniently located in the main cabin, and we can pick off the power from the wiring for the original direction switch.  The electronics battery however is all new.  To solve this, I created a simple distribution strip and attached it to the firewall.  Any electronics which needs a regulated 5V supply is connected to these strips.
Electronics Power Distribution
Because there are two different supplies, a common ground is required, and since the body of the car is plastic, this means running a wire between the the sets of wiring to do the job.  Use a medium gauge hook-up wire for this task.

Mounting all the electronics

Vibration will cause your problems in the long-run so secure everything properly.  By examining the photos, you can see I used M3 x 12 screws and nuts with a small spacer to secure the power regulator, distribution, Arduino and motor driver boards.  The Raspberry Pi I am using is an early model which has no mounting holes.  To solve this, I used some adhesive mounting hooks.

If you have not already set up your Raspberry Pi, this would be an ideal time to do so.

Wiring up

Electronics Power

Power for the electronics is switched using a relay powered off the main battery.  This sounds old-fashioned (it is), but it's also durable and effective.  The battery is connected to the voltage regulator through the relay.

The output side of the regulator goes to the electronics power distribution strip.  In my case negative is at the top, but it's up to you.  Check your strip for shorts before connecting the battery.

You need power for the Arduino, Raspberry Pi, ultrasonics, LCD and current sensor (the last two are optional).  The current sensor requires a cable run to the battery compartment, and I followed the existing wiring.  Make sure you secure the new cables to the existing ones to prevent damage as they do get close to wheels.


Completing the Arduino first makes good sense because we can use it to test subsystems on it's own.  For pin connections, always refer to the source code as the definitive reference.  The sets of connections are:
Power: To the distribution strip
Motor Driver:  Two digital outputs (D2 and D4), to the motor drive A and B inputs respectively.  Pull in an extra conductor while you are there so you can experiment with PWM control later.
Servo: PWM pin D9 to the servo control line.
Ultrasonics:  As well as power (to the dist. strip) connect the TRIG to D10 and ECHO to D12 on the Arduino.
Current Sensor (Optional):  Power (dist. strip) and a single analogue signal to A0.
LCD (Optional): Power (dist strip), SDA (Data) and SCL (Clock) to the LCD panel

Raspberry Pi

At this stage, do not connect the Serial lines to the Arduino.  We need to load the firmware and test it first.
Serial out (8) to Serial In on the Arduino
Serial in (10) to Serial Out on the Arduino
GPIO7 (26) to D5 on the Arduino
Power to the distribution strip

Current Sensor

The two screw terminals are used to connect the module in-line with the positive battery wiring.  The pin terminals connect to 5V, Ground and the analogue input on the Arduino.

Wiring all done.  Ethernet helps with development

One little problem I didn't think of at first was "How am I going to charge the electronics batteries with the front fitted in place?  This could also apply to connecting the Ethernet port on the Raspberry Pi as well.

The solution presented it when I discovered that the "grilles" were removable. (The simply clip into place).  Removing the left-hand grill, I used a 60mm hole-saw to create a discrete opening behind the grill.  A short Ethernet cable and extended battery charge cable are just within reach now.

Accessing the battery charge port

First smoke test

First of all, make sure the car is off the ground because if your work area is small the car can damage itself or something else if it gets away.

Disconnect the 5V wire from the Raspberry Pi to stop it from starting up.  Connect your development machine to the Arduino via USB and load up steering.ino.  Using the Serial Monitor function (set to 115200 bps), test both the steering and motor control functions.

Sending "." or ">" will steer the servo to the right, while "," or "<" turns left.
Sending "f" or "F" runs the drive motor forward, while "r" or "R" runs it in reverse.  "s" or "S" stops the motor.
Sending "p" or "P" triggers an ultrasonic ping.  Check it raises US_OUT in response to an object 14cm to the front of the sensor.

If your motor runs the wrong direction, reverse the connection to the motor controller.  While you are there, check the current monitor if fitted.  On mine, a positive value is a battery discharge and a negative is a charge.  This makes sense to me, but if you want to make it work like a traditional car ammeter, reverse the wires on the input to the current sensor.

You can see my car being tested in this short video:

Second smoke test

Keep the car off the ground and switch off the master power.  The reconnect the Raspberry Pi power, and plug in your camera and Ethernet (if used).  At present we are NOT starting the software automatically, so you will need to ssh into the board and start the control program manually.

What happens next really depends on what the camera is pointed at.  The drive wheel should start running forwards, and because it can't actually move the car, the front wheels will slowly move from centre to one side.  Don't worry, the software limits the movement so you can't damage the servo.

If you got a result similar to this, you can celebrate.  You earned it!

Blurred rear wheel means it's alive!
However, if things went wrong, the control program output will be a good place to start.  If it can't open the camera chances are it's not supported under Raspbian, so you will either need drivers or another camera.

If you get an "Error 2" opening the serial port, do an "ls /dev/tty*" and find your port, then update the code and recompile.  If you get "Error 5", your user is probably not in the dialout group or the port is already being used.

Update:  If you are using the 0.5.1 release of the software you will find the LCD is rather hard to read with some LCD panels.  A correction to this is on the "develop" branch of the code now, but it will be released as part of 0.6.x

Further Reading
Part 1 - Introduction
Part 2 - Preparing the car
Part 3 - Wiring Harnesses
Part 4 - Steering
Part 5 - Sensors
Part 6 - Software In Depth


Popular posts from this blog

The QYT KT-7900D

Life with Deep Racer - Part 2

DIY Self-Driving - Part 9 - New Imaging