Skip to content

Commit

Permalink
Commented and cleaned code
Browse files Browse the repository at this point in the history
  • Loading branch information
simone.morettini committed May 18, 2020
1 parent 1d806f6 commit 89f0c2b
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 130 deletions.
24 changes: 13 additions & 11 deletions ADC_parallel/ADC_parallel.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//Arduino code for acquire the analog signal from the current sensor and convert it in a digital signal

int analogPin = A0 ; //input pin (current measure)

int pin0 = 2; //output pins
Expand All @@ -19,7 +21,7 @@ int val = 0 ;
void setup ()
{
Serial.begin(9600); //check bitrate in function of desired measure frequency
pinMode(pin0,OUTPUT);
pinMode(pin0,OUTPUT);
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
Expand All @@ -29,14 +31,14 @@ void setup ()
pinMode(pin7,OUTPUT);
pinMode(pin8,OUTPUT);
pinMode(pin9,OUTPUT);

}

int i=0;
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
return (float)(x-in_min)*(out_max-out_min)/(in_max-in_min)+out_min;
}
//float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
//{
// return (float)(x-in_min)*(out_max-out_min)/(in_max-in_min)+out_min;
//}

void loop ()
{
Expand All @@ -47,9 +49,9 @@ void loop ()
float voltage= val * (5.0 / 1023.0);
//val=mapfloat(val, 0, 1023, 0, 5);
Serial.println(String(voltage)+" V");

Serial.println(String(voltage-offset)+" V OffSetted");

//Sensitivity of the sensor: 625 mV/Ipn. Ipn = 50A, so the slope is 12.5 mV/A
float current = (voltage-offset)/0.0125;
Serial.println(String(current)+"A");
Expand Down Expand Up @@ -82,10 +84,10 @@ void loop ()
Serial.println(bitRead(val,6));
Serial.println(bitRead(val,7));
Serial.println(bitRead(val,8));
Serial.println(bitRead(val,9));
Serial.println(bitRead(val,9));
}

delay(20);
i+=1;

}
11 changes: 7 additions & 4 deletions Measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
_DEBUG_ENABLED_=False




# Set the Hotkeys to get the event from the Keyboard
global event
event = "Still"
Expand Down Expand Up @@ -78,7 +76,9 @@ def setT(ev):


class ADXL345:

'''
Class for acquisition of accelerometer signals.
'''
address = None

def __init__(self, address=0x53):
Expand Down Expand Up @@ -138,14 +138,15 @@ def getAxes(self, gforce=False):

return {"x": x, "y": y, "z": z}

#TODO:: If while(true) is used, after a while it crash
#Main script
if __name__ == "__main__":
# if run directly we'll just create an instance of the class and output the current readings
adxl345 = ADXL345()
# Try to catch the Interrupt and close the file saving the results
try:
# Interval to update measures
timeInterval = 0.020

dimDer = 2
dimStd = 10
dimFilter = 10
Expand Down Expand Up @@ -200,6 +201,8 @@ def getAxes(self, gforce=False):
values['currentOut'] = currentOut

state=m.runOneStep(values)

##Using to have nice plot for testing
if(state=="Still_state"):
values["event"]=10
elif(state=="Moving_state"):
Expand Down
15 changes: 11 additions & 4 deletions Measurement_emoncms.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
'''
This file contains a working variant of Measurement.py for storing the data on a web server
in real time.
It needs to be updated with the state machine and the new features evaluated using utils.py.
Now for the detection of the state only a simple threshold method is used.
'''

import smbus
from time import sleep,time
import csv
Expand Down Expand Up @@ -123,7 +131,7 @@ def getAxes(self, gforce=False):
axes['pitch'] = math.atan2(-1*axes['x'], math.sqrt(axes['y']*axes['y']+axes['z']*axes['z'])) * 180 / math.pi

# Set initial data at first iteration
if(i==0):
if(i==0):
x = axes['x']
y = axes['y']
z = axes['z']
Expand All @@ -141,7 +149,7 @@ def getAxes(self, gforce=False):
Vx = Vx + timeInterval * x
Vy = Vy + timeInterval * y
Vz = Vz + timeInterval * z


# Check for the event [TODO] take into account also roll, pitch, mean, std, ...
axes["event"] = 0#"WOW"
Expand All @@ -157,7 +165,7 @@ def getAxes(self, gforce=False):
# Print to csv
writer.writerow(axes.values())

