Experiment with ROS, Python and Raspberry Pi

I’ve always been a robotics geek and have started exploring ROS. This might help as a ‘how to get started’ post if you’ve wondered how to use ROS with the Raspberry Pi and want to stick to Python.

Why ROS?

Using ROS is likely absolute overkill for most home projects but I am working up to quite a complex project involving different sensors, GPS and at least two motors, so ROS is actually a good fit for where I’m headed.

It will also allow me to keep everything very decoupled and simple. ROS has a heap of features that will come in handy in the future e.g. logging, data play back, data management, test framework, state and config management and more.

I intuitively like the architecture because I know it will keep things simple and it has decent Python libraries which will make for speedy development. I don’t want to be writing a framework, rather I want to focus on motor, sensor and logic code.

Caveat

ROS only supports up to Python 2.7 which’ll be a problem if your Raspberry Pi HATs or pHATs have supporting Python 3 libs only.

Assumptions

I’m going to assume you know some programming, are comfortable on the linux shell, know the basics about Raspberry PI GPIO pins so won’t do much extra explanation.

Here we go…

Raspberry PI setup.

Put simple push switches on pins 18 and 24 on a breadboard.

image

Raspberry PI ROS installation

Follow the instructions over here.

If something fails, it’ll most likely to be an environment variable issue, e.g. for me this solved packages not being found.

export ROS_PACKAGE_PATH=/home/pi/catkin_ws/src:$ROS_PACKAGE_PATH

Also, I only installed the shell version, not the full desktop version.

The Python code

See my code in GitHub. Everything other than the scripts directory was autogenerated by a ROS.

I created a new ROS package first as explained here, my two python scripts live in the scripts directory. The code is based on this tutorial code.

To Run

You can configure a launch file #, but the simplest way to test this is to run the following in three separate shells:

All from within catkin workspace directory:

/home/pi/catkin_ws/

Run the ROS OS.

roscore

Run the talker

rosrun test talker.py

Run the listener

rosrun test listener.py

Test it

Press one one of the buttons and the output might look like:

pi@raspberrypi:~/catkin_ws/src/test/scripts $ rosrun test listener.py 
[INFO] [1556918461.361843]: heading 360
[INFO] [1556918461.964700]: heading 15
[INFO] [1556918462.867729]: heading 30
[INFO] [1556918463.667803]: heading 15
[INFO] [1556918464.167762]: heading 0
[INFO] [1556918464.372864]: heading 345
[INFO] [1556918464.778167]: heading 330

‘heading’ is just a variable name that corresponds to a compass heading where the buttons turn left or right deducting or subtracting 15 degrees from the previous value.

What’s next?

In this example, we see ROS decoupling sensor reading code from the action code. It would be possible to add more hardware to the PI and wire it together with more publisher/subscriber code and custom logic. See more on publishers and subscribers.

e.g. instead of just outputting data in the listener like this:

[INFO] [1556918464.372864]: heading 345
[INFO] [1556918464.778167]: heading 330

logic could be triggered to drive a motor, move a servo or trigger a solenoid. With a more complicated robot, ROS is ideal to keep the application simple and all parts decoupled, and allow incremental development.

Resources

RS Vareo Storm Sail, Meh!

It was the first race of the season yesterday with the same wind forecast by both the Met Office and Windfinder. It looked quite fruity from the shore, so I opted for the storm sail.

That turned out to be a mistake, firstly the wind wasn’t nearly as strong as forecast (race 11:00 – 12:00), in fact there was way more sea than wind.

Secondly I was reminded of what a dog the boat is upwind with a small sail, especially into waves and they were decent yesterday. The boat is heavy, it really needs that bigger sail.

Thirdly I’m used to the power of the standard sail so the whole sense of boat balance is thrown out with anything less. Downwind with the kite was fine but reaching was slow too.

I don’t think I’ll bother with the storm sail just for myself in the future (with child maybe) and will prefer to abandon a race if it’s too hairy rather than be too slow with a smaller sail.

Where’s the fun if you aren’t hanging off the back quarter on your toes (?) 🙂

Edit: 12th April, I went out similar conditions (14-18 kts) with the full sail the other day. So much better.

March, seems there’s a routine

This is a common sight when I’m prepping to go out, local fishermen out and about, sea birds lining up to pick up their throwaways. There’s only one bird in this picture, but a queue of birds is not uncommon. Herring gulls and lesser black backed gulls mainly.

It’s not surprising really as they’re out around high tide too, close to slack water the same as me. Not that I’m very tide dependent but pulling a boat out at a higher tide is less effort.

The sailing season starts soon, I can’t wait. The frostbiting season has been a success I’d say, and I’ve been out mostly once a week at least throughout the winter regardless of the cold. 🙂

DInghy Vareo 239 sailing in Swanage Bay
Photo Gill Richards

February 2019

