RASPBERRY PI: arduino + (processing/python)
Posted by kll on November 18 2013 15:07:28
as in Google+ there was the question how to show graphs with measuring data from a arduino
use arduino and processing on a raspberry
# from

here only very short the steps
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install arduino
sudo apt-get install arduino-mk avrdude-doc equivs libjna-java-doc icedtea-plugin libnss-mdns fonts-ipafont-gothic fonts-ipafont-mincho ttf-wqy-microhei ttf-wqy-zenhei ttf-indic-fonts




sudo apt-get install librxtx-java openjdk-6-jdk
( pls see now there is even a JDK8 available, also see here and here )
wget http://processing.googlecode.com/files/processing-1.5.1-linux.tgz
tar -xvzf processing*.tgz

cd /home/pi/processing-1.5.1/

rm -rf java
#ln -s /chemin/vers/openjdk java

ln -s /usr/lib/jvm/java-6-openjdk-armhf java

mv modes/java/libraries/serial/library/linux32/librxtxSerial.so modes/java/libraries/serial/library/linux32/librxtxSerial.so.old

mv modes/java/libraries/serial/library/RXTXcomm.jar modes/java/libraries/serial/library/RXTXcomm.jar.old
cp /usr/share/java/RXTXcomm.jar modes/java/libraries/serial/library/

ls modes/java/libraries/serial/library/ #to check

cd /home/pi/sketchbook/
mkdir libraries

cd libraries

wget http://controlp5.googlecode.com/files/controlP5-1.5.2.zip
unzip controlP5-1.5.2.zip

wget http://playground.arduino.cc/uploads/Interfacing/processing-arduino.zip

unzip processing-arduino.zip
cd arduino/library/
mv Arduino.jar arduino.jar


on desktop in filemanager /home/pi/processing-1.5.1/
dblclick processing .shell script
[EXECUTE] [ok] for sketch folder
warning JAVA VM [ok] WAIT
example pie chart shows

test sketch | import library | and find arduino and controlP5

while the start IDE and start sketch/run is very slow, a running processing sketch still has game-speed, even via VNC!



#________________________try upgrade to new version
wget http://download.processing.org/processing-2.1-linux32.tgz
tar -xvzf processing-2.1-linux32.tgz
cd /home/pi/processing-2.1/
rm -rf java
ln -s /usr/lib/jvm/java-6-openjdk-armhf java


on desktop in filemanager /home/pi/processing-2.1/
dblclick processing .shell script
EXECUTE
warning "Not fond of this JAVA VM" [ok] WAIT

example pie chart shows

example graphic planets load minutes and shows glget error... failed

cd /home/pi/sketchbook/libraries/
delete or rename old libs

wget http://www.sojamo.de/libraries/controlP5/download/controlP5-2.0.4.zip
unzip controlP5-2.0.4.zip

wget http://playground.arduino.cc/uploads/Interfacing/processing2-arduino.zip
unzip processing2-arduino.zip
cd arduino/library/
mv Arduino.jar arduino.jar

wget http://kll.engineering-news.org/kllfusion01/downloads/PoorManScope_V302b.zip


#_________________serial/String problem with 2.1




change back to processing version 2.0.3

wget http://download.processing.org/processing-2.0.3-linux32.tgz
tar -xvzf processing-2.0.3-linux32.tgz
cd /home/pi/processing-2.0.3/
rm -rf java
ln -s /usr/lib/jvm/java-6-openjdk-armhf java

mv modes/java/libraries/serial/library/linux32/librxtxSerial.so

modes/java/libraries/serial/library/linux32/librxtxSerial.so.old

mv modes/java/libraries/serial/library/RXTXcomm.jar

modes/java/libraries/serial/library/RXTXcomm.jar.old
cp /usr/share/java/RXTXcomm.jar modes/java/libraries/serial/library/

cd /home/pi/sketchbook/libraries/

delete or rename old libs

wget http://www.sojamo.de/libraries/controlP5/download/controlP5-2.0.3.zip
unzip controlP5-2.0.3.zip