# api-endpoint
# api-endpoint
if(i % 50 == 0):
date = time.time()
URL = "https://emoncms.org/input/post?node=1&time=" + str(date) +"&csv=" + "x:"+str(x) + "," + "y:"+str(y) + "," + "z:"+str(axes["z"]) + "," + "norm:"+str(axes["norm"]) + "," + "roll:"+str(axes["roll"]) + "," + "pitch:"+str(axes["pitch"]) + "," + "event:"+str(axes["event"].split(":")[-1]) + "&apikey=" + apikey
Expand All @@ -175,4 +183,3 @@ def getAxes(self, gforce=False):
print("Exception: " + str(e))
finally:
f.close()

50 changes: 24 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
# Robotics Perception & Action Project 2019-20

# NEW CLEANED REPOSITORY

## Group 14 - Safety Module

* Session 10/12 : Connected MEMS to RaspberryPi. Data acquisition functioning correctly.
Future steps :
* Work on current transducer
* Setting up ADC with arduino
* Current tests with LFG and oscilloscope
* Implement threshold solution

* Session 11/12:
* Added Threshold and data in the software
* Prepared the Arduino

* Session 12/12 :
* Projected the dashboard
* Prepare the current sensor

Team members:
- Boffo Marco
- Morettini Simone
- Rosales Pablo
- Rousseau Thomas

## Setup


### Linux