I did quite a bit of work travel in the first weeks of February including a stopover on Holbox in Mexico. The Hobie Cat in the beach garden was tantalising, sadly unseaworthy amidst that beautiful environment with nice even onshore winds.

And this past week back in the UK I did a RYA Dinghy Instructor course (yep, passed 🙂 ). We were super lucky with the weather which was mild and apart from one day had enough of wind.

Photo credits Gill Richards

The DI course week was pretty intense with very packed days and homework in the evening. It feels a meaningful qualification to me, you can’t fake time on the water and sailing experience.

Apart from learning to teach sailing itself, the RYA instruction methods are excellent with much attention being paid to different learning styles, session planning, communications, feedback and everything else that goes into teaching effectively.

Not to mention learning new sailing and power boat skills, a lot on safety, protection of children and vulnerable adults, organising your training fleet and conducting sessions on land or water. All good stuff and also a nice consolidation of my own sailing skills where more advanced manoeuvres need to be at demo level and work every time.

As always, especially with sailing there is so much still to learn (it’s endless) and this stage still feels like the beginning. I’m grateful to Swanage Sailing Club for sponsoring the course and our great instructor during the week and coach/assessor on the final day.

If you’re in the UK and want to learn to sail check out the RYA website. It’s a great scheme to learn with, consistent in method across the country and you might be surprised to find a training centre not too far from where you are be it sea, river or lake.

January 2019 frostbiting

So this was a first, taking the boat cover off with actual frost on it and going for a sail.

Observations; fingers were cold and I need better gloves, particularly in light conditions where I’m not working hard and warming up. I switched to skiing gloves half way through which were good, though of course they would be useless if wet. I kept them dry.

I’ve added some more instrumentation to the boat in the form of extra tell tales and added leech tell tales where they’d worn out.

Looking at tell tales is nothing new to me, but having these extra ones really helps see airflow over the sail better.

New Year Sailing 2019

I’ve made a good start to the year so far with sails in my RS Vareo on New Year’s day and January 6th.

Since I go out on my own I’ve become more safety conscious and am taking out more gear, some items are on the UK’s dinghy cruising association page and it’s good common sense stuff. My boat isn’t designed for cruising but it’s certainly roomy enough to take out some safety gear and luxuries (coffee) including:

  • Handheld VHF radio + backup phone in a waterproof pouch.
  • Dry bag with different lengths of string, rope and shock chord, carbohydrate snacks, tape.
  • Water and coffee
  • Telescopic emergency paddle
  • Safety knife and whistle

I’ve experimented paddling a single handed and it works fine centering the rudder with a piece of shock chord tied to the toe straps. The knife is tied to the pocket of my buoyancy aid, the paddle further tied to the boat with a length of string. The other bits of string and rope should enable recovery from a few breakage scenarios I’ve imagined.

Correct clothing is an essential part and I have the choice of a drysuit and wet suit layers. The drysuit plus over trousers is less fiddly to put on and dries quickly but I prefer the wetsuit set up which is very comfy on the boat. If you have cold feet, I can recommend Rooster’s polypro and thermaflex socks.

Another obvious safety aspect is to choose the right conditions, so I’m choosing wind and tides which reduce risk and avoid overly gusty conditions to reduce breakage risk. I also avoid crazy actions in the chop, so take care to sail up and down waves rather than let the boat slam down. If more of us go out it’s fun to go out in stronger conditions, and generally if I had a newer boat I’d push it much more 🙂

We’re super lucky in Swanage to have a National Coastwatch station looking over us and I’m very grateful to have them there. There’s an RNLI station next door also overlooking the bay, but obviously no sailor wants to cause a call out.

My two hour session yesterday looked like this:

this was my first GPS tracking test, winds were light and I covered about nine miles.

It’s curious how shallow the downwind angles are and it looks like I might be beating better on starboard than port, it gives me more ideas for things to work on. I need to get better at working with wind shifts and choosing the right angles downwind. And I’ve started in earnest with off water fitness.

All in all, a good start to my sailing year.

Shell Bay 25th December 2018

These little birds are members of the sandpiper family and called sanderlings. We saw them on a visit to Shell bay (Dorset, UK).

They run along the shoreline at speed avoiding the waves, seem to like standing on one leg and even hop along at speed on one leg.

More views from Shell Bay.

I never tire of looking at the sea regardless of the weather. 🙂

December 2018

Frostbite sailing continues and I’m choosing windows between the storms to get out. It’s interesting to see how different the bay is at this time of year, days of persistent wind lead to curious wave patterns, some good for surfing, others not. It reminds me of school physics lessons exploring wave interference but now actually sailing through it.

I’m paying most attention to downwind sailing now and trying to make the most of waves. Upwind seems to have a good routine now.

I’m now much preferring the wetsuit gear over a drysuit. It is surprisingly warm and comfy on the boat.