Dawn Robotics Blog » Alan http://blog.dawnrobotics.co.uk Tutorials and updates from Dawn Robotics Fri, 26 Dec 2014 23:06:33 +0000 en-US hourly 1 http://wordpress.org/?v=3.7.5 Off to Pitch@Palace http://blog.dawnrobotics.co.uk/2014/11/pitchpalace/ http://blog.dawnrobotics.co.uk/2014/11/pitchpalace/#comments Sun, 02 Nov 2014 10:01:44 +0000 http://blog.dawnrobotics.co.uk/?p=714 Dawn Robotics is quite a young company (our store is just over a year and a half old now), and as we go through the process of bootstrapping ourselves, we’re lucky to have startup space in the Bristol Robotics Laboratory Technology Incubator. As part of our involvement with the Technology Incubator, we’re also fortunate enough […]

The post Off to Pitch@Palace appeared first on Dawn Robotics Blog.

]]>
Dawn Robotics is quite a young company (our store is just over a year and a half old now), and as we go through the process of bootstrapping ourselves, we’re lucky to have startup space in the Bristol Robotics Laboratory Technology Incubator.

As part of our involvement with the Technology Incubator, we’re also fortunate enough to have been invited to take part in Pitch@Palace, a platform for young British startups organised by HRH The Duke of York on 5th November at St. James’s Palace.

We’ll be heading along with 2 other tech startups from the BRL incubator, Reach Robotics, and OmniDynamics. This is a great opportunity for us to explain our vision for robotics in education and research to a wider audience (see the slightly rushed video pitch below…).

As part of the Pitch@Palace event, there’s a Pitch@Palace people’s choice award which is given to the company that gets the most votes from the public. So if you could find time to support us and cast a vote for us then that would be very much appreciated. :)

The post Off to Pitch@Palace appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/11/pitchpalace/feed/ 4
Adding Sensors to the Raspberry Pi Camera Robot Kit http://blog.dawnrobotics.co.uk/2014/09/adding-sensors-raspberry-pi-camera-robot-kit/ http://blog.dawnrobotics.co.uk/2014/09/adding-sensors-raspberry-pi-camera-robot-kit/#comments Mon, 15 Sep 2014 14:13:47 +0000 http://blog.dawnrobotics.co.uk/?p=656 Sensors are a vital part of any robotic project, as they allow a robot to get information about the environment in which it’s operating. Without sensors, a robot has no information about the world around it, and it’s very tough to program intelligent behaviours for the robot. Now, you’ve always been able to attach sensors […]

The post Adding Sensors to the Raspberry Pi Camera Robot Kit appeared first on Dawn Robotics Blog.

]]>
Sensors are a vital part of any robotic project, as they allow a robot to get information about the environment in which it’s operating. Without sensors, a robot has no information about the world around it, and it’s very tough to program intelligent behaviours for the robot.

With sensors your robot can find out about the world

With sensors your robot can find out about the world

Now, you’ve always been able to attach sensors to our Raspberry Pi Camera Robot and the Arduino Mini Driver board we use for motor control, but previously you would have had to modify quite a bit of code in order to get your sensor data out. To fix this, we’ve just released an update to the software for our Raspberry Pi Camera robot which makes things much easier. You can now get a large number of sensors to work simply by connecting them to the Mini Driver. The new Mini Driver firmware repeatedly reads from the attached sensors and sends the readings up to the Pi at a rate of 100 times a second. Once on the Pi, the sensor values can be easily retrieved using the robot web server’s websocket interface. Sensors can also be connected directly to the Pi, with sensor readings returned in the same way.

In this tutorial we show you how to update your robot’s software if needed, how to connect sensors to robot, and then how to read the sensor values using the Python py_websockets_bot library. This will let you write control scripts for your robot that use sensors, and which either run on the Pi, or which run on another computer connected over the network.

Upgrading the Software

If you bought your robot before this post was published, then it’s likely that you’ll need to upgrade the software on your robot. Probably the easiest way to do this is to download the latest SD card image from here, and then flash it to an SD card using the instructions here. If you’re interested, you can also find details of how we built the image from the ground up here.

Moving the Neck Pan and Tilt Servo Connectors

Originally, the pan and tilt servo were connected up to pins D4 and D3 respectively. With this software update, we’ve had to move the servo connections in order to be able to attach wheel encoder sensors (these need the interrupts on D2 and D3). Therefore, in order to make sure that the neck motors keep on working, move the pan servo to the row marked D11, and the tilt servo to the row marked D6.

Move the neck servos to pins D6 and D11

Move the neck servos to pins D6 and D11

Connecting Sensors to the Robot

You have two main options when connecting sensors to the robot. You can either connect sensors to the Mini Driver, or alternatively you can connect them to the Raspberry Pi.

Connecting sensors to the Mini Driver is usually the simpler option, as the Mini Driver can talk to sensors running at 5V, and the sensor values are read automatically at a rate of 100Hz by the firmware on the Mini Driver. It can be useful however, to sometimes connect sensors to the Raspberry Pi. This can happen if you run out of pins on the Mini Driver, or if you need to use I2C or SPI to communicate with a sensor (as these protocols aren’t supported in the Mini Driver firmware yet, and probably won’t be due to a lack of space). Connecting sensors to the Raspberry Pi will probably involve a bit more work as you may need to shift 5V sensor outputs to the 3.3V used by the Pi, and you’ll also need to write a little bit of Python code to talk to the sensor.

With that in mind, we’ll look at the two options for connecting the sensors in a bit more detail. Please Note: In the examples below we use sensors we sell in our store, but there’s nothing to stop you from using any other sensor that you can find, as long as they’re electrically compatible. This means that there’s a truly vast array of sensors out there that you can use with your robot. :D

Connecting Sensors to the Mini Driver

The range of sensors you can attach to the Mini Driver includes digital sensors, analog sensors, encoders and an ultrasonic sensor. The ability to read from analog sensors is really useful as the Raspberry Pi doesn’t have an Analog to Digital Converter (ADC) of its own.

The Mini Driver runs at 5V and so it’s assumed that that’s the voltage level at which sensors you connect to the Mini Driver will run at as well. Please check that the sensors you connect to the Mini Driver are happy running at 5V to avoid damaging them. Also, please check your 5V (VCC) and GND connections carefully before turning the power on, as getting them the wrong way round may let the magic smoke out of your sensors, rendering them useless…

Digital Inputs

Digital sensors are sensors which give a simple high or low output as a result of detecting something. There are 8 pins that you can attach digital sensors to, pins D12, D13 and the 6 analog pins A0 to A5. It may seem odd that you can attach digital sensors to the analog pins, but the pins can be used as both type of input. We’ll configure the exact type of input for the pins in software later on.