```
Expand All @@ -41,21 +28,32 @@ Activate environment:
Install dependencies
`pip install -r ./requirements.txt`

### IMPORTANT INFO
Could be usefull follow this link https://www.raffaelechiatto.com/modificare-permessi-di-esecuzione-di-script-in-powershell/

Rememeber to
### Run Script

git clone https://github.com/MesSem/RP-A_data.git
By running the script Measurement.py the module will collects data, analyses them and uses the
state machine to identify the different states.

and to commit and push inside the folder RP-A_data
```
python Measurement.py name_file seconds (ex: python Measurement.py test_run_1 10)
```

### Run Script to collect Data
The script will print the actual state in the terminal. For use this module inside a mobile robots, the print of the state should be changed. Depending on the design of the system can be establish that some pins become high depending on the state of the module.

python Measurement.py name_file seconds (ex: python Measurement.py test_run_1 10)
## Important info

To leave this repository less heavy we put the data in another repository.
Rememeber to
```
git clone https://github.com/MesSem/RP-A_data.git
```
and to commit and push inside the folder RP-A_data

https://www.raffaelechiatto.com/modificare-permessi-di-esecuzione-di-script-in-powershell/
for store new data and save on Github.

#### To open and analyse the data DataAnalisys.ipynb
### Open and analyse the data DataAnalisys.ipynb (it's in RP-A_data.git)
```
pip install notebook
jupyter notebook
```
12 changes: 8 additions & 4 deletions STD&DrvTest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# importing the requests library
import requests
'''
Script used for testing standard deviation and derivative
'''

# importing the requests library
import requests
import random
import time
import pandas as pd
Expand Down Expand Up @@ -39,8 +43,8 @@
values.update(row)
values.update(utils.calculateSTD())
values.update(utils.calculateDerivative())

writer.writerow(values.values())


f.close()
f.close()
75 changes: 40 additions & 35 deletions digital_acquisition.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'''This file contains the method to acquire the data of the current converted in digital by the Arduino'''

import RPi.GPIO as GPIO
import time
import csv
Expand All @@ -17,47 +19,50 @@
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #arduino 2 corresponding to 2^0 pin
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #arduino 5

'''Function to read the data received from the Arduino and to convert them in the corrisponding value in ampere'''
def read_current():
val = GPIO.input(17)+GPIO.input(27)*2**1+GPIO.input(22)*2**2+GPIO.input(10)*2**3+GPIO.input(9)*2**4+GPIO.input(11)*2**5+GPIO.input(5)*2**6+GPIO.input(6)*2**7+GPIO.input(13)*2**8+GPIO.input(19)*2**9

voltage = val * (5.0 / 1023.0)

#Sensitivity of the sensor: 625 mV/Ipn. Ipn = 50A, so the slope is 12.5 mV/A
offset = 2.455 # Biased sensor
val = (voltage-offset)/0.0125
return val

def old_procedure():
try:
f = open("arduino.csv.txt", "w+")
writer = csv.writer(f)
head = ["value"]
writer.writerow(head)
while(True):
#print(GPIO.input(10))
# Get the value
val = GPIO.input(17)+GPIO.input(27)*2**1+GPIO.input(22)*2**2+GPIO.input(10)*2**3+GPIO.input(9)*2**4+GPIO.input(11)*2**5+GPIO.input(5)*2**6+GPIO.input(6)*2**7+GPIO.input(13)*2**8+GPIO.input(19)*2**9
writer.writerow(str(val))
print(str(val))

if(DEBUG_MODE):
print(GPIO.input(17))
print(GPIO.input(27))
print(GPIO.input(22))
print(GPIO.input(10))
print(GPIO.input(9))
print(GPIO.input(11))
print(GPIO.input(5))
print(GPIO.input(6))
print(GPIO.input(13))
print(GPIO.input(19))


print()
time.sleep(1)
except KeyboardInterrupt as k:
print("Keyboard: " + str(k))
except Exception as e:
print("Exception: " + str(e))
finally:
f.close()

#Old procedure used to test the data acquisition. Not used anymore'''
# def old_procedure():
# try:
# f = open("arduino.csv.txt", "w+")
# writer = csv.writer(f)
# head = ["value"]
# writer.writerow(head)
# while(True):
# #print(GPIO.input(10))
# # Get the value
# val = GPIO.input(17)+GPIO.input(27)*2**1+GPIO.input(22)*2**2+GPIO.input(10)*2**3+GPIO.input(9)*2**4+GPIO.input(11)*2**5+GPIO.input(5)*2**6+GPIO.input(6)*2**7+GPIO.input(13)*2**8+GPIO.input(19)*2**9
# writer.writerow(str(val))
# print(str(val))
#
# if(DEBUG_MODE):
# print(GPIO.input(17))
# print(GPIO.input(27))
# print(GPIO.input(22))
# print(GPIO.input(10))
# print(GPIO.input(9))
# print(GPIO.input(11))
# print(GPIO.input(5))
# print(GPIO.input(6))
# print(GPIO.input(13))
# print(GPIO.input(19))
#
#
# print()
# time.sleep(1)
# except KeyboardInterrupt as k:
# print("Keyboard: " + str(k))
# except Exception as e:
# print("Exception: " + str(e))
# finally:
# f.close()
4 changes: 3 additions & 1 deletion emo.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<!-- Html code used for creating the dashboard on Emocms -->

<div id="2" class="graph" style="position: absolute; margin: 0px; top: 60px; left: 0px; width: 840px; height: 540px;" graphid="17150">
<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="/graph/embed&amp;graphid=17150" style="width: 840px; height: 540px;"></iframe>
</div>
<div id="6" class="dial" style="position: absolute; margin: 0px; top: 120px; left: 980px; width: 140px; height: 140px;" feedid="403366" max="4" scale="0" units_dropdown="__other" units="G" decimals="4" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed="">
<canvas id="can-6" width="140" height="140"></canvas>
<div id="can-6-tooltip-1"></div>
<div id="can-6-tooltip-2"></div>
</div><div id="7" class="heading-center" style="position: absolute; margin: 0px; top: 0px; left: 440px; width: 380px; height: 60px;"><h1><strong>Safety Module<strong></strong></strong></h1></div><div id="11" class="heading-center" style="position: absolute; margin: 0px; top: 140px; left: 860px; width: 100px; height: 100px;"><h1>X</h1></div><div id="12" class="heading-center" style="position: absolute; margin: 0px; top: 300px; left: 860px; width: 100px; height: 100px;"><h1>Y</h1></div><div id="13" class="heading-center" style="position: absolute; margin: 0px; top: 460px; left: 860px; width: 100px; height: 100px;"><h1>Z</h1></div><div id="14" class="dial" style="position: absolute; margin: 0px; top: 280px; left: 980px; width: 140px; height: 140px;" feedid="403367" max="4" scale="" units_dropdown="__other" units="G" decimals="4" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-14" width="140" height="140"></canvas><div id="can-14-tooltip-1"></div><div id="can-14-tooltip-2"></div></div><div id="15" class="dial" style="position: absolute; margin: 0px; top: 440px; left: 980px; width: 140px; height: 140px;" feedid="403368" max="4" scale="" units_dropdown="__other" units="G" decimals="4" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-15" width="140" height="140"></canvas><div id="can-15-tooltip-1"></div><div id="can-15-tooltip-2"></div></div><div id="17" class="graph" style="position: absolute; margin: 0px; top: 640px; left: 0px; width: 840px; height: 440px;" graphid="17160"><iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="/graph/embed&amp;graphid=17160" style="width: 840px; height: 440px;"></iframe></div><div id="20" class="heading-center" style="position: absolute; margin: 0px; top: 720px; left: 870px; width: 100px; height: 90px;"><h1>Roll</h1></div><div id="21" class="dial" style="position: absolute; margin: 0px; top: 870px; left: 990px; width: 150px; height: 150px;" feedid="403371" max="180" scale="" units_dropdown="__other" units="°" decimals="-1" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-21" width="150" height="150"></canvas><div id="can-21-tooltip-1"></div><div id="can-21-tooltip-2"></div></div><div id="22" class="dial" style="position: absolute; margin: 0px; top: 690px; left: 990px; width: 150px; height: 160px;" feedid="403370" max="180" scale="" units_dropdown="__other" units="°" decimals="-1" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-22" width="150" height="160"></canvas><div id="can-22-tooltip-1"></div><div id="can-22-tooltip-2"></div></div><div id="23" class="heading-center" style="position: absolute; margin: 0px; top: 900px; left: 870px; width: 100px; height: 60px;"><h1>Pitch</h1></div>
</div><div id="7" class="heading-center" style="position: absolute; margin: 0px; top: 0px; left: 440px; width: 380px; height: 60px;"><h1><strong>Safety Module<strong></strong></strong></h1></div><div id="11" class="heading-center" style="position: absolute; margin: 0px; top: 140px; left: 860px; width: 100px; height: 100px;"><h1>X</h1></div><div id="12" class="heading-center" style="position: absolute; margin: 0px; top: 300px; left: 860px; width: 100px; height: 100px;"><h1>Y</h1></div><div id="13" class="heading-center" style="position: absolute; margin: 0px; top: 460px; left: 860px; width: 100px; height: 100px;"><h1>Z</h1></div><div id="14" class="dial" style="position: absolute; margin: 0px; top: 280px; left: 980px; width: 140px; height: 140px;" feedid="403367" max="4" scale="" units_dropdown="__other" units="G" decimals="4" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-14" width="140" height="140"></canvas><div id="can-14-tooltip-1"></div><div id="can-14-tooltip-2"></div></div><div id="15" class="dial" style="position: absolute; margin: 0px; top: 440px; left: 980px; width: 140px; height: 140px;" feedid="403368" max="4" scale="" units_dropdown="__other" units="G" decimals="4" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-15" width="140" height="140"></canvas><div id="can-15-tooltip-1"></div><div id="can-15-tooltip-2"></div></div><div id="17" class="graph" style="position: absolute; margin: 0px; top: 640px; left: 0px; width: 840px; height: 440px;" graphid="17160"><iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="/graph/embed&amp;graphid=17160" style="width: 840px; height: 440px;"></iframe></div><div id="20" class="heading-center" style="position: absolute; margin: 0px; top: 720px; left: 870px; width: 100px; height: 90px;"><h1>Roll</h1></div><div id="21" class="dial" style="position: absolute; margin: 0px; top: 870px; left: 990px; width: 150px; height: 150px;" feedid="403371" max="180" scale="" units_dropdown="__other" units="°" decimals="-1" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-21" width="150" height="150"></canvas><div id="can-21-tooltip-1"></div><div id="can-21-tooltip-2"></div></div><div id="22" class="dial" style="position: absolute; margin: 0px; top: 690px; left: 990px; width: 150px; height: 160px;" feedid="403370" max="180" scale="" units_dropdown="__other" units="°" decimals="-1" offset="" type="6" graduations="1" unitend="0" displayminmax="0" minvaluefeed="403366" maxvaluefeed="403366" timeout="" errormessagedisplayed=""><canvas id="can-22" width="150" height="160"></canvas><div id="can-22-tooltip-1"></div><div id="can-22-tooltip-2"></div></div><div id="23" class="heading-center" style="position: absolute; margin: 0px; top: 900px; left: 870px; width: 100px; height: 60px;"><h1>Pitch</h1></div>
18 changes: 10 additions & 8 deletions emonCMS_MovingAverage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# importing the requests library
import requests
'''Script used to test the upload of the data on emocms, a web service for storing and visualizing data in a dashboard'''

# importing the requests library
import requests
import random
import time
import pandas as pd
Expand All @@ -20,7 +22,7 @@
data = pd.read_csv("crash.csv")
for index, row in data.iterrows():
# Set initial data at first iteration
if((index < 100)):
if((index < 100)):
x.append(row['x'])
y.append(row['y'])
z.append(row['z'])
Expand All @@ -40,10 +42,10 @@
pitch.pop()
pitch.append(row['pitch'])

# api-endpoint
# api-endpoint
if(index % 100 == 0):
timestamp = time.time() -2.5

print(timestamp)

normA = np.asarray(norm)
Expand All @@ -66,8 +68,8 @@

URL = "https://emoncms.org/input/post?node=2&time=" + str(timestamp) +"&csv=" + "x:"+str(xV) + "," + "y:"+str(yV) + "," + "z:"+str(zV) + "," + "norm:"+str(normV) + "," + "roll:"+str(rollV) + "," + "pitch:"+str(pitchV) + "," + "event:"+str(event) + "&apikey=" + apikey

# sending get request and saving the response as response object
r = requests.get(url = URL)
# sending get request and saving the response as response object
r = requests.get(url = URL)

# Wait and update index
time.sleep(timeInterval)
time.sleep(timeInterval)
Loading

0 comments on commit 89f0c2b

Please sign in to comment.