Step 2: Drive - smellslikeml/LitterBug_donkey GitHub Wiki

Overview

This guide will help you configure your LitterBug to drive and train. Some steps will borrow from the donkeycar docs. When you're finished, you'll be able to drive and train your Litterbug anywhere.

Raspbian image

The LitterBug uses libraries like tensorflow, opencv, etc to operate. The best place to start is to use the donkeycar raspbian image with these libraries already built-in.

First, download the image here. We suggest using a tool like etcher since it provides an easy and safe way to write the image to your microSD card. Select the image you downloaded earlier, select your drive (microSD card), and flash. This should take a few minutes.

etcher

In case etcher has unmounted your drive after it is done, briefly remove the microSD card and plug back into your computer to read. On the drive, open the boot directory. Make a new empty file called ssh and save it in this directory. Next, make another new file called wpa_supplicant.conf and paste the following:

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

Update the ssid and psk variables with your network name and password. This will help you ssh into your rover. Install your SD card in your rover's raspberry pi, power up, and wait for it to boot- ~1 minute or so. You'll need to find the ip address for your pi to ssh in. Our favorite way to find that is using nmap! If you're running windows, here are some instructions to install nmap. If you're running OSX you can simply run in a terminal:

brew install nmap

And for Linux (Debian):

sudo apt-get install nmap  

After it has installed, in the terminal run:

nmap -sP 192.168.0.1/24 

or

nmap -sP 192.168.1.1/24

depending on your local network. This will scan your local network and return a list of ip addresses for all the your connected devices. Look for the ip that matches a raspberry pi- this will be the ip you'll use to ssh. Try to ssh into your pi:

ssh pi@<YOUR-PI-IP>

Log in with the default password- "raspberry". Congrats! You've successfully connected to your pi and you're ready to move to the next step.

Configuring & Installing dependencies

Let's start by configuring some settings on the pi. On the pi, run:

sudo raspi-config

This will send you into a blue background, gray box screen with various settings. Set the following settings:

  • Change the password: Scroll to the first option Change User Password, press enter, enter a new password
  • Enable pi camera: Scroll to Interfacing Options, choose P1 Camera, confirm yes
  • Enable I2C: Scroll to Interfacing Options, choose P5 I2C, confirm yes
  • (Optional for GPS module) Enable Serial: Scroll to Interfacing Options, choose P6 Serial, select No on enabling the login shell, select Yes on enabling serial port hardware.

After this, return via the option and select .

Installing dependencies

We need to install some software to help interface the PS3 controller with your pi. Follow these instructions to get your controller set up. First, install some dependencies:

sudo apt-get install bluetooth libbluetooth3 libusb-dev
sudo systemctl enable bluetooth.service
sudo usermod -G bluetooth -a pi
sudo reboot

Plug in the controller via USB to your pi. Ssh back into the pi, hit your controller's PS logo button and continue with the following:

#in the home directory, run

wget http://www.pabr.org/sixlinux/sixpair.c
gcc -o sixpair sixpair.c -lusb
sudo ./sixpair

The last line will return a line that reads something like:

Setting master bd_addr to XX:XX:XX:XX:XX:XX

Copy that bd_addr as you'll need it in the next step Now you'll use bluetoothctl to pair up your controller:

bluetoothctl
agent on
devices
trust <bd_addr>
default-agent
quit

Now disconnect your controller from your pi and hit the PS logo button again. Your controller should now be paired to your pi! To check that everything is working correctly, run the following to make sure the controller name exists.

ls /dev/input/js0

If you run into any issues, the original donkeycar controller instructions have some more details.

Donkeycar dependencies

Go ahead and clone this repo onto your home directory and cd into it.

cd
git clone https://github.com/mayorquinmachines/LitterBug_donkey donkeycar
cd donkeycar

Move a few directories and files to your home directory

