Summary
This project has been made to practise the use of sending instructions to various electronics for both the Arduino and the Raspberry Pi.
I’ve segmented this project into two parts/versions:
- Arduino - Mouse Control via Serial Communications Port
- Raspberry Pi - Mouse Control via Python Tkinter Program
Arduino
The arduino project is very simple, there is a program written in Processing which takes the mouse inputs send strings (enclosed in <>) to the Arduino via serial communications.
A small delay of 100ms is maintained to limit the rate of data coming to the Arduino board. The Arduino has a small serial buffer, it can only read 8 bytes at a time. Therefore we need to limit the rate of data to prevent a serial buffer overflow. If this issue occurs, then the servo will not be able to effectively read the data stream.
The Arduino has a sketch uploaded to the board that will do a number opperations.
- It will ingest the string data character by character from ‘<’ and terminate at ‘>’.
- It will concatenate all the characters into a single string.
- Split the string to serve as inputs to its respective components i.e. Servo1 and Servo2
- The inputs will be converted to integers to map the angle for the Servos.
- Actuate the Servos
Raspberry Pi
The Raspberry Pi project is using a Python script to drive the inputs. I’ve created a Tkinter program which maps the mouse position on the GUI to a pair of servo angles for the turret to move.
The mouse movements are passed into an event handler to locate mouse position on the window x,y coordinates. We use positional data on the mouse to calculate a servo angle based on the coordinate on the drawn window.
In order to acuate the servos, the angle inputs must be mapped to the corresponding duty cycle. The duty cycle can take any range of values between 2 and 12 for this particular servo. It handles angles in the range between 0 and 180 degrees.
The duty cycle can be calulcated like with this calculation:
1/18 * angle + 2
Then we send the pair of updated duty cycle values to the respective GPIO pins in order to drive the servo positions.