RoboRally/docs/3_ROBOT_SETUP.md

114 lines
6.2 KiB
Markdown
Raw Normal View History

2019-07-30 15:00:26 +00:00
# Robot setup
This file explains how to setup the software for the robots. It assumes that you already assembled your robot according to the instructions in 1_ASSEMBLY.md and 2_MOTOR_SHIELD_FIX.md
Before we start, stack the D1 mini ontop of the battery shield and connect it to your computer using a micro USB cable ![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/motor_battery_stack_13.jpeg)
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/usb_connection.jpeg)
### Flash Micropython to the Wemos D1 mini
The first step is to flash the micropython firmware to the D1 mini.
You can download the latest micropython firmware for the ESP8266 from [here](http://micropython.org/download#esp8266). I used version _esp8266-20190125-v1.10.bin_
The firmware can be flashed on the D1 mini using the following commands:
```
$ esptool.py --port /dev/ttyUSB0 erase_flash
$ esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20190125-v1.10.bin
```
### Communicating with the D1 mini
After flashing the firmware we can access the python prompt on the microcontroller using a serial communication program such as picocom
On Ubuntu you can install this with
```
$ sudo apt-get install picocom
```
Connect using picocom:
```
$ picocom /dev/ttyUSB0 -b115200
```
First we enable WebREPL on the microcontroller which allows us to easily upload files and debug the robots via WiFi.
*(I will use `>>>` in order to indicate commands which should be issued in the Micropython prompt)*
```
>>> import webrepl_setup
```
-> choose enable, set a password and choose reboot
Now we set up an access point on the D1 mini:
```
>>> import network
>>> ap_if = network.WLAN(network.AP_IF)
>>> ap_if.active(True)
>>> ap_if.ifconfig() # this prints the IP
```
The default password for this access point is: `micropythoN`
Alternatively, you can also connect to an existing wifi network using:
```
>>> import network
>>> sta_if = network.WLAN(network.STA_IF)
>>> sta_if.active(True)
>>> sta_if.connect("<SSID>", "<PW>")
>>> sta_if.ifconfig() # this prints the IP
```
Joing join the same WiFi network with your PC (either the access point or the local network) and connect to the D1 mini via the [online WebREPL](http://micropython.org/webrepl):
Enter IP from above and click connect. Login with the password you set for the WebREPL.
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/webrepl0.jpg)
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/webrepl1.jpg)
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/webrepl2.jpg)
### Making settings permanent
In order to avoid having to set up the WebREPL and the WiFi everytime we reboot the robot we can upload a file called boot.py which runs each time the robot restarts:
- Find the file `boot.py` in the `micropython_firmware/` subfolder and edit the lines indicated with `# TODO`. In particular, set the wifi network, password and IP addresses. You should choose a unique IP address for each robot
```
# TODO: edit these lines
network_name = 'Your WiFi network' # existing wifi network to connect to (leave empty if unused)
password = 'Your password' # password for the network
desired_ip = '192.168.1.101' # the robot will be reachable by this IP in the network
subnet = '255.255.255.0'
gateway = '192.168.1.1'
dns = '192.168.1.1'
# TODO end edit
```
- Upload boot.py via WebREPL
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/webrepl3.jpg)
- Now the robot will automatically connect to WiFi network you set and start a WebREPL server after reboot. *Note: If the WiFi network cannot be found it will instead open an access point*
### Upload robot firmware
Next, upload the motor controller script to the microcontroller. At the moment the firmware consists of the following three files located in `micropython_firmware/`:
- `main.py` Main script handling network communication and control
- `d1motor.py` Interface for the Wemos D1 mini motor shield
- `l293dmotor.py`Interface for alternative motor driver using L293D (no longer used)
You can upload these files the same way you did before within the WebREPL terminal. After uploading all the files reboot the microcontroller by pressing the reset button or by pressing `Ctrl+D` in the WebREPL prompt.
### Test the robot
Now it's time to test if everything is working fine. Disconnect the USB cable from the microcontroller and connect the battery to the battery connector
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/battery_connection.jpeg)
Next, connect the two motors of the robot to the motor shield according to the following sketch:
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/robot_bb.jpg)
![](https://imaginaerraum.de/git/Telos4/RoboRally/raw/branch/master/docs/images/battery_connection_overview.jpeg)
Test if you can reach the robot using
```
$ ping 192.168.1.101
```
*Note: Make sure you use the correct IP address for your robot.*
If everything worked, connect to the robot via WebREPL as before. Don't worry if the prompt does not appear to be working. The robot is running a program waiting for incoming connections for remote control.
We can connect to the robot via a socket on port `1234`. There is a simple demo program which illustrates how to control the robot remotely via keyboard. The program uses `pygame` for input handling so make sure to install it using
```
$ sudo pip install pygame
```
The program is located in the `remote_control/`. Run it using
```
$ python keyboard_controller.py '192.168.1.101'
```
When running the program you should see an output in the WebREPL that a connection was established.
You can use the arrow keys for sending commands to the microcontroller which it then passes on to the motors.
Now you're all set up for controlling your robot remotely via wifi.
You can control the robot by send commands to the robot as a string containing `'(u1, u2)\n'`, where `u1` and `u2` are floats in the range `[-1.0, 1.0]`.
Have a look at the `keyboard_controller.py` script to see how this works in python.
The next step is to setup the position detection using ROS. For this see the ROS_SETUP.txt