Template: AlphaBot2-Pi Quick Start

From Waveshare Wiki
Jump to: navigation, search

Quick start

Extract the package AlphaBot2.tar.gz and place it to the directory of /home/pi under raspbian system.

Before using this extension board, you should install the library python to your Raspberry Pi.

In case that the example is unable to be performed after installing the library, you can try to apply the command chmod +x filename to grant the program the executable permission.

Explanation: For the freshmen of Raspberry Pi, please refer to the Raspberry Pi Tutorial Series. You can optionally read the following content.

Motor testing

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python
sudo python AlphaBot2.py 

Expectation result: The smart robot moves forward when the program runs and drives the motor to rotate.

In case that the direction of motion is wrong, you may need to modify the motor wring or change the pin settings in your program. We recommend the latter one. If left wheels of the robot run in a wrong direction, you should exchange the settings of ain1 and ain2. For right wheels, exchange the settings of bin1 and bin2.

def __init__(self, ain1 = 12, ain2 = 13, ena = 6, bin1 = 20, bin2 = 21, enb = 26):

5-way joystick

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python Joystick.py 

Expectation result: This Joystick has 5 control directions: Up, Down, Left, Right and Center (Enter). Open the serial monitor, and set the baud rate to 115200. Then, the monitor displays the current key press. When there is a key pressed down, the buzzer sounds and the motor rotates in the direction according to the key. If the wheel(s) runs in the wrong direction, you should modify the settings of the corresponding pins.

Alphabot2-pi-manual-1.png

Infrared remote control

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python IRremote.py 

Expectation result: You can control the smart robot by pressing down the numeric keys on the infrared remote controller, in which, “2”is for go forwards, “8” for go backwards, “4” for turn left, “6” for turn right, and “5” for stop. Keys “-“ and “+” can adjust the speed of the robot, and “EQ” can restore the default setting of the speed.

(Note: The key encoding may change with different Infrared remote controller. You can modify the program as required.)

Infrared obstacle avoidance

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python Infrared_Obstacle_Avoidance.py 

When there is no obstacle ahead of the robot, the green LED on the robot is off. When the obstacle is detected, the green LED will light up. In case that the LED is always off or always on, you can try to adjusted the potentiometers on the bottom of the robot to make the LED work properly.

Expectation result: The robot goes straight when there is no obstacle detected by the sensor on the left, and turns right when obstacle is detected.

Ultrasonic distance measurement

Note: Raspberry Pi 3B kit does not contain ultrasonic unit

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python Ultrasonic_Rangeing.py 

Expectation result: The terminal will display the current measured distance.

Alphabot2-pi-manual-2.png

Ultrasonic distance measurement

Note: Raspberry Pi 3B kit does not contain ultrasonic unit

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python Infrared_Obstacle_Avoidance.py 

Expectation result: The robot goes straight when there is no obstacle detected by the sensor on the left, and turns right when obstacle is detected.

RGB LED

Copy the file rpi_ws281x-master.zip into your Raspberry Pi, and run the following command to extract the file.

unzip rpi_ws281x-master.zip

Install the depended-upon software.

sudo apt-get build-essential python-dev install scons swig

Compile the program.

cd rpi_ws281x-master
sudo scons

Alphabot2-pi-manual-3.png

Run the following command to perform the testing, you can see the RGB LED flickering.

sudo ./test

Enter the commands below to install the library python.

cd python 
sudo python setup.py install

Run the library python for testing, you can see the RGB LED flickering.

cd examples
sudo python lowlevel.py

Now, the library has been installed.

Enter the following commands under the terminal:

cd ~/AlphaBot2/python 
sudo python ws2812.py 

Expectation result: The four RGB LEDs on the bottom of the smart robot will be in red, green, blue and yellow, respectively.

If they display the wrong color, you can find out the file /boot/config.txt and add the lines below into it. Restart the module to make the lines take effect. The RGB LED use DMA control, which can occupy the audio output DMA channels on the Raspberry Pi. So, adding these lines may cause the earphone interface to be unavailable.

hdmi_force_hotplug=1
hdmi_force_edid_audio=1

Tracker sensor testing

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python TRSensors.py 

Expectation result: You can see five set of data printed on the terminal, they are corresponding to five sensors. When the smart robot is placed on the white KT board, the data value will be in the range of 800~900. When it is held in the air, the value can be in the range of 0~100. And, when it on the black line, the value can be in the range of 100~300.

