Skip to content
Snippets Groups Projects

Included barometer code to esp_slave and esp_master

Closed Aaron John Sabu requested to merge aaron-baro2main_py into main
Viewing commit a42c2575
Prev
Show latest version
2 files
+ 95
47
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -2,13 +2,20 @@
@@ -2,13 +2,20 @@
#include <Wire.h>
#include <Wire.h>
#include <Arduino.h>
#include <Arduino.h>
#include <esp_now.h>
#include <esp_now.h>
#include <SparkFun_VL53L1X.h>
#include <VL53L1X.h>
#include <Adafruit_MPL3115A2.h>
#include <Adafruit_MPL3115A2.h>
#include "Camera.h"
#include "Camera.h"
#include "utilities.h"
#include "utilities.h"
#include <Adafruit_MotorShield.h>
#include <Adafruit_MotorShield.h>
 
// For LIDAR 1 and 2
 
#define XSHUT_pin1 A7
 
#define XSHUT_pin2 A6
 
 
#define Sensor1_newAddress 43
 
#define Sensor2_newAddress 45
 
// REPLACE WITH THE MAC Address of your receiver (MASTER)
// REPLACE WITH THE MAC Address of your receiver (MASTER)
// Slave: 40:F5:20:44:B6:4C
// Slave: 40:F5:20:44:B6:4C
// MAC Address of the receiver (MASTER)
// MAC Address of the receiver (MASTER)
@@ -36,8 +43,9 @@ String Rec_dir4;
@@ -36,8 +43,9 @@ String Rec_dir4;
int count = 0;
int count = 0;
int count_var = 0;
int count_var = 0;
int print_count = 0;
int print_count = 0;
int change_count =0;
int change_count = 0;
int Lidar_flag = 0;
int Lidar1_flag = 0;
 
int Lidar2_flag = 0;
int Baro_flag = 0;
int Baro_flag = 0;
// Define variables to be sent;
// Define variables to be sent;
@@ -47,16 +55,16 @@ int Sent_tz = 0;
@@ -47,16 +55,16 @@ int Sent_tz = 0;
int Sent_rx = 0;
int Sent_rx = 0;
int Sent_ry = 0;
int Sent_ry = 0;
int Sent_rz = 0;
int Sent_rz = 0;
int Sent_dist = 0;
int Sent_dist1 = 0;
 
