From 652221b10999170b5ebbc7268810d0da1d721f45 Mon Sep 17 00:00:00 2001
From: Samo Penic <samo.penic@gmail.com>
Date: Wed, 18 May 2022 11:12:52 +0000
Subject: [PATCH] Miha's accelerometer

---
 firmware/accelerometer_nrf24_basestation_fw/accelerometer_nrf24_basestation_fw.ino |   73 ++++++++++++
 firmware/debug_receiver/receiver/receiver.ino                                      |   51 +++++++
 firmware/acellerometer_nrf24_sensor_node_fw/acellerometer_nrf24_sensor_node_fw.ino |  107 +++++++++++++++++
 firmware/accelerometer_nrf24/accelerometer_nrf24.ino                               |   74 ++++++++++++
 4 files changed, 301 insertions(+), 4 deletions(-)

diff --git a/firmware/accelerometer_nrf24/accelerometer_nrf24.ino b/firmware/accelerometer_nrf24/accelerometer_nrf24.ino
new file mode 100644
index 0000000..fce3224
--- /dev/null
+++ b/firmware/accelerometer_nrf24/accelerometer_nrf24.ino
@@ -0,0 +1,74 @@
+/* There was a help from these two pages:
+https://forum.arduino.cc/t/connect-nrf24l01-to-esp32/678710/4
+https://electropeak.com/learn/wireless-communication-w-arduino-and-nrf24l01/
+*/
+
+#include <RF24_config.h>
+#include <nRF24L01.h>
+#include <RF24.h>
+#include <printf.h>
+
+#include<WiFi.h>
+
+#define IN_TRIGGER 17
+#define OUT_TRIGGER 16
+
+
+//RF24 radio(2, 4); // CE, CSN
+RF24 radio(22, 21); // CE, CSN
+
+const byte address[6] = {'R','E','C','V', '1'};
+char dataToSend[4] = "TRG";
+
+void IRAM_ATTR isr() {
+//    detachInterrupt(digitalPinToInterrupt(IN_TRIGGER));
+    digitalWrite(OUT_TRIGGER, LOW);
+    long time=micros();
+    while(micros()-time<4000);
+    digitalWrite(OUT_TRIGGER,HIGH);
+//        radio.write( &dataToSend, sizeof(dataToSend) );
+
+//    attachInterrupt(digitalPinToInterrupt(IN_TRIGGER), isr, RISING);
+  
+}
+
+
+
+void setup() {
+  // put your setup code here, to run once:
+  
+  WiFi.mode(WIFI_OFF);
+  btStop();
+
+
+  radio.begin();
+  radio.openWritingPipe(address);
+  radio.setPALevel(RF24_PA_MAX);
+  radio.setDataRate( RF24_1MBPS );
+  radio.setRetries(1,0); // delay, count
+  //pinMode(IN_TRIGGER, INPUT_PULLUP);
+  pinMode(OUT_TRIGGER, OUTPUT);
+  digitalWrite(OUT_TRIGGER,HIGH);
+  attachInterrupt(digitalPinToInterrupt(IN_TRIGGER), isr, FALLING);
+  Serial.begin(115200);
+
+}
+
+void loop() {
+  // put your main code here, to run repeatedly:
+//Serial.print(micros());
+//Serial.println(": ---> TRG");
+//unsigned long start_timer = micros();
+
+radio.write( &dataToSend, 4 );
+long time=micros();
+    while(micros()-time<5000);
+//Serial.print(micros());
+//Serial.println(": TRG--->");
+/*    
+unsigned long end_timer = micros();
+Serial.print(F(" Time to transmit: "));
+      Serial.print(end_timer - start_timer);                 // print the timer result
+      Serial.println(F(" us"));
+*/
+}
diff --git a/firmware/accelerometer_nrf24_basestation_fw/accelerometer_nrf24_basestation_fw.ino b/firmware/accelerometer_nrf24_basestation_fw/accelerometer_nrf24_basestation_fw.ino
new file mode 100644
index 0000000..a18578b
--- /dev/null
+++ b/firmware/accelerometer_nrf24_basestation_fw/accelerometer_nrf24_basestation_fw.ino
@@ -0,0 +1,73 @@
+#include <RF24_config.h>
+#include <nRF24L01.h>
+#include <RF24.h>
+#include <printf.h>
+
+RF24 radio(22, 21); // CE, CSN
+//RF24 radio(15,4); // CE, CSN
+
+const byte address[6] = {'L','O','G','G', 'R'};
+
+char val[4];
+
+struct PayloadStruct
+{
+  uint8_t nodeID;
+  uint16_t payloadID;
+  float ax;
+  float ay;
+  float az;
+  float wx;
+  float wy;
+  float wz;
+};
+
+PayloadStruct payload;
+void setup() {
+  // put your setup code here, to run once:
+
+  Serial.begin(115200);
+  radio.begin();
+  radio.openReadingPipe(0, address);
+  radio.setPALevel(RF24_PA_MIN);
+  radio.setDataRate( RF24_1MBPS );
+  radio.startListening();
+
+}
+
+void loop() {
+  // put your main code here, to run repeatedly:
+
+  delay(5);
+  //radio.startListening();
+  if ( radio.available()) {
+    while (radio.available()) {
+      Serial.print(millis());
+      radio.read(&payload, sizeof(payload));
+/*      Serial.print("Received payload ID = ");
+      Serial.print((int)payload.payloadID);
+      Serial.print(" ax=");
+      Serial.println(payload.ax);
+  */
+    
+    Serial.print(",");
+    Serial.print(payload.nodeID);
+    Serial.print(",");
+    Serial.print(payload.payloadID);
+    Serial.print(",");
+    Serial.print(payload.ax);
+    Serial.print(",");
+    Serial.print(payload.ay);
+    Serial.print(",");
+    Serial.print(payload.az);
+    Serial.print(",");
+    Serial.print(payload.wx);
+    Serial.print(",");
+    Serial.print(payload.wy);
+    Serial.print(",");
+    Serial.println(payload.wz);
+              
+    }
+  }
+
+}
diff --git a/firmware/acellerometer_nrf24_sensor_node_fw/acellerometer_nrf24_sensor_node_fw.ino b/firmware/acellerometer_nrf24_sensor_node_fw/acellerometer_nrf24_sensor_node_fw.ino
new file mode 100644
index 0000000..81cd737
--- /dev/null
+++ b/firmware/acellerometer_nrf24_sensor_node_fw/acellerometer_nrf24_sensor_node_fw.ino
@@ -0,0 +1,107 @@
+#include <RF24_config.h>
+#include <nRF24L01.h>
+#include <RF24.h>
+#include <printf.h>
+
+#include <Adafruit_MPU6050.h>
+#include <Adafruit_Sensor.h>
+#include <Wire.h>
+
+Adafruit_MPU6050 mpu;
+
+
+//RF24 radio(22, 21); // CE, CSN
+RF24 radio(15,4); // CE, CSN
+
+const byte RXaddress[6] = {'R','E','C','V', '1'};
+const byte TXaddress[6] = {'L','O','G','G', 'R'};
+
+char val[4];
+
+struct PayloadStruct
+{
+  uint8_t nodeID;
+  uint16_t payloadID;
+  float ax;
+  float ay;
+  float az;
+  float wx;
+  float wy;
+  float wz;
+};
+
+PayloadStruct payload;
+void setup() {
+  // put your setup code here, to run once:
+
+  Serial.begin(115200);
+  radio.begin();
+  radio.openReadingPipe(0, RXaddress);
+  radio.openWritingPipe(TXaddress);
+  radio.setPALevel(RF24_PA_MIN);
+  radio.setDataRate( RF24_1MBPS );
+ // radio.setPayloadSize(sizeof(payload));
+  radio.setRetries(1,0); // delay, count
+  radio.startListening();
+  payload.payloadID=0;
+  payload.nodeID=1;
+
+   // Try to initialize MPU accel!
+  if (!mpu.begin()) {
+    Serial.println("Failed to find MPU6050 chip");
+    while (1) {
+      delay(10);
+    }
+  }
+  Serial.println("MPU6050 Found!");
+
+  mpu.setAccelerometerRange(MPU6050_RANGE_4_G);
+  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
+  mpu.setFilterBandwidth(MPU6050_BAND_260_HZ);
+}
+
+void loop() {
+  // put your main code here, to run repeatedly:
+  sensors_event_t a, g, temp;
+  //delay(5);
+  mpu.getEvent(&a, &g, &temp);
+  //radio.startListening();
+  if ( radio.available()) {
+    while (radio.available()) {
+
+      radio.read(&val, sizeof(val));
+//      Serial.print("Received = ");
+//      Serial.println(val);
+      radio.stopListening(); // put radio in TX mode
+      
+//      unsigned long start_timer = micros();                    // start the timer
+
+      
+      payload.ax=a.acceleration.x;
+      payload.ay=a.acceleration.y;
+      payload.az=a.acceleration.z;
+      payload.wx=g.gyro.x;
+      payload.wy=g.gyro.y;
+      payload.wz=g.gyro.z;
+      bool report = radio.write(&payload, sizeof(payload));    // transmit & save the report
+//      unsigned long end_timer = micros();                      // end the timer
+ /*     if (report) {
+      // payload was delivered
+
+      Serial.print(F("Transmission of payloadID "));
+      Serial.print(payload.payloadID);                       // print payloadID
+      Serial.print(F(" as node "));
+      Serial.print(payload.nodeID);                          // print nodeID
+      Serial.print(F(" successful!"));
+      Serial.print(F(" Time to transmit: "));
+      Serial.print(end_timer - start_timer);                 // print the timer result
+      Serial.println(F(" us"));
+      } else {
+      Serial.println(F("Transmission failed or timed out")); // payload was not delivered
+      } */
+      payload.payloadID++;                                     // increment payload number
+      radio.startListening();
+    }
+  }
+
+}
diff --git a/firmware/debug_receiver/receiver/receiver.ino b/firmware/debug_receiver/receiver/receiver.ino
index 478a842..5466d12 100644
--- a/firmware/debug_receiver/receiver/receiver.ino
+++ b/firmware/debug_receiver/receiver/receiver.ino
@@ -3,18 +3,40 @@
 #include <RF24.h>
 #include <printf.h>
 
