Hardware Reference
In-Depth Information
RANGE ; this is because the large pendulums do not swing as much as the small ones, so there
is a bit of an ampliication factor to make a slightly bigger display. When two pendulums are
used, the displacement distance at any time is simply the two pendulum readings added
together. As they swing back and forth you need to add a ixed ofset so that the swing will
be  in the centre of the window when you plot it on the Raspberry Pi. You will see in the
sendData() function the angle data from each pendulum can be combined in a number of
diferent ways depending on what commands have been received. hen inally the data is
split up, tagged and sent out of the serial port to the Raspberry Pi.
Programming the Pi
Finally you have come to the point where you want to take the data streaming in from the
pendulums and plot them onto the screen to see what patterns they make. he irst thing a
program has to do is to establish communication with the Arduino. When you plug an
Arduino into the USB port it can potentially appear on one of two ports, so you have to try
both of them. You want to send the data as fast as possible, so use the Arduino's top speed of
115200 baud. hen you have to open a Pygame window and command the Arduino to start
sending data by sending it the letter G for “go”, down the serial port. he full listing of the
Python program to do this is shown in Listing 16-4.
Listing 16-4 The Pendulum Pi Plotting Program
import piface.pfio as pfio # piface library
#!/usr/bin/env python
“””
Harmonograph plotter
Data comes from pendulums attached to an arduino and feeding
into the Pi through the USB serial port
version 2 with reduced byte transfer count
“””
import time # for delays
import os, pygame, sys
from pygame.locals import *
import serial
try:
ser = serial.Serial('/dev/ttyACM0',115200, timeout=2)
except:
ser = serial.Serial('/dev/ttyACM1',115200, timeout=2)
pygame.init() # initialise graphics interface
Search WWH ::




Custom Search