use the processing sketch PMS3 (without arduino and controlP5 libs in subdir data / because that is now in the sketchfolder

PMS3 test run ends with expected arrayIndexOutOfBoundsException from Serial.list()[2]
its time to get the arduino running!

well, the RPI is at the limit,
using my PMS3 (poor man scope 3 beam )
i send 3 values of 360 records for one batch at 115200Bd
and with arduino terminal and processing sketch PMS3 the RPI is on 100% load,
even i put in arduino delaytimer in the batch and in the record send. ( a 9600Bd did not help )
also this all has nothing to do how fast the scope works ( example 15kHz batch modus)
problem is the datatransport to RPI. ( the original was tuned alread with delaytimer for the big desktop PC ( what also was running on the limit ),
so it is more a java + IDE serial software problem.
The RPI USB linux itself is no problem,
as a large file copy SDcard to USB stick goes with 10MB/s



here i connected A2 to 3v3, A1 to 5v and A0 touch with hand / get stray 50Hz
so 50Hz*360pix =18kHz sampling => 1 sinus would fit in the window

pls see in both pictures that with arduino terminal OR processing PMS3 window
RPI cpu is on 100% load.




when we want to get the datastream with python we need:
sudo apt-get install python-serial
i just leave the arduino running as is and get a batch
1 headerline and 360 datalines, to array and save to file (5.5kB)
allow 10 files in temp fs, as a ringbuffer.

as no processing is running the python sketch ( on the slowed down arduino / 1 batch every 146sec only) now runs on 10 .. 15% cpu.
But i will start tuning the arduino only AFTER i get the processing running, as a file reading version instead of serial communication. BUT that means also NO ARDUINO OPERATION possible from there.

the empty processing scope screen
/ no serial / no data / noloop / no buttons /
still needs more as a minute to start,
but the python job, creating the batch files from arduino serial,
seems undisturbed.
and the now inactive scope window of processing and python in background takes about 20%load.
to update the "scope" press [space]
and in 1 sec the new data are read, ( handeled by a textfile what points at the newest ringbuffer file )
but need 6 sec more to redraw the scope screen.


but starting now the arduino IDE additionally to make some tuning there, RPI cpu stays at 100% and arduino load after 5min?.
Thats definitely too much for RPI.
the python service must be stopped anyhow for arduino IDE to can communicate with AVR.
this code
sudo nano /home/pi/python_cam/arduino_stream_PMS3.py
next step: make the python autostart
-- make it executable
chmod +x /home/pi/python_cam/arduino_stream_PMS3.py
-- auto start it at boot with this code
sudo nano /etc/init.d/arduino_stream_PMS3
sudo chmod 755 /etc/init.d/arduino_stream_PMS3
sudo update-rc.d arduino_stream_PMS3 defaults

optional remove auto start
sudo update-rc.d -f arduino_stream_PMS3
for start
sudo /etc/init.d/arduino_stream_PMS3 start
for stop ( f.e. to run arduino IDE )
sudo /etc/init.d/arduino_stream_PMS3 stop
check with top for python, if it still shows
for (re) connect arduino USB need stop / start ( restart )

p.s. with TOP i see for python only 1 .. 1.3% CPU time and 1.6%memory
start processing:
after 1min cpu 100% then java 13%
load PMS3RPI 6 sec then java 15%
run PMS3RPI 100 sec then java 12 .. 15% and scope window shows
[space] 14sec show new data


now work on the timing inside arduino
( stop python service and start arduino IDE )
still have to work on the init!
pyton now needs 1.3% cpu time ( and the batches come every 146 sec). NOW CHANGE:
delay [ms] after batch ( so RPI can process file... ) 2000 .. 1000
delay [ms] each recordline 400 .. 200
pyton now needs 4% cpu time ( and the batches come double as fast ( every 73 sec ))
delay [ms] each recordline 200 .. 50
pyton now needs 11% cpu time ( and the batches come every 19 sec)
delay [ms] each recordline 50 .. 30
pyton now needs 17% cpu time ( and the batches come every 12sec)
as the scope file load / screen update needs about same time i don't need to push it more.
here the sketch code for arduino and processing ( and reduced libs )

-+- just a idea, i could change the 10 file ring buffer to 100 files ( no ring)
and zip them away to usb stick. "digital storage oscilloscope" is the word!
-++- i miss the operation of the arduino, but there can now be only one way, it must be at the start of the python script.
i could make a text file with the settings for arduino:
'-' for faster scanrate ( 35kHz) ( only scope1) or '+' for slower...
'C' for change channel of scope3 from A2 to A3,A4,A5,A1,A2...
and load it at start to python and via serial to arduino.

-+++- if JDK8 would help about the speed problems?




+-+ or i give up on processing and try to make the graphs in python
see here and here and here
matplotlib
QWTlib




As i first try to test that on my WIN7 PC ( because i need to learn more python fast,)
i install PYTHON 2.7.6 windowsinstaller on it and with the help of python winlib got lots of extension libs.
needed needed serial lib




again on RPI
sudo apt-get install python-matplotlib (21MB)
optionally:
sudo apt-get install python-matplotlib-doc (70MB!)
and find info at /usr/share/doc/python-matplotlib-doc/html/index.html
using the subplot_demo.py example

at first time it was very slow, but from second start the graph needed about 8 sec to load.
now have this code zip
nano /home/pi/python_cam/PMS3scope.py
run from IDLE

chmod +x /home/pi/python_cam/PMS3scope.py
python /home/pi/python_cam/PMS3scope.py
and execute it direct from termina l


as i run the PMS3scope.py today from TV ( normally work headless putty VNC )
i found that it needs only 3 sec to UPDATE!!
so the timing in arduino could be changed to much faster.

also i see that when i touch the arduino A0 the USB communication to RPI is disturbed, and the scope gets batch not fit to the 360 value.. there is no check on that in both python scripts.
the scope runs into errors from matplotlib but not stop/ can recover when good data arrive.
anyhow that was just a test.

in V2 i change
- PMS3RPI.ino arduino timing
. . .( 500ms wait batch / 20ms wait record ) ==> 8sec update time
. . .( 500ms wait batch / 10ms wait record ) ==>4.3sec update time cpu 50%
. . .( 500ms wait batch / 15ms wait record ) ==>6.1sec update time cpu 35..40%

- arduino_stream_PMS3.py ( no change )
- PMS3scope_v2.py
. . .count and show only if ( 3 value data-records ) count = 360
. . .new CONTI button ( reset by MANUAL update button ) now there is a internal timer ( 2 sec )
and if CONTI is enabled there is a check if there is a new/different filename ( in /run/shm/current.txt )
and then a get_data is done.

pls see the load and that in the list there seems that he missed one ( number 9 ) file
so there is reason why we startup in manual ( update) mode.

in ver 3
3 more buttons for the arduino operation
what write a text file with the character to be send to arduino ( usb menu ) to operate there:
'+' '-' batch speed oscilloscope ( on arduino max 35kHz 1 channel , on MAX32 500kHz 1 channel )
'C' beam 1 = A0, beam 2 = A1, beam 3 = select A3..A4..A5..A0..A1..A2

pls see menu buttons, the file and file content

at 35kHz arduino send only data from A0
hmm, i got confused with the batch timing!
in arduino i programmed a WAIT after the batch data are send.
that is wrong! the RPI can not use that time because it does not understand that the batch is finished
( 360 rec counter not used for program control ) it waits for the headerline to understand that new batch comes.
sketch flow is now changed like this:
arduino: PMS3RPI (Ver 3.2.R.3)
- read 360 records to array
- write the summary information line ( of this new batch )
++ wait ( give RPI time to file processing of last batch)
- send the batch data 360 lines with 1024,1024,1024, ( or 1024,0,0 for highest sample frequency )
RPI: arduino_stream_PMS3 (v3)
- reads USB line
++ if long line it must be summary line >> save buffer to file >> reset buffer
- save line to buffer
( now with some manual mods run on WIN7 PC also )


with
chmod +x /home/pi/python_cam/PMS3scope_v3.py
python /home/pi/python_cam/PMS3scope_v3.py

and switch to CONTI MODE get


download




as my system now is messed up and i want back and play with RPI CAM again,
i also think about new ways "to serve",
my blog is first for me, because i plan my work here prior to play with hardware and software,
that's why it is so detailed, i write it first ( directly in the CMS / or in a text file on my desktop )
and use the copy paste ( to PUTTY window ) to test / improve it.
also inside RPI desktop i use a text file / snipplet to work with terminal ( to fight my alzheimer )
the code ( file content) can be in separate text files shown from BLOG and finally in a ZIP file to download.
screenshots from the desktop with FTP to BLOG CMS and called in articles is only a matter
of seconds. perhaps you noticed that in this article i used a different way to show them,
i did not format the width like i did in the BLOGs before.
but i like that effect that when the browser is full screen, the screen snaps are shown very big /
( over the limits of the CMS template layout ), when the browser window is reduced, the images are automatically fitted in the BLOG column.
( but the mouse right click |show image| was also not too difficult to show a image detailed )

That for the DIY thinkers,
but there might be people who are not interested in the details,
they just want it running and play with it.
I can not do self installing programs...
RPI has the way to copy/burn whole SD cards,
but handling of the min. 4GB files is too time consuming.

My idea is to make berry-boot backups and put them up for download.
so all you have to know is how to install a additional system with berry-boot from USB stick...
so you can play and not fuckup your private system.
now press / hold mouse at ADD OS and you see the option to find the backups you did to USB stick.
that is how you can backup / recover a whole OS, even to a new berry-boot SD.
the recover button in the menu has nothing to do with the backup..
with that you can reset / init a installed OS to its defaults, means delete all your user settings !!

but to do this i must start from scratch.
ok, in 4 hours i did it, but the idea was not that good, the berryboot backup is 1.7GB,even as ZIP its 830MB,
i will not copy to this server,



but here also the comprehensive DIY info

so back to RPI camera, where i restart with same job, build it again