-RF24 radio(22, 21); // CE, CSN
-const byte address[6] = {'R','E','C','V', '1'};
+//RF24 radio(22, 21); // CE, CSN
+RF24 radio(15,4); // CE, CSN
+
+const byte RXaddress[6] = {'R','E','C','V', '1'};
+const byte TXaddress[6] = {'L','O','G','G', 'R'};
+
 char val[4];
 
+struct PayloadStruct
+{
+  uint8_t nodeID;
+  unsigned long payloadID;
+  float ax;
+  float ay;
+  float az;
+  float wx;
+  float wy;
+  float wz;
+};
+
+PayloadStruct payload;
 void setup() {
   // put your setup code here, to run once:
 
   Serial.begin(115200);
   radio.begin();
-  radio.openReadingPipe(0, address);
+  radio.openReadingPipe(0, RXaddress);
+  radio.openWritingPipe(TXaddress);
   radio.setPALevel(RF24_PA_MIN);
+  radio.setDataRate( RF24_250KBPS );
+  radio.setPayloadSize(sizeof(payload));
   radio.startListening();
+  payload.payloadID=0;
+  payload.nodeID=1;
 
 }
 
@@ -22,13 +44,34 @@
   // put your main code here, to run repeatedly:
 
   delay(5);
-  radio.startListening();
+  //radio.startListening();
   if ( radio.available()) {
     while (radio.available()) {
 
       radio.read(&val, sizeof(val));
       Serial.print("Received = ");
       Serial.println(val);
+      radio.stopListening(); // put radio in TX mode
+      
+      unsigned long start_timer = micros();                    // start the timer
+      bool report = radio.write(&payload, sizeof(payload));    // transmit & save the report
+      unsigned long end_timer = micros();                      // end the timer
+      if (report) {
+      // payload was delivered
+
+      Serial.print(F("Transmission of payloadID "));
+      Serial.print(payload.payloadID);                       // print payloadID
+      Serial.print(F(" as node "));
+      Serial.print(payload.nodeID);                          // print nodeID
+      Serial.print(F(" successful!"));
+      Serial.print(F(" Time to transmit: "));
+      Serial.print(end_timer - start_timer);                 // print the timer result
+      Serial.println(F(" us"));
+      } else {
+      Serial.println(F("Transmission failed or timed out")); // payload was not delivered
+      }
+      payload.payloadID++;                                     // increment payload number
+      radio.startListening();
     }
   }
 

--
Gitblit v1.9.3