As an example of attaching digital sensors we use the trusty line sensor which gives a high signal when it detects a black line on a white background.

Multiple sensors allow you to detect the direction to the line

Multiple sensors allow you to detect the direction to the line

In the image above we’ve attached 3 line sensors to the front of the chassis using some M2 spacers, and we then connect them to the Mini Driver using some female jumper wires, as shown in the image below.

It doesn’t matter which of the 8 possible digital input pins you use, but we’ve used pins A1, A2 and A5, and will then configure these pins as digital inputs in software. With these 3 sensors attached it’s now possible to make the robot follow a black line laid down using a marker pen, or insulation tape.

One possible way of attaching line sensors

One possible way of attaching line sensors. Note: One of the pins on the line sensor is marked as NC (No connection) so the extra wire can be tied back or cut away.

Attaching an Ultrasonic Sensor

Ultrasonic sensors emit bursts of high pitched sound waves (beyond the range of human hearing) which bounce off obstacles in front of the robot. By measuring how long it takes for signals to go out and come back, the robot can work out if there are any obstacles in front of it, and if so, how far away they are.

You can fit both a camera, and an ultrasonic sensor on the robot's pan/tilt head.

You can fit both a camera, and an ultrasonic sensor on the robot’s pan/tilt head.

In the image above we’ve attached an ultrasonic sensor to the pan/tilt head of the robot, and then connected it to the Mini Driver using a 3 pin jumper wire. The only pin you can attach the ultrasonic sensor to with our Mini Driver firmware is pin D12. Also, because reading from the ultrasonic sensor can take a long time, at the moment we only read from the ultrasonic sensor at a rate of 2Hz. If you need to attach more than one ultrasonic sensor to your robot, or if you need to read at a faster rate than 2Hz, then you’ll need to attach the other ultrasonic sensors to the Raspberry Pi’s GPIO pins.

Connect the Ultrasonic sensor to D12

Connect the Ultrasonic sensor to D12

Analog

The analog inputs of the Mini Driver can be used to read from analog sensors where the output signal is between 0V and 5V. The ADC on the Mini Driver is a 10-bit ADC so the readings will be returned as values between 0 and 1023. In the image below we’ve connected the X, Y and Z channels of the ADXL335 accelerometer to pins A1, A2 and A3 of the Mini Driver. Accelerometers are great for detecting the acceleration due to gravity (and thus working out if the robot is on a slope), and they can also be used as a neat method for detecting collisions, as we showed in our blog post on ‘bump navigation‘.

Attach analog sensors such as an ADXL335 accelerometer to the ADC input pins.

Attach analog sensors such as an ADXL335 accelerometer to the ADC input pins.

Incremental Encoders

Incremental Encoders are sensors which you can use to tell you how fast a motor is turning, and usually, what direction it’s turning in as well (see here for a good introductory article). We don’t yet sell encoders for the Dagu 2WD Chassis (coming soon), but in the meantime, you may still find this feature useful if you’re using a different chassis, which does have encoders, to build your robot.

Quadrature encoders have 2 outputs, phase A and phase B, and can be used to detect both the speed and direction in which the motor is turning. Wire the left encoder to pins D2 and D4, and wire the right encoder to pins D3 and D5. The Mini Driver firmware can also be made to work with single output encoders. In this case wire the left encoder to D2 and the right encoder to D3.

Connect the encoders up to the first 4 digital pins

Connect the encoders up to the first 4 digital pins

Reading from the Sensors

Once you’ve connected up all your sensors, the next thing to think about, is how to read from the sensors, to make use of them in your control programs. The firmware on the Mini Driver (actually an Arduino sketch which can be found here) reads from the sensors at a rate of 100Hz and sends the data back to the Pi over the USB cable using serial.

On the Pi, you have two main options for talking to the Mini Driver. The first is to use the Mini_Driver Python class we provide in your own Python script, (example script here, read comments at top of file). The second, recommended and more flexible option, is to talk to the robot web server which is running on the Pi using the Python py_websockets_bot library.

Instructions for installing the py_websockets_bot library can be found in this blog post here. If you’ve already got py_websockets_bot installed then you may need to update it to get the latest version. This can be done by navigating to the py_websockets_bot library and running.

git pull
sudo python setup.py install

We’ve added an example script to py_websockets_bot called get_sensor_readings.py which shows you how to read sensor values from the robot. Run the script using the following command

examples/get_sensor_readings.py ROBOT_IP_ADDRESS

where ROBOT_IP_ADDRESS is the network address of your robot. After a short delay you should see sensor values streaming back from the robot.

Looking at the example script in more detail, the important bits of the script are as follows.

Configure the Sensors

Firstly we construct a SensorConfiguration object and send it over to the robot

# Configure the sensors on the robot
sensorConfiguration = py_websockets_bot.mini_driver.SensorConfiguration(
    configD12=py_websockets_bot.mini_driver.PIN_FUNC_ULTRASONIC_READ, 
    configD13=py_websockets_bot.mini_driver.PIN_FUNC_DIGITAL_READ, 
    configA0=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ, 
    configA1=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
    configA2=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ, 
    configA3=py_websockets_bot.mini_driver.PIN_FUNC_DIGITAL_READ,
    configA4=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ, 
    configA5=py_websockets_bot.mini_driver.PIN_FUNC_ANALOG_READ,
    leftEncoderType=py_websockets_bot.mini_driver.ENCODER_TYPE_QUADRATURE, 
    rightEncoderType=py_websockets_bot.mini_driver.ENCODER_TYPE_QUADRATURE )

# We set the sensor configuration by getting the current robot configuration 
# and modifying it. In this way we don't trample on any other 
# configuration settings
robot_config = bot.get_robot_config()
robot_config.miniDriverSensorConfiguration = sensorConfiguration

bot.set_robot_config( robot_config )

Pin D12 can be set as either PIN_FUNC_ULTRASONIC_READ or PIN_FUNC_DIGITAL_READ, pin D13 can be set as either PIN_FUNC_INACTIVE or PIN_FUNC_DIGITAL_READ, and the analog pins A0 to A5 can be set as either PIN_FUNC_ANALOG_READ or PIN_FUNC_DIGITAL_READ. The encoders can be set to be either ENCODER_TYPE_QUADRATURE or ENCODER_TYPE_SINGLE_OUTPUT.

Estimate Time Difference to Robot