Infrared line tracking

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python LineFollow.py

Expectation result: While the program is running, the smart robot goes into the calibration mode, moving to left and then right. When the calibration finished, you can swing the smart robot side to side, and the terminal will display the current position of the black line and the values of five sensors. Place the smart robot on the black line, and press down the center key on the joystick. Now, you can see the smart robot run along with the black line, and the RGB LEDs on the bottom of the robot display different colors.

Servo

Enter the Linux terminal, and input the following commands:

cd ~/AlphaBot2/python 
sudo python PCA9685.py

Expectation result::The servo will move to left and right, and its rotational range can reach 180 degree.

mjpg-streamer software real-time monitoring

"MJPG-streamer" takes JPGE from the web camera, and streams them over an IP-based network to various types of viewers. a) Run the command raspi-config to start up the camera before using it:

sudo raspi-config 

Select Enable Camera

b) For the camera with CSI interface, the system may be unable to find out the device under the directory of /dev/video0. In this case, you should add a line, bcm2835-v4l2, into the file /etc/modules.

sudo nano /etc/modules 

Add the line: bcm2835-v4l2

c) After reboot, the system will load the module name stored in this file, and find out the device video0 under the directory of /dev. (Please take a notice that the letter l in the command bcm2835-v4l2 is the lower case of L.)

Explanation: After performing the steps a), b) and c), if you still cannot find out the device video0 under the directory of /dev, and all the operations and commands are correct, you should run the command: rpi-update.

The software mjpg-streamer in the program has been compiled. You only need to run the following lines to start up the mjpg-streamer:

cd AlphaBot2/lib/mjpg-streamer
sudo ./start

Then, open the browser, and enter the IP address of RPi and the port number: 8080 into the address bar. (Notices: the port number here is 8080, other than 8000 what we used before.) The website will show the information about the M-JPEG streamer. Click the option Stream, it will real-time display the image captured by the camera.

RPi-Motor-Driver-Board-Manual-3.png

Under the terminal, pressing the keys Ctrl + C can end the program. However, we recommend you to keep the program running, since we still need it in the following operations.

Remote control via webiopi

Webiopi is a web framework that can control GPIO of RPi. Here is the website: http://webiopi.trouch.com/

However, this software is no longer to be updated after the version 0.7.1. The software here we provided is the improvement version, able to support Raspberry Pi 3B.

To install the webiopi, you should open a new terminal, and keep the route described above running.

cd AlphaBot2/lib/WebIOPi-0.7.1-raspi2
sudo ./setuo.sh

Alphabot2-pi-manual-4.png

Check if the library is installed successfully by running the line webiopi –h. if the library has been installed, you can see the interface as the figure shows below:

Alphabot2-pi-manual-5.png

Enter the terminal, and run the lines:

sudo webiopi -d -c /etc/webiopi/config 

Alphabot2-pi-manual-6.png

Then, open the browser on your PC or cell phone, and enter the IP address of RPi and the port number: 8000 into the address bar. For example, input http://192.168.1.16:8000 (the address can be changeable according to your actual condition). To login the website, you should enter your account and password. The default account name is webiopi, and the original password is raspberry. When finished the steps above, if you can enter the webpage WebIOPi Main Menu, it means the environment is configured well.

Alphabot2-pi-manual-7.png

Click the option “GPIO Header”, then, you can see the interface below. In this webpage, you can control the level of the GPIO.

Alphabot2-pi-manual-8.png

Enter the directory of mjpg-AlphaBot, and modify the video source IP.

cd ~/AlphaBot2/mjpg-AlphaBot/
sudo nano index.html

Find out the link: http://192.168.10.235:8080/?action=stream

In here, 192.168.10.235 is the IP address of RPi, and it can be changeable according to your actual condition. The port number should be set to 8080 (This port is used for transmitting stream). Start up the Raspberry Pi camera program:

cd ~/AlphaBot2/mjpg-AlphaBot 
sudo webiopi -c config 

Then, open the browser, and enter the IP address of RPi and the port number: 8000 into the address bar. (Notices: the port number here is 8000, other than 8080.) The image captured by the camera will be shown in the website. Clicking the direction icon on the website can control the action of the robot.


Alphabot2-pi-manual-9.png

