From be3f52c1f1b5b73d19e7195705c3db7b26be3d1a Mon Sep 17 00:00:00 2001 From: mablr Date: Thu, 9 Jan 2020 16:56:02 +0100 Subject: [PATCH] initial code --- arduino/arrivee/arrivee.ino | 48 ++++++++++++++++++++ arduino/depart/depart.ino | 58 ++++++++++++++++++++++++ rpi/cds.py | 88 +++++++++++++++++++++++++++++++++++++ rpi/opcv2.py | 22 ++++++++++ rpi/ser.py | 10 +++++ 5 files changed, 226 insertions(+) create mode 100644 arduino/arrivee/arrivee.ino create mode 100644 arduino/depart/depart.ino create mode 100755 rpi/cds.py create mode 100755 rpi/opcv2.py create mode 100755 rpi/ser.py diff --git a/arduino/arrivee/arrivee.ino b/arduino/arrivee/arrivee.ino new file mode 100644 index 0000000..beeba7e --- /dev/null +++ b/arduino/arrivee/arrivee.ino @@ -0,0 +1,48 @@ +// Réalisé à l'aide des exemples fournis avec la bibliotheque NRFLite.h publiée sous licence MIT par Dave Parson +#include +#include + +const static uint8_t RADIO_ID = 1; +const static uint8_t PIN_RADIO_CE = 9; +const static uint8_t PIN_RADIO_CSN = 10; + +const static uint8_t PIN_RPI = 4; + +struct RadioPacket // structure pour les paquets +{ + uint8_t FromRadioId; + boolean portiqueOuvert; + uint32_t FailedTxCount; +}; + +NRFLite _radio; + +RadioPacket _radioData; + +void setup() +{ + Serial.begin(9600); + + // Initialisation de la radio + if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN)) + { + Serial.println("Cannot communicate with radio"); + while (1); + } + // On force l'initialisation à "portique fermé" + _radioData.portiqueOuvert = 0; + + // On initialise la pin de sortie pour le RPI + pinMode(PIN_RPI, OUTPUT); + digitalWrite(PIN_RPI, 0); + +} + +void loop() +{ + while (_radio.hasData()) + { + _radio.readData(&_radioData); + Serial.println(_radioData.portiqueOuvert); + } +} diff --git a/arduino/depart/depart.ino b/arduino/depart/depart.ino new file mode 100644 index 0000000..935b4df --- /dev/null +++ b/arduino/depart/depart.ino @@ -0,0 +1,58 @@ +// Réalisé à l'aide des exemples fournis avec la bibliotheque NRFLite.h publiée sous licence MIT par Dave Parson +#include +#include + +const static uint8_t RADIO_ID = 0; +const static uint8_t DESTINATION_RADIO_ID = 1; +const static uint8_t PIN_RADIO_CE = 9; +const static uint8_t PIN_RADIO_CSN = 10; + +const static uint8_t PIN_MICRO_RUPT = 8; + + +struct RadioPacket // structure pour les paquets +{ + uint8_t FromRadioId; + boolean portiqueOuvert; + uint32_t FailedTxCount; +}; + +// var radio +NRFLite _radio(Serial); + +// var pour stocker le contenus des messages radio +RadioPacket _radioData; + + + +void setup() +{ + Serial.begin(9600); + + /* + * _radio.printChannels(); + * Permet de scanner les canaux libres + * Par défaut on utilise le 100 + */ + + // Initialisation de la radio + if (!_radio.init(RADIO_ID, PIN_RADIO_CE, PIN_RADIO_CSN, NRFLite::BITRATE2MBPS, 100)) + { + Serial.println("Cannot communicate with radio"); + // Si l'initialisation échoue on ne va pas plus loin + while (1); + } + // Initialisation de l'id de la radio + _radioData.FromRadioId = RADIO_ID; + + // On init le pin sur lequel le microrupteur est branché en entrée + pinMode(PIN_MICRO_RUPT,INPUT); + +} + +void loop() { + // put your main code here, to run repeatedly: + Serial.println(digitalRead(PIN_MICRO_RUPT)); + _radioData.portiqueOuvert = digitalRead(PIN_MICRO_RUPT); + _radio.send(DESTINATION_RADIO_ID, &_radioData, sizeof(_radioData), NRFLite::NO_ACK); +} diff --git a/rpi/cds.py b/rpi/cds.py new file mode 100755 index 0000000..0132cbf --- /dev/null +++ b/rpi/cds.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +# (c) Mablr +# GNU GPL v3 + + +import time +import cv2 +import serial + +avertPortique = False +portiqueOuvert = True +seqInit = True +courseEnCours = False + +cap = cv2.VideoCapture(0) +fgbg = cv2.createBackgroundSubtractorMOG2() +detectShadows = True + +fermeturePropre = 0 +ouverturePropre = 0 + +# Seuil de détection +seuilDetec = 5000 + +# Peut etre a enlever +continuer = True + +# Initialisation de la connexion série +ser = serial.Serial('/dev/ttyACM0',9600) + +while continuer: + # Récupération de l'état du portique + serialDATA = str(ser.readline())[2:3] + + if serialDATA == "0": + portiqueOuvert = False + else: + portiqueOuvert = True + + ## Séquence d'initialisation + if portiqueOuvert and seqInit and not courseEnCours: + fermeturePropre = 0# remise à zéro du compteur fermeture stable + if not avertPortique: + print("Veuillez fermer le portique.") + avertPortique = True + + # Si le portique est fermé + if not portiqueOuvert and seqInit and not courseEnCours: + time.sleep(0.01) + fermeturePropre += 1 # incrément fermeture stable + + if not portiqueOuvert and fermeturePropre > 20 and not courseEnCours and seqInit: + # On peut résonablement penser que le portique de départ est bien fermé + input("Taper sur ENTRER pour autoriser le départ, ou Ctrl+C pour quitter le programme.\n") + seqInit = False + avertPortique = False + ouverturePropre = 0 + + # Si le portique ouvre (départ du skieur) + if portiqueOuvert and not seqInit and not courseEnCours: + ouverturePropre += 1 # incrément ouverture stable + + ## Début de la course + if portiqueOuvert and ouverturePropre > 100 and not seqInit and not courseEnCours: + tsDepart = time.time() + courseEnCours = True + print("Timestamp de départ :", tsDepart) + + # Stabilisation de l'image + for i in range(100): + ret, frame = cap.read() + fgmask = fgbg.apply(frame) + + + if courseEnCours and not seqInit: + ret, frame = cap.read() + fgmask = fgbg.apply(frame) + countWhite = cv2.countNonZero(fgmask) + # Si on détecte un skieur + if countWhite >= seuilDetec: + # Traitement du temps du skieur + tsArrivee = time.time() + temps = tsArrivee - tsDepart + print("\n#################################\n# Temps de course:", str(temps)[:7],"sec. #\n#################################\n") + # La course est finie + courseEnCours = False + seqInit = True + diff --git a/rpi/opcv2.py b/rpi/opcv2.py new file mode 100755 index 0000000..83783ba --- /dev/null +++ b/rpi/opcv2.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +# Code inspiré par la doc d'OpenCV https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html#backgroundsubtractormog2 + +import numpy as np +import cv2 + +cap = cv2.VideoCapture(0) +fgbg = cv2.createBackgroundSubtractorMOG2() +detectShadows = False +while(1): + ret, frame = cap.read() + + fgmask = fgbg.apply(frame) + count_white = cv2.countNonZero(fgmask) + print(count_white) + cv2.imshow('frame',fgmask) + k = cv2.waitKey(30) & 0xff + if k == 27: + break + +cap.release() +cv2.destroyAllWindows() diff --git a/rpi/ser.py b/rpi/ser.py new file mode 100755 index 0000000..c961c25 --- /dev/null +++ b/rpi/ser.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 + +import serial +import time + +ser = serial.Serial('/dev/ttyACM0',9600) +while True: + value = int(str(ser.readline())[5:6]) + print(value) +