For some robot applications, it can be important to know exactly when a sensor reading was made. In our software, whenever a sensor reading reaches the Raspberry Pi, it is timestamped with the time that it arrived at the Pi. The problem is however, that if your robot control script is running on a PC connected to the robot over a network, then the system clock of the control PC is likely to be different from the system clock of the Pi. To resolve this problem, we provide a routine to estimate the offset from the system clock to the Raspberry Pi’s clock.

robot_time_offset = bot.estimate_robot_time_offset()

At the moment the algorithm for estimating the time offset is not particularly efficient, and will block for about 10 seconds or so. In the future we’d like to modify this routine so that it estimates the time offset asynchronously and continuously in the background. In the meantime, if you’re not interested in the precise time at which sensor readings were made, then you can leave this line out of your own programs.

Read the Status Dictionary

Sensor readings are returned as part of the status dictionary which, as you might expect, contains data about the current status of the robot. Retrieve the status dictionary using the following line

status_dict, read_time = bot.get_robot_status_dict()

Read the Sensor Values

Having obtained the status dictionary, the sensor readings are returned as a dictionary called ‘sensors’. Each sensor reading is represented as a timestamp which gives the time on the Pi system clock when the reading arrived at the Pi, coupled with the data for the sensor reading.

# Print out each sensor reading in turn
sensor_dict = status_dict[ "sensors" ]
for sensor_name in sensor_dict:

    # Get the timestamp and data for the reading
    timestamp = sensor_dict[ sensor_name ][ "timestamp" ]
    data = sensor_dict[ sensor_name ][ "data" ]

    # Calculate the age of the reading
    reading_age = (time.time() + robot_time_offset) - timestamp

    # Print out information about the reading
    print sensor_name, ":", data, "reading age :", reading_age

The format of the data entry will depend on the type of sensor being read. For the sensor types that can be attached to the Mini Driver the dictionary entries are

  • batteryVoltage – This is a floating point number giving the current voltage of the batteries attached to the +/- pins of the Mini Driver.
  • ultrasonic – This is an integer giving the distance reading in centimetres. The maximum range of the sensor is 400cm, and if it looks as if no ultrasonic sensor is attached to the Mini Driver then the value returned will be 1000.
  • analog – This is an array of 6 integers, one for each of the analog pins A0 to A5, giving a value from 0 to 1023, representing 0V to 5V. If an analog pin is configured as a digital input then it will still have an entry in this array, but it will be set to 0.
  • digital – This is a byte, with each bit representing one of the possible digital pins. Looking at the byte from left to right, the bits correspond to pins A5, A4, A3, A2, A1, A0, D13 and D12.
  • encoders – This is a pair of integers giving the current tick count for the left encoder and the right encoder.

Connecting Sensors to the Raspberry Pi

You should now have a good idea of how to connect a variety of sensors to your robot, and how to read values from those sensors using the py_websockets_bot library. You may find however, that you also need to connect some sensors to the Raspberry Pi’s GPIO pins. This could be because you run out of space on the Mini Driver (the more sensors the better!) or because you have a sensor that uses I2C or SPI as a means to communicate with them.

As an example, in the images below we’ve attached a digital light sensor, and a digital compass to the I2C GPIO pins of the Pi using some jumper wires and a Grove I2C hub, to create a robot that can navigate with the compass, and detect light levels (perhaps it wants to hide in dark places…).

You can attach a range of sensors (such as a digital light sensor and a compass) using I2C

You can attach a range of sensors (such as a digital light sensor and a compass) using I2C

pi_sensors_02

The sensors fixed to the robot

We don’t have the space here to go into detail about how you would wire up all the different sensor types to the Pi’s GPIO pins, and then communicate with them using Python. But there are lots of good tutorials on attaching sensors to the Pi that you can find with Google. Once you’re in the situation where you can connect to, and communicate with the sensor, the steps you need to take to integrate it with the robot are

  1. Take a copy of default_sensor_reader.py and rename it to something like ‘my_sensor_reader.py’. Leave it in the sensors directory.
  2. Fill in the routines in the file. The comments in the file should tell you what you need to do, basically you need to provide a routine to set up your sensors, to read from your sensors and optionally to shut them down. When you read from the sensors you’ll create a SensorReading object (timestamp and data) and put it into a dictionary for integration with the main sensor dictionary. Note: Do not change the name of the class from PiSensorReader.
  3. Update the robot configuration to use your sensor reader module. This can be done with code that looks like this
robot_config = bot.get_robot_config()
robot_config.piSensorModuleName = "sensors.my_sensor_reader"
bot.set_robot_config( robot_config )

If all goes well then your sensor readings should now be returned to you in the sensor dictionary.

Taking Things Further

This tutorial has shown some of the many different types of sensor you can attach to your Raspberry Pi robot, and hopefully it’s given you a good idea of how you’d go about wiring them up and reading from them. Now, the sky is the limit, as putting sensors onto your robot really makes it much easier to program interesting and intelligent behaviours for your robot. Build a robot that can drive around a room, avoiding obstacles, use a light sensor to get the robot to chase bright lights etc, the possibilities are endless.

If you have any questions about attaching sensors to your robot, or need clarification on anything we’ve talked about in this tutorial, then please post a comment below or in our forums. Also we’d love to hear what sensors you decide to put on your robot, and how you end up using them. :)

The post Adding Sensors to the Raspberry Pi Camera Robot Kit appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/09/adding-sensors-raspberry-pi-camera-robot-kit/feed/ 3
Improving the Battery Life of Your Raspberry Pi Robot with a UBEC http://blog.dawnrobotics.co.uk/2014/08/improving-battery-life-raspberry-pi-robot-ubec/ http://blog.dawnrobotics.co.uk/2014/08/improving-battery-life-raspberry-pi-robot-ubec/#comments Thu, 14 Aug 2014 16:55:54 +0000 http://blog.dawnrobotics.co.uk/?p=624 We recently started selling a USB powerbank which can be used to power the Raspberry Pi robot kit that we sell. Using the powerbank provides great battery life, but we’re still interested in making the robot run well from AA batteries, as this may be the cheaper option if you already have rechargeable batteries and […]

The post Improving the Battery Life of Your Raspberry Pi Robot with a UBEC appeared first on Dawn Robotics Blog.

]]>
A UBEC provides a nice efficient way to power your Pi from batteries

A UBEC provides a nice efficient way to power your Pi from batteries

We recently started selling a USB powerbank which can be used to power the Raspberry Pi robot kit that we sell. Using the powerbank provides great battery life, but we’re still interested in making the robot run well from AA batteries, as this may be the cheaper option if you already have rechargeable batteries and a charger lying around.

Testing battery life

Testing battery life

