Friday, July 21, 2017
Home » Flowstone Workshop » Flowstone Workshop 16

Flowstone Workshop 16

Flowstone-HeaderFlowPaw & Raspberry Pi

FLOWSTONE WORKSHOP 16-1Welcome to the FlowStone Workshop number 16, where we give a beginner’s guide to computer programming using the FlowStone graphical programming language & FlowBotics Studio. In this issue we are going to look at combining different technologies to make a DIY autonomous roving robot and different ways you can program the same hardware. We will be using the new FlowPaw board from DSPRobotics, Raspberry Pi B+, Python, Http, Wifi, and, of course, FlowStone.

FLOWSTONE WORKSHOP 16-2

The PawPi Rover.

THE ROVER

For our rover we are going to use four-wheel drive with four geared motors. This will be controlled ‘Tank Style’ where the steering comes from turning the wheels on each side in different directions which simplifies the whole steering mechanism. We will also need some form of power source and in this case we are using a 7.4V Lithium battery but anything above 6V would work.

MOTOR CONTROLLER

To control the speed of an electric motor you need a Motor Controller, these work by using Pulse Width Modulation (PWM) and switching the motor on and off at a very high speeds (> 10Khz). The amount of ‘On’ vs ‘Off’ is called the Duty Cycle and can vary from all ‘Off’ to all ‘On’ and everything in between. So for example a duty cycle of 50% would, for a given pulse, be on for 50% and off for 50%. In our case we are using a very simple (L298N) motor controller that takes a PWM signal in at low current and outputs the same signal pulses but with a high current capacity for the motor. We need a two-channel controller to control each pair of wheels that are wired in parallel.

VOLTAGE REGULATOR

Since our electronics (FlowPaw & Raspberry Pi) run off 5V we will also need a small voltage regulator to drop the battery voltage to a steady 5V. For this we used a small RC 5V regulator.

FLOWPAW

We are using the new FlowPaw board from DSPRobotics to take in control signals (i.e. turn left, forward etc.) and convert these into the PWM signals required by the motor controller. To do this we are using a special (secret) mode called Rover mode. This is achieved by setting the RC servo speed for the FlowPaw at 100 (usually 0-99), this then tells the FlowPaw firmware to output motor PWM from servo pins 3, 4, 5, & 6. The other servo pins (0, 1, 2 & 7) will still work to control standard RC servos if you want to use any. All of the other functions on the FlowPaw (displays, sensors, buzzers, DIO, analog ins, etc.) will still work as normal and can be used to pimp your rover!

FLOWSTONE WORKSHOP 16-3
The FlowPaw with Claws!

RASPBERRY PI

Raspberry Pi is a small low cost Linux PC board and can be used as a standalone controller usually programmed using Python. It also has USB ports available to connect to the FlowPaw and also a Wifi dongle connected in our case. Over 2 Million Raspberry Pi boards have been sold to date and to be honest it’s not the easiest way to start learning to code, but it’s cheap and with a little help can be quite useful. One thing to note is that the early boards didn’t conform properly to the USB standard as far as supplying power through the USB ports. This was partially fixed with the new B+ version that we are using. It’s not to say that the old boards won’t work but you may have to use a self-powered USB hub if you run into power problems.

FLOWSTONE WORKSHOP 16-4
The Raspberry Pi model B+.

ROVER SENSORS

We are going to use a combination of infrared distance sensors to detect objects in the way of the rover so it can navigate around them. These come in two main forms; analog ones (from Sharp) that give out a voltage proportional to the detected object distance, and cheaper digital ones that just output a ‘one’ when an object is detected.

FLOWSTONE WORKSHOP 16-5
The Shape IR Distance Sensor.

RASPBERRY PI + FLOWPAW – STANDALONE

The first way we are going to look at programming our rover is using the Raspberry Pi as the brain and making a standalone solution. This works by programming the Pi using Python and sending the control data to the FlowPaw via USB. This simplifies the Pi programming as most of the clever stuff like the motor PWM is taken care of on the Flow- Paw.