mv car/* ../
rmdir car

now run

cd
pip install -e .[donkeycar]

This will install our modifications to the original donkeycar library that work for the LitterBug. The mycar directory contains all the files your rover needs to drive and train.

The start scripts contains files to help start your car without having to ssh in and run scripts. The sensor_scripts directory and sensors database are optional files you can use if you've added extra sensors to log their data.

If you want to setup your car so that you don't need to ssh in, you can edit your crontab and add this file to start at boot.

#open up crontab 
crontab -e
#add this line
@reboot /home/pi/start/main.py

(Optional) If you've included the gps module, go ahead and run the following. Otherwise skip it.

sudo apt-get install gpsd gpsd-clients
sudo killall gpsd
sudo gpsd /dev/serial0 -F /var/run/gpsd.sock
pip install gps

If you run into any errors, read adafruit's installation instructions for more details.

Add the following lines to your crontab like earlier to collect data from various extra sensors:

@reboot /home/pi/sensor_scripts/gps.py

#for 10DOF sensor
* * * * * /home/pi/sensor_scripts/gyro_accel_bmp.py
* * * * * sleep 15; /home/pi/sensor_scripts/gyro_accel_bmp.py
* * * * * sleep 30; /home/pi/sensor_scripts/gyro_accel_bmp.py
* * * * * sleep 45; /home/pi/sensor_scripts/gyro_accel_bmp.py

#for motion sensor
0 20 * * * timeout 8h /home/pi/sensor_scripts/motion.py

Setting up pi network

The LitterBug is designed to rove outdoors so we can't rely on a local network to connect to it. Therefore as a final setup step, we'll configure the pi as an access point in a standalone network. For more detailed explanations of every step, follow this official guide. Otherwise, follow along these next steps:

Updating your pi and installing software:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dnsmasq hostapd

Pause the service and reboot:

sudo systemctl stop dnsmasq
sudo systemctl stop hostapd
sudo reboot

Ssh back into your pi and with your favorite editor, open the file /etc/dhcpcd.conf and add the following lines:

interface wlan0
    static ip_address=192.168.4.1/24
    nohook wpa_supplicant

Save and restart the service

sudo service dhcpcd restart

Open the file /etc/dnsmasq.conf, delete everything in the file and add:

interface=wlan0      # Use the require wireless interface - usually wlan0
  dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h

Save and close. Open a new conf file /etc/hostapd/hostapd.conf and add:

interface=wlan0
driver=nl80211
ssid=LitterBug
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=<ADD-A-PASSWORD-HERE>
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Replace <ADD-A-PASSWORD-HERE> with your own password. Save it and close. Next, edit the file /etc/default/hostapd, find the line that starts with #DAEMON_CONF and replace it with:

DAEMON_CONF="/etc/hostapd/hostapd.conf"

Save and close.

Restart services paused earlier:

sudo systemctl start hostapd
sudo systemctl start dnsmasq

Edit the file /etc/sysctl.conf and uncomment the line that reads:

net.ipv4.ip_forward=1

Configure ip tables:

sudo iptables -t nat -A  POSTROUTING -o eth0 -j MASQUERADE
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

Edit the file /etc/rc.local and above the exit 0 line add:

iptables-restore < /etc/iptables.ipv4.nat

Finally, reboot!

sudo reboot

Now your pi will have its own wifi network for which you can connect to from your computer, tablet, phone etc. wherever. The network name will show up as LitterBug and the password will be whatever you chose earlier. We recommend to use the Termius app as it provides a handy terminal for mobile devices.

Driving your rover

You have two options to start driving your rover:

  • ssh into the terminal and run driving scripts
  • pair your controller via the PS logo button and pressing select button

To ssh in and start the vehicle:

If you chose not to set up your crontab to start your controller at reboot, you can do the following. Connect to the LitterBug wifi network and ssh into your pi using its new static ip:

Press the PS logo button to pair your controller. Then, run the following commands:

screen      #press enter after running command
cd mycar
python manage.py drive --js

This will take a few minutes to start. You'll hear your donkeycar beep and you can start wiggling the joysticks to test your scoop and steering are moving. This means you're ready to start driving!

wiggle car

To start car via controller:

Make sure your crontab includes the line

@reboot /home/pi/start/main.py

Press the PS logo button to pair your controller. Now you'll be able to press the select button and it will execute the manage.py file like shown above. You'll see a green flash to indicate the rover has started. It will take a couple minutes for the rover to setup. Again, you'll head the donkeycar beep and you can start wiggling the joysticks to test movement. You can start driving now!

Controls

We've configured the controller to give you as much control as possible. Here is a guide to what each mapped button can do:

  • Left stick - steering (left-right)
  • Right stick - scoop (up-down)
  • R2- throttle (unpressed=full throttle forward, mid-pressed= stop, full-pressed=full reverse)
  • Triangle - Increase max throttle
  • X - Decrease max throttle
  • Circle - Toggle recording (disabled by default. auto record on throttle is enabled by default)
  • dpad up - Increase throttle scale
  • dpad down - Decrease throttle scale
  • dpad left - Increase steering scale
  • dpad right - Decrease steering scale
  • Start - Toggle constant throttle. Sets to max throttle (modified by X and Triangle).
  • Select - Start rover for training (press again to stop training)

It takes a little while to get a hang of all the controls, so practice before you start collecting data. You can change the max throttle and max steering and scoop angles by editing the config file /home/pi/mycar/config.py. When you are ready to collect samples to train a trashpilot, you can also go back to that config file /home/pi/mycar/config.py and change the AUTO_RECORD_ON_THROTTLE variable to True. This will record training data as soon as you press the throttle button when starting your rover.

Collect lots of training samples in an environment you plan to deploy your rover and read the next step to learn how to train your first trashpilot!

⚠️ **GitHub.com Fallback** ⚠️