Towards a Generic Architecture for Building Modular CPS as Applied to Mobile Robotics

Pablo Gonz�lez-Nalda1, Ismael Etxeberria-Agiriano1, Isidro Calvo2
1Lenguajes y Sistemas Inform�ticos
2 Ingenier�a de Sistemas y Autom�tica
Escuela Universitaria de Ingenier�a de Vitoria-Gasteiz

University of the Basque Country (UPV/EHU), Vitoria-Gasteiz, Spain

{pablo.gonzalez, ismael.etxeberria, isidro.calvo}@ehu.eus

Code to stream a webcam capture connected to the Raspberry Pi

#!/bin/bash cvlc -vv v4l2:///dev/video0:width=50:height=50:fps=2:chroma=MJPGJ
:sout="#transcode{vcodec=mp4v,vb=500,scale=1,acodec=mp4a,ab=128,channels=2,samplerate=8000}:rtp{sdp=rtsp://:8080/test.sdp}"
:sout-keep 2>/dev/null &

Code to visualise the stream

vlc rtsp://:8080/test.sdp

Code to control two servos in Arduino

#include <Servo.h>
Servo sd, sm; // create servo object to control a servo

const int sdPin = 6; // steering direcc
const int smPin = 3; // motor
int Min = 20; // minimum value
int Max = 160; // maximum value
int vMin = 80; // minimum value
int vMax = 120; // maximum value
String cadena;
byte d,v;

void setup() {
sd.attach(sdPin); // pin 6 servo Steering Direccion
sm.attach(smPin); // pin 3 servo Thrust Motor
sd.write(90); // dir
sm.write(90); // th

Serial.begin(9600);
Serial.flush();
delay(30); // waits for the servo to get there
}

void loop() {
while (!Serial.available()) {
} // wait for data to arrive
while (Serial.read() != 10) {
}
d=Serial.read();
v=Serial.read();

if (d != 255 && v != 255) {
if (d> Max) d = Max;
if (d< Min) d = Min;
if (v>vMax) v = vMax;
if (v<vMin && v>=5) v = vMin;
// cadena=String(d)+" --- "+String(v);
// Serial.println(cadena);
sd.write(d); // dir
sm.write(v); // th
}

delay(20); // waits for the servo to get there
}

Part of Python code that sends data to the Raspberry Pi via serial USB

import signal
import os
import threading
import socket
import atexit

########## Codigo para leer del USB serie
import serial
import time
import sys

import struct

#############################################################################
########## Codigo para start
#############################################################################
def start(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(('',self.port))
self.arduino=serial.Serial('/dev/ttyACM0',9600, timeout=3)
time.sleep(2) # wait for Arduino

#############################################################################
########## Codigo para update_servos
#############################################################################
def update_servos(self, d, v):
print "MOVE: \t" +str(d)+" \t"+str(v)
mensaje = struct.pack('B', d) # packing the number as a binary
self.arduino.flush()
self.arduino.write(mensaje)
mensaje = struct.pack('B', v) # packing the number as a binary
self.arduino.flush()
self.arduino.write(mensaje)
mensaje = struct.pack('B', 10) # packing the number as a binary
self.arduino.flush()
self.arduino.write(mensaje)

##### ppal
pid = os.getpid()
f = file(PIDFILE,"w")
f.write(str(pid))
f.close()
r = RobotServer(1337)
for i in range(50):
try:
signal.signal(i, do_exit)
except Exception:
pass
try:
r.start()
except Exception:
exit(r)

App with webcam image

Image