If you cannot enter this webpage, we recommend you to use the Chrome browser or the Firefox browser. Furthermore, please make sure the current directory of Raspberry Pi is:

~/AlphaBot2/mjpg-AlphaBot/mjpg-AlphaBot

since there is a file index.html under this directory. And then, input the code:

sudo webiopi -c config

to enter the index.html. Or else, you will go to the WebIOPi Main Menu webpage.


Configure boot-up option: Replace the file config

cd ~/AlphaBot2/mjpg-AlphaBot 
sudo cp config /etc/webiopi/config

Run the command below to set the webiopi to be started up on boot.

sudo update-rc.d webiopi defaults

Open the file /etc/rc.local with your editor to add the following commands:

sudo vi /etc/rc.local

Add the commands following the comments. Please make sure the line exit 0 is the last line. And then, save the modification and exit.

cd /home/pi/AlphaBot2/lib/mjpg-streamer 
sudo ./start.sh &

Remote control via Bottle

Bottle is a simple and effective micro python Web framework complying with WSGI. By Bottle, you can fast and easily implement Web control. 1) Install the library:

sudo apt-get install python-bottle

2) Perform web control RGB LED via Bottle

cd ~/AlphaBot2/Web-RGB
sudo python main

3) Then, enter the IP address of your Pi and the port number 8000 into the address bar of the browser. Clicking different position of the pallet, you can see RGB LED displays different colors. There are 3 display modes selectable, static, breath and flash.

(Note: This web-RGB program only support the mobile phone browser. It is unavailable for PC browser.)

Alphabot2-pi-manual-10.png

To implement web video control the smart robot by Bottle, you should modify the IP address for the video stream.

cd ~/AlphaBot2/Web-Control
vi index.html

Find out the line below, and change the IP address to the actual RPI address and set the port to the stream port.

<img width="320" height="240" src="http://192.168.10.235:8080/?action=stream">

Then, run the following command to start the program:

sudo python main

Then, open the browser, and enter the IP address of RPi and the port number: 8000 into the address bar. (Notices: the port number here is 8000, other than 8080.) Then, you can see the following webpage. Clicking the direction icon on the website can control the action of the robot.

Alphabot2-pi-manual-11.png

You can set the program to be started up on boot. Open the file /etc/rc.local with your editor and add the following line into it.

sudo vi /etc/rc.local

Add the commands following the comments. Please make sure the line exit 0 is the last line. And then, save the modification and exit.

cd /home/pi/AlphaBot2/lib/mjpg-streamer 
sudo ./start.sh &
cd /home/pi/AlphaBot2/Web-Contrl
sudo python main &

Since the Raspberry Pi uses dynamic IP which may change each time the device restart, you can add the following lines under the directory of /etc/dhcpcd.comf to set the static IP:

interface wlan0
static ip_address = 192.168.1.114/24
static routers = 192.168.1.1

In here, “192.168.1.1” is the router IP address, and “192.168.1.114” is the IP address to be set, following which the“/24” should not be removed.

Remote control via software

To implement video control the smart robot via the software on your mobile phone or PC, you should modify the IP address in the program firstly.

cd ~/AlphaBot2/App-Control
vi main.py

Find out the line below, and change the IP address to the actual RPI address and set the port to the stream port.

Host = '192.168.10.235'

Then, run the following command to start the program:

sudo python main

Open a new terminal to run the mjpg-streamer.

Run the program AlphaBot.exe on your PC, and enter the IP address, the port numbers of mjpg-stream and smart robot Soket sever.

Click the button Viedeo Connet to connect to the video. When the connection is built up, the button will turn to green. Then, click the button Cmd Connect to connect to the smart robot sever. You will see the button turn to green, if the connection is successful. Now, you can control the action of the smart robot by pressing down the keys on the left, and control the camera and steer by the keys on the right.

Alphabot2-pi-manual-12.png

Then, scan the QR code below with your mobile phone, and download the relative APP.

Alphabot2-pi-manual-13.png

Alphabot2-pi-manual-14.pngAlphabot2-pi-manual-15.png

Enter the actual IP address of RPi, and click the button Connect. Then, you can see the images captured by the camera. Next, click the button Connect under the image. When the control port has been connected to the program, this button will change from green to blue. Now, you can control the actions of the smart robot and the steer by pressing down the corresponding keys. By dragging the slider, you can change the speed of the robot.