Therefore, we’re modifying the kit a bit to include a UBEC (Universal Battery Elimination Circuit). This is an efficient switching voltage regulator which takes the load off the linear voltage regulator of the Mini Driver and improves the running time of a robot being powered by AA batteries a lot. This post gives some details about why we’re making this change and also describes a battery testing script that we’ve written to determine what kind of run times can be expected for different methods of powering the Raspberry Pi robot.

The Dagu Arduino Mini Driver board used in the Raspberry Pi robot contains a L4941B 1A linear voltage regulator which can be used to run the robot from batteries. Linear voltage regulators are cheap, which helps keep the Mini Driver affordable, but they’re not very efficient, so a lot of battery power is wasted as heat. Also the Mini Driver, Raspberry Pi and a USB Wifi dongle use up quite a lot of the available 1A so there’s not much headroom for adding extra sensors to the robot.

By contrast, the UBEC we’re using is a switching voltage regulator which are a lot more efficient than linear regulators, wasting much less battery power. Also, the UBEC is able to provide up to 3A of current, so provided the batteries can keep up, this provides a lot more current to supply USB peripherals plugged into the Pi, and sensors plugged into the robot.

Wiring up the UBEC

Making use of the UBEC is very straightforward. If you’ve already wired up your robot to provide power to the Raspberry Pi from the Mini Driver, then first remove these wires. After that, connect the red input wire of the UBEC to the pin directly next to the on/off switch of the Mini Driver (this is connected to battery voltage), and then connect the black input wire of the UBEC to a GND pin on the Mini Driver. We’ve used the GND pin on the 3×2 programming header as this is unlikely to be used for anything else. Finally, plug the USB connector of the UBEC into the Pi and attach a set of 6xAA batteries to the +/- pins of the Mini Driver and you’re good to go. :)

Remove any power wires connecting the Mini Driver to the Pi.

Remove any power wires connecting the Mini Driver to the Pi.

 

Connect the UBEC to the Mini Driver and the Pi

Connect the UBEC to the Mini Driver and the Pi

Power the Robot using 6xAA batteries (preferably rechargeable)

Power the Robot using 6xAA batteries (preferably rechargeable)

Testing the Battery Life of the Robot

To help us test and compare different ways of powering our robot we’ve developed a little battery testing script using our py_websockets_bot control library (installation instructions here). This test script runs on a PC and connects to the robot over WiFi. It runs in a loop making the robot look in different directions, and then periodically making the robot spin either left or right. In this way we hope to simulate the conditions in which the robot might be used i.e. lots of stop/start motion. The Mini Driver has one of its ADC pins hard-wired to a voltage divider to measure its battery voltage, and so the test script periodically records this voltage. When the robot runs out of juice, it stops responding and the script can be ended, at this point the script saves the voltages out to a CSV file so we can plot how the voltage of the battery changes over time.

The graph below shows some of the tests we’ve run. All of the tests were run on our Raspberry Pi robot using a Model B, so we’d expect runtime improvements if using a Model B+, as this has lower power consumption. The voltage readings are quite noisy, but you still get the general discharge curve over time when running off batteries. The graph shows the advantage of using good rechargeable batteries (NiMH) instead of non-rechargeable (Alkaline) batteries. On rechargeable batteries (Duracell 2400mAh NiMH) the robot ran for approximately 3 hours, which was 3 times longer than when using Alkalines (Sainsbury’s Extra Long Life). The graph also shows the results of running the robot on the TeckNet iEP387 USB powerbank that we sell.

We’ll update this graph with more tests as we run them, i.e. we’re planning to run tests using the Model B+. Also, we’d be really interested to see the results of any battery tests that users run on their camera robots to see how they compare with our results.

battery_power

Results of running battery_test.py using different power supplies for the robot.

 

 

The post Improving the Battery Life of Your Raspberry Pi Robot with a UBEC appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/08/improving-battery-life-raspberry-pi-robot-ubec/feed/ 1
Robotics and the Raspberry Pi Model B+ http://blog.dawnrobotics.co.uk/2014/07/robotics-raspberry-pi-model-b/ http://blog.dawnrobotics.co.uk/2014/07/robotics-raspberry-pi-model-b/#comments Sat, 19 Jul 2014 16:54:07 +0000 http://blog.dawnrobotics.co.uk/?p=618 So, the big news this week (if you’re a Raspberry Pi fan) is that the Raspberry Pi foundation announced the release of an upgraded version of the Model B Pi, the Model B+. The Raspberry Pi Model B+ is a really nice incremental update of the Model B, and it’s especially good for people wanting […]

The post Robotics and the Raspberry Pi Model B+ appeared first on Dawn Robotics Blog.

]]>
The new Model B+

The new Model B+

So, the big news this week (if you’re a Raspberry Pi fan) is that the Raspberry Pi foundation announced the release of an upgraded version of the Model B Pi, the Model B+. The Raspberry Pi Model B+ is a really nice incremental update of the Model B, and it’s especially good for people wanting to use the Pi in robotic projects. This is because, alongside extra USB ports, it now uses switching voltage regulators which means that it consumes less power (between 0.5W to 1W less) and therefore it will last longer on batteries.

As soon as we got a Model B+ this week, we put it onto one of our Raspberry Pi Camera Robots, and it works great. :) The Model B+ has a different layout and mounting holes than the Model B so we’ve updated the assembly instructions for the robot to show how the Model B+ should be mounted.

We’ve also released a new version of the software for the robot, as the Model B+ needs different drivers for its USB and network ports. This new software has a few bugfixes, and also allows the robot to be driven at slower speeds than before. This new feature may not sound like much, but with the old software, the motors often couldn’t be made to go slower than about 33% speed before they stalled due to friction in the gearboxes. Now by driving the motors in a different way, we get the motors to turn with more torque at lower speeds to overcome this friction. This means that it’s easier to drive the robot whilst looking through the camera, and makes making precise turns easier.

The new SD card image can be downloaded here. Other options for getting the software are discussed here.

The post Robotics and the Raspberry Pi Model B+ appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/07/robotics-raspberry-pi-model-b/feed/ 0
New Product – A Power Bank for your Raspberry Pi Robot http://blog.dawnrobotics.co.uk/2014/06/new-product-power-bank-raspberry-pi-robot/ http://blog.dawnrobotics.co.uk/2014/06/new-product-power-bank-raspberry-pi-robot/#comments Wed, 18 Jun 2014 09:48:31 +0000 http://blog.dawnrobotics.co.uk/?p=588 One issue that has caught quite a few people out when they build our Raspberry Pi robot kit, is the issue of power. The kit comes with a 6xAA battery holder, but the trouble is, not all AA batteries are the same, which is easy to overlook when you’re grappling with all the other complexities […]