In order to communicate with the FlowPaw you will need to use a USB library for Python called PiUSB. One thing to remember is that the Pi isn’t very powerful as far as CPU power is concerned; it is small and cheap, after all. We made a special Pi Mode (#Q) on the FlowPaw to reduce the data flow to help with the CPU power. Usually the Paw sends out masses of data back to the PC hundreds of time a second. Unfortunately, the Pi can’t process this amount of real-time data so for the Pi mode, we made it such that you send a message string and receive one back. It, via this process, essentially handshakes

The FlowPaw uses the HID (Human Interface Device) protocol to communicate, the same as any games controller. It is formatted as a 64 byte hex string in and out. The data within this string is defined by the FlowPaw protocol, which is quite simple. Usually: Function code, followed by the data, sent as a Hexadecimal value. For example to turn on LED1 on the FlowPaw you need to send #L01 (234C01 in hex, where the 234C is the Ascii code for #L).

Here are a few common code examples:

LED1 On: #L01 (234C01 in Hex)

LED1 Off: #L00 (234C00 in Hex)

Pi Mode: #Q (2351 in Hex)

Motor Speed (Rover Mode) 100: #P0100 (23500064 in Hex)

Motor 3 50% Duty Cycle: #S030500 (23530301F4 in Hex)

Here’s what this would look like in Python (e.g., turn all LEDs On):

import os

import sys

import time

import usb.core

import usb.util

packet_len=64

dev = usb.core.find(idVendor=0x1234,

idProduct=0x0001)

def pack_request(*arguments):

packet = [0x0] * packet_len

i = 0

for arg in arguments:

packet[i] = arg

i += 1

return ‘’.join([chr(c) for c in packet])

if dev is None:

raise ValueError(‘Device not found’)

try:

dev.detach_kernel_driver(0)

print(“detached”)

except:

True

dev.set_configuration(1)

piMode = pack_request(0x23,0x51) # #Q =

PiMode only send when sent to

#hex data for leds

led1=pack_request(0x23,0x4c,0x01)

led2=pack_request(0x23,0x4c,0x02)

led3=pack_request(0x23,0x4c,0x04)

led4=pack_request(0x23,0x4c,0x08)

ledsOn=pack_request(0x23,0x4c,0x0f)

ledsOff=pack_request(0x23,0x4c,0x00)

#Setup Pi Mode

dev.write(1,piMode,0) #set FlowPaw into Pi

Mode

test=dev.read(0x81,64)

#LEDs On

dev.write(1,ledsOn,0) #Leds On

test=dev.read(0x81,64)

print(“done”)

As you can see it’s not so easy, but quite achievable if you know a bit about Python. Luckily, there is a better way!

RASPBERRY PI + FLOWPAW + FLOWSTONE

One of the difficulties with programming a Raspberry Pi as a standalone device is that every time you want to make a change to the Python code you have to grab the rover plug in the keyboard, mouse and monitor. Then, Re-boot the Pi, wait for it to load, and then go about changing your code. This then becomes a very slow process. A better way to use the technology is to use the Pi as a wireless translator, using a cheap USB Wifi dongle on the Pi!

This way, you can program your rover on your PC in real-time using FlowStone and use Wifi to send the commands to the Raspberry Pi via Http and let the Pi translate these into USB commands for the FlowPaw. You never even need to look at a line of Python, and use a simple pre-loaded SD card Pi image that just boots directly into our Python script that does the translation for you. This logs onto your wifi network and looks for messages to send to the Flow- Paw – simple.

In order to do this you need to set the FlowPaw into HTTP mode and give it the IP address of the Raspberry Pi. This is done using the ID input on the FlowPaw module and entering your IP address there. This then sends/receives the FlowPaw messages over the network instead of using the PC USB port. This is a really neat use of technology as there are so many Raspberry Pi’s lying around doing nothing!

SO WHAT CAN WE PROGRAM?

Once you have comms via http and the Wifi network to your Pi + Paw rover you can then start on with the writing of your Flow- Stone code to bring it to life.

FLOWSTONE WORKSHOP 16-6
The FlowStone Rover Code.

Step1: First, let’s get the rover driving around from a remote control like an Xbox controller. This way you can use the joysticks on the games controller to drive your rover like a remote controlled car. To do this you need to use the FlowStone Xbox module and scale the joystick outputs (usually +/- 1) to match the servo inputs for the wheels (0-1000). The way the motor controller works is that it has two inputs for each controller; if the pulse width is equal on both then the motor is stationary. If input 1 is > input 2 then you have forwards motion, if 2 < 1 you have reverse motion.

So, using FlowStone we have made a project that sends 500 to both wheel inputs (Left = S3&S4, and Right = S5&S6 on the FlowPaw module) when the Xbox is in neutral. Then when you want to move forward it sends a max of ‘1000’ to input 1 and ‘0’ to input 2 for forward motion etc. (see FlowStone example graphic).

Step2: Next, add some AI (Artificial Intelligence) to our rover by adding some proximity sensors. The easiest way to do this is by adding some Sharp IR distance sensors to the front and sides. These give out an analogue voltage 0-3.3V proportional to the distance of any object detected. Connecting these into the FlowPaw analogue ins then sends this information over the airwaves back to FlowStone where you can then program some decisions, etc.

A simple decision can be ‘if the right side sensor on the rover detects an object slow down the opposite wheels to steer away’, or if an object is detected in front of the rover reverse and turn 90 degrees’. These programming decisions are very easy to implement in FlowStone and you have the choice of using the basic FlowStone building blocks or programming in the Ruby Programming language within FlowStone.

Step3: Now you can start using the power of FlowPaw and add some Claws! These can perform other tasks; for example, there is the buzzer that can beep to tell you of collisions detected, or the 8×8 Led matrix display that can write scrolling messages, the Speak- Up Click that can do voice recognition so you can command your robot, or you can use the accelerometer to detect gradients and increase the power, etc. etc.! There are over 20 Claws to choose from and more coming online all of the time.

CONCLUSION

This is a great application for Raspberry Pi, and using FlowPaw makes the hardware interface much easier than using the Pi’s own GPIO. You have two programming options, to use Python and code it on the Pi or use FlowStone/FlowBotics on the PC and control it all wirelessly using telemetry. There is a Bluetooth option also for the FlowPaw to make it wireless doing away with the Raspberry Pi altogether but we wanted to show the versatility in this workshop and the fact that FlowPaw can interface to a Raspberry Pi quite easily

Links

FlowPaw, www.FlowPaw.com
DSPRobotics, www.DSPRobotics.com
FlowBotics, www.FlowBotics.com

Leave a Reply

Your email address will not be published. Required fields are marked *