In Part One, we talked about the Interaction and Communications between the end user device (laptop/tablet/phone), the Web Server presenting the control panel, and the socket service listening for commands on the Robot itself.
This is the mode described here, with code examples for each programming platform. As part of my learning process, I chose a very simple DIY approach, as opposed to a framework like node.js
|Raspberry Pi / Arduino I2C|
We will take the incoming message, and repeat it via I2C to the Arduino that is managing the DC motors and wheel encoders.
Note: typically, there would be a validation step between receiving the command, and issuing it to the motor controllers.
You would want to ensure that the command made sense, but also ensure that it was not going to put the robot into danger. This would include proximity / obstacle detection, as well as "cliff detection". In more advanced robots, you may also have environmental sensors that could ensure that the path chosen was safe to travel.
Ok... so... in my implementation, I rely heavily on the Adafruit Raspberry Pi python library for I2C communications both for existing I2C sensors, as well as for communicating with my Arduino's. I fully admit to replicating an existing I2C sensor library, and then making it work with my motor controller Arduino.
provides a number of methods for sending and receiving data via I2C, in 8bit bytes, 16bit integers, or character array (python list) form.
I'm not going to explain how to prepare your Pi for I2C...
please go here -> Adafruit: Configuring the Pi for I2C
I had looked at Quick2Wire, and WiringPi, but their implementations is based on python3, whereas it appears that more I2C connectivity libraries exist for python 2.x via Adafruit.
Here is my stripped down "library" for sending a command string to the Arduino Motor Controller via I2C.
With no further ado... here is my python "command repeater" aka tcp socket listener-I2C Master.
Please read the comments. This is a bare-bones implementation that takes a command string, and forwards it to The arduino via I2C for processing.
You will need to add your proximity and safety testing to this. As well, sending a string via I2C is not the most efficient use of the bus. I simply did this so that I had both options available on the Arduino.
A proper Data Register method will be shown in an upcoming article.
As you will see in the next segment of this series, I share the Command Interpreter / processor between the serial port and the I2C. Commands can be received via either.
Adafruit: Configuring the Pi for I2C