The post New Product – A Power Bank for your Raspberry Pi Robot appeared first on Dawn Robotics Blog.

]]>
One issue that has caught quite a few people out when they build our Raspberry Pi robot kit, is the issue of power. The kit comes with a 6xAA battery holder, but the trouble is, not all AA batteries are the same, which is easy to overlook when you’re grappling with all the other complexities of building a Raspberry Pi robot. :)

We recommend that the robot be powered with good quality, high capacity, rechargeable (NiMh or NiCd) batteries, such as Duracell 2400mAh NiMh . Non-rechargeable (Alkaline) batteries are not recommended as they will struggle to provide enough current to power both the Pi and the motors of the robot.

Good for Robots

Good for robots

Bad for Robots

Bad for robots

Good for Robots

Pretty (and also good for robots)

 

 

 

 

 

As an alternative to AA batteries, we’re now selling the  TeckNet iEP387 USB power bank which can be used to power the entire robot. The power bank is more expensive that the cost of 6 AA rechargeable batteries, but you get the advantage of increased runtime (approx 5 hours compared to 3hrs for the NiMh Duracells), and you don’t have to buy a battery charger.

In this blog post we show you how to use the power bank with the robot.

Connecting the Battery to the Robot

Please Note: If you are using a USB battery pack to power the Pi and mini driver, then you do not need to use the UBEC which we’ve started to supply with more recent versions of our Raspberry Pi robot.

Once you’ve built the robot following these instructions, Slide the iEP387 USB power bank into the chassis behind the wheels.

The iEP387 should come with 2 USB cables, a micro USB cable for powering the Pi, and a USB cable with 2.54mm connectors for connecting to the Mini Driver, and powering the motors. First plug the micro USB cable into the 5V 2.1A output of the iEP387 (the Pi needs this) and connect it to the power connector on the Pi (the extra cable can be wound around the Raspberry Pi mounting struts).

Secondly, use the USB power cable with red and black leads, and 2.54mm connectors to attach the 5V 1.0A output of the iEP387 to the battery pins of the Mini Driver (marked + and – next to the mini USB connector). The red wire should attach to the + pin and the black wire should attach to the – pin. Don’t worry if you get them the wrong way round though, as the Mini Driver has reverse bias protection.

Connecting the iEP387 USB Power Bank to the Robot

Connecting the iEP387 USB Power Bank to the Robot

Make sure that you leave a bit of slack in the cables so that you are able to slide the iEP387 sideways slightly and press the on/off button.

Turning on the iEP387 Power Bank

Turning on the iEP387 Power Bank

Turning on the Robot

To turn on the robot, first switch on the power switch on the Mini Driver. This is important in order to provide power to the motors. Then slide the iEP387 sideways, and press the power button. This should turn on the robot.

Turning off the Robot

To turn off the robot you need to unplug the micro USB connector from the Pi, and turn off the power switch on the Mini Driver. This is not as neat as we’d like it, but there’s not really an easy way (without adding more hardware, and therefore more cost) to stop the Pi from drawing power from the power bank.

Unplug the micro USB cable to turn off the Pi

Unplug the micro USB cable to turn off the Pi

Pulling the power from the Pi shouldn’t damage anything as the robot’s software doesn’t write anything to the SD card. However, it’s always nice to let the Pi shutdown cleanly if possible, and so to do that you can use the shutdown button on the robot’s web interface.

 Updated: Extra Photos to Show Installation of Battery

I’ve had a couple of people say that their battery pack touches the wheels so have posted the following pictures to try to clarify things. The pictures are not great quality, but hopefully I’ll have time to update them in the new year.

There should be a small (approx 2mm) gap between the battery pack and the wheels

There should be a small (approx 2mm) gap between the battery pack and the wheels

 

The battery pack should be held back by the plastic motor tabs.

The battery pack should be held back by the plastic motor tabs.

As an alternative to inserting the battery pack in from the side, it’s also possible to remove the central support and slide the battery pack in from the front. This is a snug fit, but again there should be a few millimetres between the battery pack and the wheels. This gap can be increased by sliding the wheels slightly along the motor axles.

If the central support is removed, the battery pack can be slid in from the front.

If the central support is removed, the battery pack can be slid in from the front.

There should be a few millimetres of clearance between the battery pack and the wheels.

There should be a few millimetres of clearance between the battery pack and the wheels.

 

 

The post New Product – A Power Bank for your Raspberry Pi Robot appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/06/new-product-power-bank-raspberry-pi-robot/feed/ 28
Programming a Raspberry Pi Robot Using Python and OpenCV http://blog.dawnrobotics.co.uk/2014/06/programming-raspberry-pi-robot-using-python-opencv/ http://blog.dawnrobotics.co.uk/2014/06/programming-raspberry-pi-robot-using-python-opencv/#comments Wed, 11 Jun 2014 16:34:48 +0000 http://blog.dawnrobotics.co.uk/?p=554 Our Raspberry Pi robot has proven to be very popular, as it allows people to easily put together a fun little robot, that they can drive around using a smartphone, tablet or computer, whilst viewing the world with the camera on the robot. However, fun as this is, it’s hard to view this ‘robot’ as […]

The post Programming a Raspberry Pi Robot Using Python and OpenCV appeared first on Dawn Robotics Blog.

]]>
Our Raspberry Pi robot has proven to be very popular, as it allows people to easily put together a fun little robot, that they can drive around using a smartphone, tablet or computer, whilst viewing the world with the camera on the robot. However, fun as this is, it’s hard to view this ‘robot’ as being much more than a remote controlled toy. Our personal view has always been that a robot should be autonomous in some way, and that’s why we’ve been working on a programming interface for our robot that will let users of our robots create cool, autonomous behaviours.

Bring your robot to life with Python and OpenCV

Bring your robot to life with Python and OpenCV

The interface is a Python library called py_websockets_bot. The library communicates with the robot over a network interface, controlling it’s movements and also streaming back images from its camera so that they can be processed with the computer vision library OpenCV. Communicating over a network interface means that your scripts can either run on the robot, or they can run on a separate computer. This feature is really useful if you want to use a computer more powerful than the Pi to run advanced AI and computer vision algorithms, or if you want to coordinate the movement of multiple robots.

In this post we show you how to install the interface library, and provide some example programs, that show you how to make the robot move, how to retrieve images from the camera and how to manipulate the images with OpenCV.

Software Overview

Before we talk about installing the py_websockets_bot library, the image below should hopefully give you a better idea about how the software on the robot works.