int Sent_dist2 = 0;
float Sent_baro = 0;
float Sent_baro = 0;
int8_t g1 = 0,g2=1,g3=2;
int8_t g1 = 0,g2 = 1,g3 = 2;
int8_t goal_id[3] = {g1, g2, g3};
int8_t goal_id[3] = {g1, g2, g3};
// Define Lidar variables
// Define Lidar variables
SFEVL53L1X distanceSensor;
VL53L1X Sensor1;
int budgetIndex = 4;
VL53L1X Sensor2;
int budgetValue[7] = {15, 20, 33, 50, 100, 200, 500};
// Define Barometer variables
// Define Barometer variables
Adafruit_MPL3115A2 baro;
Adafruit_MPL3115A2 baro;
@@ -68,25 +76,26 @@ int LED = LED_BUILTIN;
@@ -68,25 +76,26 @@ int LED = LED_BUILTIN;
// ==================================== data structure =================================================
// ==================================== data structure =================================================
//Structure the sending data
//Structure the sending data
//Must match the receiver structure
//Must match the receiver structure
typedef struct struct_message {
typedef struct struct_message {
int Rtx;
int Rtx;
int Rty;
int Rty;
int Rtz;
int Rtz;
int Rrx;
int Rrx;
int Rry;
int Rry;
int Rrz;
int Rrz;
int Rdist;
int Rdist1;
 
int Rdist2;
float Rbaro;
float Rbaro;
String DebugM;
String DebugM;
int Spwm1;
int Spwm1;
int Spwm2;
int Spwm2;
int Spwm3;
int Spwm3;
int Spwm4;
int Spwm4;
String Sdir1;
String Sdir1;
String Sdir2;
String Sdir2;
String Sdir3;
String Sdir3;
String Sdir4;
String Sdir4;
} struct_message;
} struct_message;
// Create a struct_message to hold incoming sensor readings
// Create a struct_message to hold incoming sensor readings
// struct_message incomingReadings;
// struct_message incomingReadings;
struct_message receivedData;
struct_message receivedData;
@@ -133,7 +142,7 @@ Camera cam(&interface);
@@ -133,7 +142,7 @@ Camera cam(&interface);
// ========================== Motor part ====================================
// ========================== Motor part ====================================
// Create the motor shield object with the default I2C address
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// changed
Adafruit_DCMotor *motorVertical_L = AFMS.getMotor(1); // pwm1
Adafruit_DCMotor *motorVertical_L = AFMS.getMotor(1); // pwm1
Adafruit_DCMotor *motorVertical_R = AFMS.getMotor(2); // pwm2
Adafruit_DCMotor *motorVertical_R = AFMS.getMotor(2); // pwm2
Adafruit_DCMotor *motorLeft = AFMS.getMotor(3); // pwm3
Adafruit_DCMotor *motorLeft = AFMS.getMotor(3); // pwm3
@@ -152,16 +161,40 @@ void setup() {
@@ -152,16 +161,40 @@ void setup() {
digitalWrite(LED, LOW);
digitalWrite(LED, LOW);
// -------------- lidar part --------------------
// -------------- lidar part --------------------
if (distanceSensor.begin() == 0){
pinMode(XSHUT_pin1, OUTPUT);
Serial.println("Lidar online!");
pinMode(XSHUT_pin2, OUTPUT);
Lidar_flag = 1;
} else {
Lidar_flag = 0;
}
distanceSensor.startRanging();
Wire.setClock(100000);
distanceSensor.setIntermeasurementPeriod(budgetValue[budgetIndex]);
 
pinMode(XSHUT_pin1, INPUT);
 
if (!Sensor1.init()){
 
Serial.println("Failed to detect and initialize sensor1!");
 
Lidar1_flag = 0;
 
} else {
 
Lidar1_flag = 1;
 
}
 
delay(10);
 
Sensor1.setAddress(Sensor1_newAddress);
 
pinMode(XSHUT_pin2, INPUT);
 
if (!Sensor2.init())
 
{
 
Serial.println("Failed to detect and initialize sensor2!");
 
Lidar2_flag = 0;
 
} else {
 
Lidar2_flag = 1;
 
}
 
delay(10);
 
Sensor2.setAddress(Sensor2_newAddress);
 
 
Sensor1.setTimeout(500);
 
Sensor2.setTimeout(500);
 
 
Sensor1.setDistanceMode(VL53L1X::Medium);
 
Sensor1.setMeasurementTimingBudget(33000);
 
Sensor2.setDistanceMode(VL53L1X::Medium);
 
Sensor2.setMeasurementTimingBudget(33000);
 
// ----------------- barometer -----------------
// ----------------- barometer -----------------
if (baro.begin()) {
if (baro.begin()) {
Serial.println("Barometer online!");
Serial.println("Barometer online!");
@@ -199,12 +232,11 @@ void setup() {
@@ -199,12 +232,11 @@ void setup() {
}
}
// Register for a callback function that will be called when data is received
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
esp_now_register_recv_cb(OnDataRecv);
}
}
// ================================== Main loop ===================================================
// ================================== Main loop ===================================================
void loop()
void loop()
{
{
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// ========== goal finder =========
// ========== goal finder =========
int id = -1;
int id = -1;
@@ -230,18 +262,31 @@ void loop()
@@ -230,18 +262,31 @@ void loop()
Sent_ry = 0;
Sent_ry = 0;
Sent_rz = 0;
Sent_rz = 0;
}
}
if (Lidar_flag == 1){
if (Lidar1_flag == 1){
Sent_dist = distanceSensor.getDistance(); // Lidar distance (use the real Lidar data)
Sensor1.startContinuous(50);
} else {
Sensor1.read();
Sent_dist = 0;
Sensor1.stopContinuous();
 
Sent_dist1 = Sensor1.ranging_data.range_mm; // Lidar distance (use the real Lidar data)
 
}else {
 
Sent_dist1 = 0;
}
}
 
if (Lidar2_flag == 1){
 
Sensor2.startContinuous(50);
 
Sensor2.read();
 
Sensor2.stopContinuous();
 
Sent_dist2 = Sensor2.ranging_data.range_mm; // Lidar distance (use the real Lidar data)
 
}else {
 
Sent_dist2 = 0;
 
}
 
if (Baro_flag == 1){
if (Baro_flag == 1){
Sent_baro = baro.getAltitude() - groundAlt; // Barometer altitude (use the real Barometer data)
Sent_baro = baro.getAltitude() - groundAlt; // Barometer altitude (use the real Barometer data)
} else {
} else {
Sent_baro = 0.0;
Sent_baro = 0.0;
}
}
 
// ========== lidar state info =========
// ========== lidar state info =========
if (Sent_dist < 300 && Sent_dist > 0){
if (Sent_dist1 < 300 && Sent_dist1 > 0){
receivedData.DebugM = "found b";
receivedData.DebugM = "found b";
}else{
}else{
receivedData.DebugM = "no b";
receivedData.DebugM = "no b";
@@ -253,7 +298,8 @@ void loop()
@@ -253,7 +298,8 @@ void loop()
receivedData.Rrx = Sent_rx;
receivedData.Rrx = Sent_rx;
receivedData.Rry = Sent_ry;
receivedData.Rry = Sent_ry;
receivedData.Rrz = Sent_rz;
receivedData.Rrz = Sent_rz;
receivedData.Rdist = Sent_dist;
receivedData.Rdist1 = Sent_dist1;
 
receivedData.Rdist2 = Sent_dist2;
receivedData.Rbaro = Sent_baro;
receivedData.Rbaro = Sent_baro;
send_var_once();
send_var_once();
print_received_Data();
print_received_Data();