Introduction: Toy Truck Powered by Raspberry Pi
Here is a great way to impress your fiends when your out an' about at the park, by modifying a broken toy Truck/Car with a Raspberry Pi and Adafruit's DC & Stepper Motor Pi HAT , to existing electronics . And for the controller to run the car/truck, you basically have two options to choose from, as shown in the pictures and video above -
#1 KeyBoard Controller - if you like playing computer games using your keyboard ,follow the steps below to upload the python program to your Pi to use the wireless keyboard to control your toy car/truck as shown in the second part of the video above.
#2 Mobile controller using a flask web app - use your mobile/tablet to control the truck, by connecting to the Pi's WiFi network and opening up the flask controller web app on your mobile browser as shown in the first part of the video above.
Step 1: Getting the Monster Tuck Ready for the Pi
Remove the screws from the Monster truck and gently dis-assemble the parts.
Be careful and patient ! while getting the circuit board and the battery holder out to prevent it from damaging the battery holder.
In my case, I got the monster truck from my neighbor after his 6 year old broke the receiver and yes the truck was also in a bad shape. After repeatedly trying to fix the controller , I decide to strip the truck as shown in the picture above and check if the motors and battery holder were still functional.
Step 2: Test the Motors and Solder Wire to the Battery Pack
Before you get to interesting bits below test to check if the DC motor are still functional , in my case I used a 4 AA battery holder to test both the motors as shown in the picture above.
Now desolder the circuit board using a soldering iron from the battery holder.
Once done add batteries to holder and do a quick voltage test using a multimeter as shown in the picture above, in my case the battery holder uses 5 AA batteries , which means a output voltage of about 8-8.5 Volts.
Solder some breadboarding wire to the two ends of the battery holder as shown in the picture above.
Step 3: If Previous Step Is = PASS --> You'll Need the Following Electronic Components
Now if your motors are functional and you've figured out that the battery holder is a go, collect/buy the following components
- Raspberry Pi B+/ 2
- Adafruit DC & Stepper Motor HAT for Raspberry Pi
- Wireless Keyboard for the Pi- if you plan on using the keyboard as your controller for the monster truck
- Wi-Fi USB adapter - if you plan to use your tablet/mobile as the controller
- Micro SD card
- AA batteries
- Breadboarding wire to connect the Motor and battery holder to the Pi-hat
- Power bank to power the Pi
- Soldering Iron and solder
- In addition optionally if you dont have a Pi case , 3D print the part as shown in the 3D picture above
- 4X40 screws to mount the Pi to the base 3D printed part
Step 4: 3D Print the Part Attached (Optional)
Now ! since your going to be using the Raspberry Pi on a rugged moving thing called the monster truck !! you may want to protect your Pi using a case or 3D print the STL files attached above.
In my case I am using Repetier-Host as my 3D printing software with the Printrbot Simple metal, and the slicer set at 0.2mm layer height .
- Slicing the files should take about 30 seconds
- And printing the files takes about 2 hours.
As shown in the picture above the base has a lot of holes which can be used for mounting on toy truck/car.
Step 5: Setup Your Pi
Download the latest version of Raspbian from https://www.raspberrypi.org/downloads/
Based on the Laptop/Computer operating system follow the SD card setup guide at
https://www.raspberrypi.org/documentation/installa...
Once the SD card is setup ,insert it in the slot of your Pi and boot it up.
Now since your Pi is going to mounted on the the Monster Truck is a good idea to enable SSH on your PI using
pi@raspberrypi ~$sudo raspi-config
in the configuration tool go to Advanced Options-> SSH--> Enable
Also setup the Pi to boot to the desktop
And setup a static ip address for your Pi by modifying interfaces file as shown in the screen shot above
pi@raspberrypi / $ sudo pico /etc/network/interfaces
In addition to upload programs attached in the steps below you will need to install an FTP client like FileZilla as shown in the third picture above.
Step 6: Mounting the Pi and the Hat on the Monster Truck
Now using a screw driver and the screws that you salvaged from the truck mount the base on the battery holder of the truck and ten mount the Raspberry Pi on the base
Add use 4 pieces of bread boarding wire, just long enough to reach the motor wires to M2 and M3 of the motor shield( here you can connect it to M1 and M2 , if you do so, remember to modify the code in the steps below)
Connect the battery holder wired to the + and - holder as shown in the 4th picture.
Connect the wires from M2 to the back motor which rotates foward and backward
and connect M3 to the motor in the front which is used to turn left and right.
And then finally connect the Power bank to the Pi.
Step 7: Enable I2C and Install Software for the DC Motor Pi Hat
Now enable the I2C tool utility
pi@raspberrypi ~$sudo apt-get install python-smbus<br>pi@raspberrypi ~$sudo apt-get install i2c-tools
enable the I2C in rasp-config
pi@raspberrypi ~$sudo raspi-config
in the configuration tool go to Advanced Options-> I2C-> Enable
For more details refer to
https://learn.adafruit.com/adafruits-raspberry-pi-...
Follow the learning guide to download and setup the required software for the DC motor PI hat
https://learn.adafruit.com/adafruit-dc-and-stepper...
Once done navigate to /home/pi/Adafruit-Motor-HAT-Python-Library/examples
And run through the DCTest.py and run it using the command below to test the back motors are spinning.
pi@raspberrypi ~/Adafruit-Motor-HAT-Python-Library/examples $ sudo python DCTest.py
Step 8: Download and Keyboard Controller Code and Setup
Download the keyboard.py program and FTP the file to the Pi /home/Pi directory using the FTP tool called filezilla which you installed in the previous step.
Run the program using
pi@raspberrypi ~$sudo python keyboard.py
as shown in the third picture and give the keyboard controller a test as shown in the video.
Modify the block of code highlighted in the first picture to increase the speed and time interval which kind off drives the sensitivity of the keyboard as the controller.
keyboard.py program, uses key strokes which are similar to most action games that you would play on your computer, that is
- W - for foward
- S - Back
- A - Left turn
- D - Right turn
- Q - to Speed up
- S - Slow down
If you plan to take the monster truck outside, where you wont have Wi-Fi range from your router, setup the program to run when the Raspberry Pi starts up using the following command
pi@raspberrypi ~ $ sudo crontab -e
Once the file opens add the following line at the end of the file and hit ctrl+X to save.
@reboot /usr/bin/python /home/pi/keyboard.py &
Attachments
Step 9: Setup Flask - Which Will Host a Web Server on the Pi
Flask is a microframework for Python based on Werkzeug, Jinja 2.
To install flask you will need to install python-pip using
pi@raspberrypi ~ $ sudo apt-get install python-pip
once done you can now use pip to install flask
pi@raspberrypi ~ $ sudo pip install flask
Since flask is a microframework, which means you should be able to get it up an running quick and test out if your setup was successful
To do this, save the following python program to a hello.py file
from flask import Flask<br>app = Flask(__name__)
@app.route("/") def hello(): return "Hello World!"
if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True)
And to start the webserver run the hello.py file using
pi@raspberrypi ~ $ python hello.py
Now to open the website on you laptop/computer browser use the http://ipaddress-of-pi (in my case http://192.168.0.16)
For more info/documentation about Flask follow the link below
Step 10: Download the Zip Attached and Run the Python Program
Now if you are able to open the hello world webpage on you Laptop,Tablet or Phone you are ready to download the Truck-Pi.zip , extract it and upload it to the pi using Filezilla.
Then run the controller.py program in the folder
pi@raspberrypi ~/Truck-Pi $ sudo python controller.py
Now access the website hosted on your Pi using
http://ipaddress-of-Pi:800 which in my case in http://ipaddress-of-Pi:800 as shown in the screen shot of the table above.
For the latest version of the code refer to the link below, feel free to fork and remix..
Attachments
Step 11: Setting Up Web Server to Start on Reboot
Now if you are planing to take the Monster Truck to your living room still in the Wifi range of your router you will want to setup the program to run automatically when the Pi boots up, using
pi@raspberrypi ~ $ sudo crontab -e
Once the file opens add the following line at the end of the file and hit ctrl+X to save.
@reboot /usr/bin/python /home/pi/Truck-Pi/controller.py &
once done reboot your Pi using
pi@raspberrypi ~ $ sudo reboot
And in a couple of mins you should be able to test the drive by loading the website on your Phone/tablet browser as shown in the picture.
Step 12: Taking the Truck Out An' About - Setting Up Ad-Hoc Network on Pi
If you plan to take you truck out an' about to a park , you will want to setup the Pi act like your wifi router at home, so that you can connect your mobile to the Pi's Wifi and use the flask web app you just deployed as the controller for the truck.
To do this you will have to setup a Ad-Hoc network on your Pi with its own DHCP service which will server IP address to device that want to connect to it, which in this case is your mobile when your at the park.
SSH into your Pi and install the DHCP package
pi@raspberrypi ~ $ sudo apt-get install isc-dhcp-server
Once done add the following to the bottom of the dhcpd.conf, here observe we have set the Pi ip address as 192.168.1.1
pi@raspberrypi ~ $ sudo nano /etc/dhcp/dhcpd.conf
Copy paste the following details to bottom of the file
ddns-update-style interim;<br>default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; subnet 192.168.1.0 netmask 255.255.255.0 { range 192.168.1.5 192.168.1.100; }
Now make a copy of your interfaces file , which you modified in an earlier step using the command below(I like to keep a copy so that I can use the renamed file,when I am at home to connect to my home wifi router and use the Pi for other stuff.. )
pi@raspberrypi ~ $ cd /etc/network
pi@raspberrypi /etc/network $sudo cp interfaces interfacesOldStaticIp
Then modify the interface file using the following command
pi@raspberrypi /etc/network $ sudo pico interfaces
Modify the wlan0 section of the file to
auto wlan0<br>iface wlan0 inet static address 192.168.1.1 netmask 255.255.255.0 wireless-channel 1 wireless-essid TruckPiWiFi wireless-mode ad-hoc
And now you ready to reboot your Pi
pi@raspberrypi /etc/network $ sudo reboot
You should now see the new Wifi you created "TruckPiWifi" in the wifi setup section of your computer and mobile(as shown in the 4th screen shot above)
Test you setup by access the URL http://192.168.1.1:800 as shown in the video above and adjust the speeds in the code if required, before taking the truck the park..