Overview of the Raspberry Pi Robot software

Overview of the Raspberry Pi Robot software

At a low level, we use an Arduino compatible board called a Mini Driver to control the hardware of the robot. Running on the Mini Driver is a sketch that listens over serial USB for commands from the Pi, whilst sending back status data (at the moment just battery voltage, but other sensors could also be added).

On the Pi, we run a web server written in Python that provides a web interface, and which listens for commands over the WebSockets protocol. When it gets commands, it sends them onto the Mini Driver via serial USB. The other program running on the Pi is a camera streamer called raspberry_pi_camera_streamer. This is a custom Raspberry Pi camera program we’ve written that streams JPEG images over the network. It can also stream reduced size images (160×120) for computer vision, along with motion vectors (coarse optical flow data) from the Raspberry Pi camera.

To control the robot, you can either use a web browser such as Chrome or Firefox, or now, you can also write a program using the py_websockets_bot library. Both of these will communicate with the web server and the camera streamer on the Pi using WebSockets and HTTP.

It may seem a bit over complicated to run a web server, and to communicate with this, rather than controlling the robot’s hardware directly, but it gives us a lot of flexibility. The web interface can be used to observe the robot whilst it’s being controlled by a script, and as mentioned before, control scripts can be written just once and then run either on the robot, or on a separate computer, depending upon how much computing power is needed. Also, in theory, you can write your control scripts in whatever language you like, as long as you have a library that can speak WebSockets. We have provided a Python library, but there are WebSockets libraries available for many other languages, such as Javascript, Ruby and the .NET framework.

Installing the Software

Starting with the standard Dawn Robotics SD card, run the following commands on your Pi to make sure that the robot’s software is up to date

cd /home/pi/raspberry_pi_camera_streamer
git pull
cd build
make
sudo make install
cd /home/pi/raspberry_pi_camera_bot
git pull

Reboot your Pi to use the updated software.

Installing py_websockets_bot on a Linux PC or the Raspberry Pi

Run the following commands to install the libraries dependencies

sudo apt-get update
sudo apt-get install python-opencv

then

git clone https://bitbucket.org/DawnRobotics/py_websockets_bot.git
cd py_websockets_bot
sudo python setup.py install

Installing py_websokects_bot on Windows

This is trickier but involves the following steps

If needed, more details for OpenCV setup on Windows can be found at http://docs.opencv.org/trunk/doc/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

Installing py_websokects_bot on a Mac

We don’t have a Mac to do this, but hopefully, the installation process should be similar to installing on Linux. If there are any Mac users out there who could give this a go and let us know how they get on, we’d be very grateful. :)

Making the Robot Move

Making the robot move is very straightforward, as shown in the code snippet below

import py_websockets_bot

bot = py_websockets_bot.WebsocketsBot( "ROBOT_IP_ADDRESS" )
bot.set_motor_speeds( -80.0, 80.0 )    # Spin left

For ROBOT_IP_ADDRESS you would put something like “192.168.42.1″ or “localhost” if the script was running on the robot. The code snippet connects to the robot, and then starts it turning left by setting the left motor to -80% speed and the right motor to +80% speed.

The example file motor_test.py shows more of the commands you can use. It can be run from the py_websockets_bot directory by using the command

examples/motor_test.py ROBOT_IP_ADDRESS

Getting and Processing Images from the Robot

One of the really exciting thing about using the Pi for robotics, is that it has a good standard camera, and enough processing power to run some computer vision algorithms. The example file get_image.py shows how to get an image from the camera, and then use the OpenCV library to perform edge detection on it.

Run it as follows

examples/get_image.py ROBOT_IP_ADDRESS
The result of running edge detection on a camera image from the Raspberry Pi robot

The result of running edge detection on a camera image from the Raspberry Pi robot

Small Images

The Pi can run computer vision algorithms, but the processing power of its CPU is very limited when compared to most laptops and desktop PCs. One crude but effective way to speed up a lot of computer vision algorithms is simply to run them on smaller images. To support this, the py_websockets_bot library also offers routines to stream ‘small’ images which have been reduced in size on the GPU of the Pi. The standard camera images from the robot are 640×480, and the small images are 160×120.

Getting Motion Vectors (Optical Flow) from the Robot’s Camera

The Raspberry Pi contains a very capable GPU which is able to encode images from the Pi camera into H264 video in real time. Recently, the clever people at Pi towers added a feature to the Pi’s firmware that allows the vectors generated by the motion estimation block of the H264 encoder to be retrieved. What this means, is that it’s possible to get your Pi to calculate the optical flow for its camera images, practically for free! (0% CPU) We haven’t managed to do anything cool with this yet, but we’ve provided routines to retrieve them from the camera so that other people can.

The motion vectors from waving a hand in front of the Raspberry Pi Robot's camera (no really, squint a bit...)

The motion vectors from waving a hand in front of the Raspberry Pi Robot’s camera (no really, squint a bit…)

To see this for yourself, run the following example

examples/display_motion_vectors.py ROBOT_IP_ADDRESS

Taking Things Further

Hopefully you can see that there are lots of possibilities for creating cool behaviours for your robot using the py_websockets_bot library and OpenCV. For example, you could write a script to get the robot to drive around your home. Perhaps you could get it to act as a sentry bot, emailing you an image if it detects motion. Or perhaps you could get it to detect and respond to people using face detection.

You can get more details about the capabilities of py_websockets_bot in its documentation here, and also the documentation of OpenCV can be found here. If you have any questions about the py_websockets_bot library, please post on our forums, You can also use our forums as a place to share your robotic projects with the rest of the world. :)

The post Programming a Raspberry Pi Robot Using Python and OpenCV appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/06/programming-raspberry-pi-robot-using-python-opencv/feed/ 8
New Software For Our Raspberry Pi Camera Robot http://blog.dawnrobotics.co.uk/2014/05/new-software-raspberry-pi-camera-robot/ http://blog.dawnrobotics.co.uk/2014/05/new-software-raspberry-pi-camera-robot/#comments Wed, 07 May 2014 14:27:03 +0000 http://blog.dawnrobotics.co.uk/?p=547 We had a great response to a recent blog post we wrote, describing how to build a Raspberry Pi robot that you can drive around using a tablet, smartphone or PC. Therefore we’ve carried on developing the software for it, and are pleased to announce the update today. The changes that we’ve made to the […]

