Compiling an Arduino Sketch on the Pi
I am using an Arduino Uno to control the two dual H bridges I have controlling the motors.
The pi and the uno are connected using a USB cable. In order to up load sketches from the pi command line (I control the pi using SSH) I followed the tutorial https://thezanshow.com/electronics-tutorials/arduino/tutorial-11
I used sudo apt install arduino-mk to install the arduino-mk. However when I came to run make as directed by the tutorial I hit an error – avrdude: can’t open config file “/usr/share/arduino/hardware/tools/avr/etc/avrdude.conf”: No such file or directory. After a little searching I found a fix – https://groups.google.com/g/linux.debian.bugs.dist/c/EFFx-yaMo5Y
patch@patch:/usr/share/arduino/hardware/tools/avr> sudo mkdir etc
patch@patch:/usr/share/arduino/hardware/tools/avr> cd etc
patch@patch:/usr/share/arduino/hardware/tools/avr/etc>sudo ln -s /etc/avrdude.conf
Still following the tutorial I used sudo apt install screen So I can see the serial output from the arduino. Screen was already installed on my pi.
So after amending my blink.ino I can make it, upload it start screen and clean up after by using make upload monitor clean as my command.
Once I have finished with the monitor I can exit it by using ctr-a and ctr-d. Then see it is still there by screen -list , and then you can re-attach by screen -r
In order to upload more code to the arduino the serial connection needs to broken – screen -X kill does the trick.
Don’t forget to put the arduino sketch into an .ino file. This file along with a copy of the Makefile should be in it’s own directory. In that directory you can safely issue the make upload monitor clean command. and not have any conflicts with other .ino files.
patch_control.ino
Arduino sketch uploaded to the Uno from the pi. It is on github – https://github.com/patchnspotnami/patch
It is deigned to accept two integers – as strings, I tried but could not get integers to transmit nicely over serial. Just gave up and used strings instead :-). The first integer is the heading and the second is the time to move for, in seconds. These two integers are generated by AI in ptalk.py
On initialisation the sketch calculates the error in the MPU, and generates a correction factor. The robot yaw does drift slightly, about 1 degree every 3 seconds. I think this is because there is a finite time between readings of the MPU. To improve this the IMU could be read continuously, in a separate thread for example.
In loop it checks for new serial data coming in, if not it carries on with the previous command.
jmoving is the function that controls the robot, if the angle is greater than 90′ it spins until it is less than 90 and then moves forward. The relative PWM of each side is given by the graph: I think in the coding I made an error and -ve angles are to the right, not left as they should be. I did not bother to correct the coding merely giving it mirror commands.
ptalk.py
Python program to enable the pi to respond to questions and control the movement of the robot. I have already outlined this in my previous post.
I recently amended ptalk.py to include in the context sent to the AI an indication of the format I expect any moving command to come back in. This command is picked up in jspeak_syn the function that synthesises the speech. I had some trouble with the AI reply containing lots of spaces, so I had to strip these out. The function extract_numbers was written by Perplexity AI. The first time I have used code from an AI 🙂
Video
A sort video of Patch responding to questions and a command to move forward – https://youtube.com/shorts/VrGBR-Skw-I?feature=share
Notice wake word is immediately detects a female voice. I think this is a function of the training being on a female voice.