Open the file /etc/rc.local with your editor to add the following commands:

sudo vi /etc/rc.local

Add the commands following the comments. Please make sure the line exit 0 is the last line. And then, save the modification and exit.

cd /home/pi/AlphaBot/lib/mjpg-streamer 
sudo ./start.sh &
cd /home/pi/AlphaBot/App-Contrl
sudo python main &

You should connect the Raspberry Pi to the router at every restart. Of course, you can enable the AP function of the RPi wireless network adapter, taking the RPi as sever. For more information about the operation, please refer to the Raspberry Pi tutorial.

Remote control via RPi Bluetooth

1) Upgrade the relative packages of the Bluetooth, and install the depended-upon software.

sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get install pi-bluetooth bluez bluez-firmware blueman

2) Add the pi user to the Bluetooth group.

sudo usermod –G Bluetooth –a pi

3) Restart the Raspberry Pi.

sudo reboot

4) Start/Add the SPP, and turn on the Bluetooth device.

sudo vi /etc/systemd/system/dbus-org.bluez.service

Modify two lines in the following file:

Alphabot2-pi-manual-16.png

After restarted the RPi, enter the command hciconfig (similar to the command ifconfig) to check the Bluetooth service.

Alphabot2-pi-manual-17.png

If the Bluetooth has been working, you will see the device hci0 displayed on the screen. When the Bluetooth does not work, no Bluetooth device can be identified.

Note: If the line dtoverlay=pi3-miniuart-bt exists in the file /boot/config.txt, it should be commented out.

5) Connect to Bluetooth device Input the following command to enter the Bluetooth control interface (you can input the command help to check the command list after entered the interface).

sudo bluetoothctl
agent on

After entered the interface, input the following lines (you can input the command help to check the command list after entered the interface).

agent on
default-agent

Following the scan on, you can see the physical address of the scanned Bluetooth device, similar to

XX:XX:XX:XX:XX:XX.

Alphabot2-pi-manual-18.png

For Bluetooth pairing, use the following command (In here, XX:XX:XX:XX:XX:XX can be replaced to your device address. For Android mobile phone, you can check the Bluetooth address by Setting-> About phone-> Status information)

pair XX:XX:XX:XX:XX:XX

Alphabot2-pi-manual-19.png

Alphabot2-pi-manual-20.png

When pairing is successful, add the device to the trust list.

trust XX:XX:XX:XX:XX:XX

Alphabot2-pi-manual-21.png

Next, connect to the device.

connect XX:XX:XX:XX:XX:XX

Alphabot2-pi-manual-22.png

At last, enter the command quit to exit the interface. After the settings above, as the Bluetooth device is turned on, the Raspberry Pi will automatically connect to it.

Note: If you are unable to build up the connection with the command line, you can enter the graphic interface to make the connection.

Alphabot2-pi-manual-23.png

Click the Bluetooth icon and add new device.

Alphabot2-pi-manual-24.png

Select the corresponding Bluetooth device. Please make sure the Bluetooth on the mobile phone should be enable and set to discoverable.

Alphabot2-pi-manual-25.png

On the RPi, select the option OK for pairing. And at the same time, on the mobile phone, click the option Pair.

Alphabot2-pi-manual-26.png

Then, you may see the prompt saying that “Paired successfully, but this device has no services which can be used with Raspberry Pi.”

Click OK.

Now, the connection has been built up.

Run the following commands to start the program:

cd /AlphaBot2/Bluetooth-Control
sudo rfcomm watch hci0 1 python bluetooth.py

Then, scan the QR code below with your mobile phone, and download the relative APP. (This is the software for controlling the Dual-mode Bluetooth module. If you has installed this software in your Arduino mobile phone, you can skip this step)

Alphabot2-pi-manual-27.pngAlphabot2-pi-manual-28.png

Start the APP, and click the option Scan. (Note: please make sure the Bluetooth on your mobile phone is turned on) Normally, you can see the relative Bluetooth devices in the list. Then, select the option “raspberrypi”. Go to the next page, and select the option Remote control.

Alphabot2-pi-manual-29.png Alphabot2-pi-manual-30.png

If the Bluetooth of the Raspberry PI can not be searched, you can run the following lines to enable the Bluetooth and set it to discoverable.

Alphabot2-pi-manual-31.png