The post New Software For Our Raspberry Pi Camera Robot appeared first on Dawn Robotics Blog.

]]>
We had a great response to a recent blog post we wrote, describing how to build a Raspberry Pi robot that you can drive around using a tablet, smartphone or PC. Therefore we’ve carried on developing the software for it, and are pleased to announce the update today. The changes that we’ve made to the software have largely been to support cool things that we want to do with the robot in the future, but features that users of the robot will notice are

  • much faster camera streaming. Initially the camera was streaming images at about 4-5 frames per second using raspistill and mjpg-streamer, but we’ve now written our own camera streamer, and got it streaming at 15fps which is much smoother, and makes driving the robot around a lot easier.
  • support for more WiFi dongles when working as an access point. Our previous software release mainly worked with WiFi dongles that used the same chipset as the Edimax EW-7811Un (the hostapd rtl871xdrv driver). It was possible to get it to work with other WiFi dongles that used the hostapd nl80211 driver, but it required a fairly technical user. Now our software should work with a much wider range of WiFi dongles with no change required.
  • we’ve added a shutdown button. Previously the robot was turned off by just cutting off the power. This was very unlikely to corrupt the SD card as nothing was written to it, but a lot of users felt uncomfortable with not doing a proper shutdown. Now a shutdown button in the web interface provides peace of mind.
  • more configuration options. The configuration webpage of the robot has been expanded to offer more options to control the movement of the robot.

As before, there are multiple options for getting hold of this software. If you already have an SD card then you can download an SD card image with all of the software installed here (go for the most recent version). Update: If you use our SD card image, please remember to expand it after installation by running

sudo raspi-config

and choose the ‘Expand Filesystem’ option.

Alternatively, we sell SD cards with the image preloaded in our store. Finally, for those who want to set up the software from scratch, or who want to modify it for their own robotic projects, we give full details for building the SD card, and installing all the software, here.

We believe that the Raspberry Pi is a great platform for robotics, and have got a number of tutorials for our Raspberry Pi Camera bot lined up for the coming months. If there’s anything in particular you’d like to see, please let us know.

 

The post New Software For Our Raspberry Pi Camera Robot appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/05/new-software-raspberry-pi-camera-robot/feed/ 5
Attending the Linux User Pi Jam http://blog.dawnrobotics.co.uk/2014/04/attending-linux-user-pi-jam/ http://blog.dawnrobotics.co.uk/2014/04/attending-linux-user-pi-jam/#comments Mon, 07 Apr 2014 19:00:29 +0000 http://blog.dawnrobotics.co.uk/?p=538 Last Saturday we attended the Linux User Pi Jam, in Poole, and had an absolute blast. The Jam was held at the RNLI college, which is a great location for an event, and we met some very interesting people at the Jam. We’re glad to see that robotics is rising in popularity as something to […]

The post Attending the Linux User Pi Jam appeared first on Dawn Robotics Blog.

]]>
linux_user_jam33565-RNLI-CollegeLast Saturday we attended the Linux User Pi Jam, in Poole, and had an absolute blast. The Jam was held at the RNLI college, which is a great location for an event, and we met some very interesting people at the Jam.

We’re glad to see that robotics is rising in popularity as something to do with your Pi. :) We ran a store at the Jam, and other robotics vendors there included The Little British Robot Company and PiBorg (with their very cool DoodleBorg tank). Ben from Phenoptix was also there with a neat little laser cut robot arm. We saw some cool Raspberry Pi robot projects from Leo White, and had a nice chat with Ian Renton, the guy behind the Raspberry Pi tank.

In the afternoon we gave a talk on combining the power of Arduino with the Pi to build Raspberry Pi robots as we have with our Camera Bot. For anybody interested the slides can be found here.

Pi Jams are a great way to meet interesting people, and get ideas for your next Pi project, if you haven’t been to one yet then we recommend that you keep an eye out for one in your area and head along. :)

The post Attending the Linux User Pi Jam appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/04/attending-linux-user-pi-jam/feed/ 1
Building a Raspberry Pi Robot – CamJam Video and Slides http://blog.dawnrobotics.co.uk/2014/04/building-raspberry-pi-robot-camjam-video-slides/ http://blog.dawnrobotics.co.uk/2014/04/building-raspberry-pi-robot-camjam-video-slides/#comments Thu, 03 Apr 2014 15:34:10 +0000 http://blog.dawnrobotics.co.uk/?p=526 A couple of months ago, Dawn Robotics went to the fantastic Cambridge Pi Jam organised by Michael Horne and Tim Richardson. Whilst there, I gave a talk on building a mobile robot with the Raspberry Pi (such as the camera robot we talk about in this blog post). The talk was aimed at quite a […]

The post Building a Raspberry Pi Robot – CamJam Video and Slides appeared first on Dawn Robotics Blog.

]]>
A couple of months ago, Dawn Robotics went to the fantastic Cambridge Pi Jam organised by Michael Horne and Tim Richardson. Whilst there, I gave a talk on building a mobile robot with the Raspberry Pi (such as the camera robot we talk about in this blog post). The talk was aimed at quite a high level, and looked at 7 key areas you should think about when building a mobile robot.

Well, I recently found out that there was a video of the talk online (along with videos of all the other talks at the Jam). Therefore I thought that I’d link to it here, along with the slides for the talk. The sound quality is not too great, and I need to work on my presenting style, but hopefully it’ll be useful to people thinking of building their own robot. If you’ve got any questions, or would like more information on any of the areas talked about in the video, please post on our forums.

Also, for those that are in the area, we’ll also be giving a talk at the Linux User Raspberry Pi Jam being held in Poole this Saturday (5th April 2014). A small number of tickets for the Jam are still available here. This talk will mainly be about using the Pi with an Arduino, but we’ll still try to squeeze in some robots. :)

The post Building a Raspberry Pi Robot – CamJam Video and Slides appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/04/building-raspberry-pi-robot-camjam-video-slides/feed/ 0
Using the Pi Co-op as a General Purpose I/O Board for the Raspberry Pi http://blog.dawnrobotics.co.uk/2014/03/using-pi-co-op-general-purpose-io-board-raspberry-pi/ http://blog.dawnrobotics.co.uk/2014/03/using-pi-co-op-general-purpose-io-board-raspberry-pi/#comments Sat, 15 Mar 2014 08:04:02 +0000 http://blog.dawnrobotics.co.uk/?p=496 We released the Pi Co-op, an Arduino add-on board for the Raspberry Pi back in the middle of January. But for various reasons, we haven’t had the time to do much promotional work, and explain to people why it’s so cool, and why you’d actually want to add an Arduino to your Pi. To fix […]

The post Using the Pi Co-op as a General Purpose I/O Board for the Raspberry Pi appeared first on Dawn Robotics Blog.

]]>
We released the Pi pi_co-opCo-op, an Arduino add-on board for the Raspberry Pi back in the middle of January. But for various reasons, we haven’t had the time to do much promotional work, and explain to people why it’s so cool, and why you’d actually want to add an Arduino to your Pi.

To fix that, we’ve created a video, because reading text can be really boring :) , and then, we’ve written this blog post to show you one of the really useful things you can do with the Pi Co-op. We show you how you can use your Pi Co-op as a general purpose I/O board for the Pi.

So now, instead of having to buy loads of different add-on boards for your Pi, you can just buy the Pi Co-op here. :) You can use it as an Analog to Digital Converter (ADC), you can use it to connect to 5V devices, you can use it to generate PWM signals, and you can use it for I2C. To top it all off, you can also control all of this functionality from a high level language such as Python.

Getting Started

It’s very easy to set up your Raspberry Pi to work with the Pi Co-op. Full details are provided in the manual, but as a quick recap, these are the steps you need to follow in Raspbian.

Open up a terminal window and run the following commands

sudo apt-get update
sudo apt-get install arduino git

This will install the Arduino IDE and some supporting Python libraries. Now run the following commands

git clone https://bitbucket.org/DawnRobotics/pi_co-op.git
cd pi_co-op
sudo python setup_pi_co-op.py install

This will alter configuration files to allow us to use the serial port on the GPIO pins of the Pi. Finish the configuration by restarting the Raspberry Pi

sudo reboot

If everything goes well, then you will now be ready to start programming the Pi Co-op.

Installing PyMata

To use the Pi Co-op as a general purpose I/O board we make use of a project called Firmata. Firmata is a program for an Arduino (the Pi Co-op is compatible with an Arduino Uno) that allows you to control all of its functionality using serial communication. The reason for doing this is, if you have a library that speaks the correct serial protocol with Firmata, then you can control the Pi Co-op with any language you want, and you don’t have to program it directly!

The Firmata Github page contains links to client libraries for most languages. For example there are libraries for Python, Javascript, Ruby and the .NET Framework, to name but a few. To show how this works, for this post we’re going to use Python and a library called PyMata. PyMata is an open source library that was written by Alan Yorinks. We’ve extended PyMata slightly so that it can also use a tool called Ino to automatically program the Pi Co-op with Firmata, if needed.

To get started with the installation. Open up a terminal window and enter the following

sudo apt-get update
sudo apt-get install python-pip python-dev python-serial
sudo pip install tornado ino

Now install PyMata by entering

git clone https://github.com/DawnRobotics/PyMata.git
cd PyMata
sudo python setup.py install

Blinking an LED with PyMata

The classic ‘Hello World!’ program to run on an Arduino is to blink an LED. The Pi Co-op has an LED so you can get it to start blinking by executing the following Python code on the Pi.

import time

from PyMata.pymata import PyMata

# Pin 13 has an LED connected on most Arduino boards.
# give it a name:
LED = 13

# Create an instance of PyMata.
SERIAL_PORT = "/dev/ttyS0"
firmata = PyMata( SERIAL_PORT, max_wait_time=5 )

# initialize the digital pin as an output.
firmata.set_pin_mode( LED, firmata.OUTPUT, firmata.DIGITAL )

try:
    # run in a loop over and over again forever:
    while True:

        firmata.digital_write( LED, firmata.HIGH ) # turn the LED on (HIGH is the voltage level)
        time.sleep( 1.0 ) # wait for a second
        firmata.digital_write( LED, firmata.LOW ) # turn the LED off by making the voltage LOW
        time.sleep( 1.0 ) # wait for a second

except KeyboardInterrupt:

    # Catch exception raised by using Ctrl+C to quit
    pass

# close the interface down cleanly
firmata.close()

When you run this program, if Firmata is already installed on your Pi Co-op then you should just see a short bit of connection text and the LED on your Pi Co-op should start blinking.

If Firmata isn’t installed on your Pi Co-op then you will see lots of text scroll past as PyMata compiles the Firmata code and uploads it to the Pi Co-op. This does take a little bit of time, but don’t worry, the next time you run the script, the connection process will be a lot quicker. Also, the compiled Firmata is cached, so that if you upload a different Arduino sketch to your Pi Co-op, then the next time you use PyMata it will just upload the already compiled version.

Code Explanation

Hopefully, if you’re familiar with the Arduino Blink sketch then the PyMata LED blink code should be fairly self explanatory. The important bits are

# Create an instance of PyMata.
SERIAL_PORT = "/dev/ttyS0"
firmata = PyMata( SERIAL_PORT, max_wait_time=5 )

This creates an instance of the PyMata class to connect to Firmata on the given serial port. The parameter max_wait_time specifies the time to wait in seconds when trying to connect to Firmata, before giving up and starting the upload process.

# initialize the digital pin as an output.
firmata.set_pin_mode( LED, firmata.OUTPUT, firmata.DIGITAL )

This line sets the LED pin as a digital output

        firmata.digital_write( LED, firmata.HIGH ) # turn the LED on (HIGH is the voltage level)
        time.sleep( 1.0 ) # wait for a second
        firmata.digital_write( LED, firmata.LOW ) # turn the LED off by making the voltage LOW
        time.sleep( 1.0 ) # wait for a second

Finally, these lines which form the body of the while loop, cause the LED pin to be written HIGH and then LOW in order to flash the LED pin.

Running the Other Examples

We’ve written a number of example scripts for using the Pi Co-op with PyMata, and put them in the examples folder of the Pi Co-op repository. The example we’ve just discussed is called pymata_blink.py, and there are also examples for reading from the ADC (pymata_analog_read.py) and controlling a servo (pymata_servo_sweep.py).

To make sure you’ve got the latest code, and then to run one of the examples, you can use the following commands

cd pi_co-op
git pull
./examples/pymata_blink.py

Taking Things Further

Using Firmata can give you a lot of flexibility when it comes to deciding how to use your Pi Co-op. If you stick with Python, then you can learn more about the functionality offered by PyMata by looking at the documentation here. Alternatively, you might decide that you want to use another language to control the Pi Co-op. There are lots of libraries to choose from, including the excellent Johnny-Five for javascript.

Also, if you’ve got any questions in general about the Pi Co-op, and how you can use it with your Raspberry Pi, then please head over to our forums, as we’re always happy to help. :)

The post Using the Pi Co-op as a General Purpose I/O Board for the Raspberry Pi appeared first on Dawn Robotics Blog.

]]>
http://blog.dawnrobotics.co.uk/2014/03/using-pi-co-op-general-purpose-io-board-raspberry-pi/feed/ 4