|
|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
// code for Pro Micro or STM32 on the CubeSat Simulator STEM Payload board
|
|
|
|
|
// answers "OK" on the serial port Serial1 when queried by the Pi
|
|
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
|
#include <Adafruit_Sensor.h>
|
|
|
|
|
#include <Adafruit_BME280.h>
|
|
|
|
|
#include <MPU6050_tockn.h>
|
|
|
|
|
#include <EEPROM.h>
|
|
|
|
|
|
|
|
|
|
#define SEALEVELPRESSURE_HPA (1013.25)
|
|
|
|
|
|
|
|
|
|
Adafruit_BME280 bme;
|
|
|
|
|
@ -20,10 +22,20 @@ void eeprom_word_write(int addr, int val);
|
|
|
|
|
short eeprom_word_read(int addr);
|
|
|
|
|
int first_time = true;
|
|
|
|
|
int first_read = true;
|
|
|
|
|
|
|
|
|
|
#if defined __AVR_ATmega32U4__
|
|
|
|
|
float T2 = 26.3; // Temperature data point 1
|
|
|
|
|
float R2 = 167; // Reading data point 1
|
|
|
|
|
float T1 = 2; // Temperature data point 2
|
|
|
|
|
float R1 = 179; // Reading data point 2
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4)
|
|
|
|
|
float T2 = 25; // Temperature data point 1
|
|
|
|
|
float R2 = 671; // Reading data point 1
|
|
|
|
|
float T1 = 15.5; // Temperature data point 2
|
|
|
|
|
float R1 = 695; // Reading data point 2
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int sensorValue;
|
|
|
|
|
float Temp;
|
|
|
|
|
float rest;
|
|
|
|
|
@ -33,6 +45,7 @@ void setup() {
|
|
|
|
|
Serial.begin(9600); // Serial Monitor for testing
|
|
|
|
|
|
|
|
|
|
Serial1.begin(115200); // Pi UART faster speed
|
|
|
|
|
// Serial1.begin(9600); // Pi UART faster speed
|
|
|
|
|
|
|
|
|
|
Serial.println("Starting!");
|
|
|
|
|
|
|
|
|
|
@ -88,70 +101,57 @@ void setup() {
|
|
|
|
|
Serial.println(((float)eeprom_word_read(2)) / 100.0, DEC);
|
|
|
|
|
Serial.println(((float)eeprom_word_read(3)) / 100.0, DEC);
|
|
|
|
|
}
|
|
|
|
|
pinMode(greenLED, OUTPUT);
|
|
|
|
|
pinMode(blueLED, OUTPUT);
|
|
|
|
|
/**/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
|
|
|
|
if ((Serial.available() > 0)|| first_time == true) {
|
|
|
|
|
if (Serial1.available() > 0) {
|
|
|
|
|
blink(50);
|
|
|
|
|
char result = Serial.read();
|
|
|
|
|
// Serial.println(result);
|
|
|
|
|
char result = Serial1.read();
|
|
|
|
|
// Serial1.println(result);
|
|
|
|
|
// Serial1.println("OK");
|
|
|
|
|
|
|
|
|
|
if (result == 'R') {
|
|
|
|
|
Serial.println("OK");
|
|
|
|
|
delay(100);
|
|
|
|
|
first_time = true;
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
else if (result == 'C') {
|
|
|
|
|
Serial.println("Clearing stored gyro offsets in EEPROM\n");
|
|
|
|
|
eeprom_word_write(0, 0x00);
|
|
|
|
|
first_time = true;
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
if ((result == '?') || first_time == true)
|
|
|
|
|
// if (result == '?')
|
|
|
|
|
{
|
|
|
|
|
first_time = false;
|
|
|
|
|
if (bmePresent) {
|
|
|
|
|
Serial.print("OK BME280 ");
|
|
|
|
|
Serial.print(bme.readTemperature());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readPressure() / 100.0F);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readHumidity());
|
|
|
|
|
Serial1.print("OK BME280 ");
|
|
|
|
|
Serial1.print(bme.readTemperature());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readPressure() / 100.0F);
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readHumidity());
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
Serial.print("OK BME280 0.0 0.0 0.0 0.0");
|
|
|
|
|
Serial1.print("OK BME280 0.0 0.0 0.0 0.0");
|
|
|
|
|
}
|
|
|
|
|
mpu6050.update();
|
|
|
|
|
|
|
|
|
|
Serial.print(" MPU6050 ");
|
|
|
|
|
Serial.print(mpu6050.getGyroX());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getGyroY());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getGyroZ());
|
|
|
|
|
Serial1.print(" MPU6050 ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroX());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroY());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroZ());
|
|
|
|
|
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccX());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccY());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccZ());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccX());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccY());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccZ());
|
|
|
|
|
|
|
|
|
|
sensorValue = analogRead(A3);
|
|
|
|
|
//Serial.println(sensorValue);
|
|
|
|
|
Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1));
|
|
|
|
|
sensorValue = read_analog();
|
|
|
|
|
|
|
|
|
|
// Serial.println(sensorValue);
|
|
|
|
|
Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1));
|
|
|
|
|
|
|
|
|
|
Serial.print(" XS ");
|
|
|
|
|
Serial.print(Temp);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.println(Sensor1);
|
|
|
|
|
Serial1.print(" XS ");
|
|
|
|
|
Serial1.print(Temp);
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.println(Sensor2);
|
|
|
|
|
|
|
|
|
|
float rotation = sqrt(mpu6050.getGyroX()*mpu6050.getGyroX() + mpu6050.getGyroY()*mpu6050.getGyroY() + mpu6050.getGyroZ()*mpu6050.getGyroZ());
|
|
|
|
|
float acceleration = sqrt(mpu6050.getAccX()*mpu6050.getAccX() + mpu6050.getAccY()*mpu6050.getAccY() + mpu6050.getAccZ()*mpu6050.getAccZ());
|
|
|
|
|
@ -159,7 +159,12 @@ void loop() {
|
|
|
|
|
// Serial.print(" ");
|
|
|
|
|
// Serial.println(acceleration);
|
|
|
|
|
|
|
|
|
|
if (acceleration > 1.2)
|
|
|
|
|
if (first_read == true) {
|
|
|
|
|
first_read = false;
|
|
|
|
|
rest = acceleration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (acceleration > 1.2 * rest)
|
|
|
|
|
led_set(greenLED, HIGH);
|
|
|
|
|
else
|
|
|
|
|
led_set(greenLED, LOW);
|
|
|
|
|
@ -169,60 +174,72 @@ void loop() {
|
|
|
|
|
else
|
|
|
|
|
led_set(blueLED, LOW);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Serial1.available() > 0) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Serial.available() > 0) {
|
|
|
|
|
blink(50);
|
|
|
|
|
char result = Serial1.read();
|
|
|
|
|
// Serial1.println(result);
|
|
|
|
|
char result = Serial.read();
|
|
|
|
|
// Serial.println(result);
|
|
|
|
|
// Serial.println("OK");
|
|
|
|
|
// Serial.println(counter++);
|
|
|
|
|
|
|
|
|
|
if (result == 'R') {
|
|
|
|
|
if (result == 'R') {
|
|
|
|
|
Serial1.println("OK");
|
|
|
|
|
delay(100);
|
|
|
|
|
first_read = true;
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
else if (result == 'C') {
|
|
|
|
|
Serial.println("Clearing stored gyro offsets in EEPROM\n");
|
|
|
|
|
eeprom_word_write(0, 0x00);
|
|
|
|
|
first_time = true;
|
|
|
|
|
setup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result == '?')
|
|
|
|
|
if ((result == '?') || first_time == true)
|
|
|
|
|
{
|
|
|
|
|
first_time = false;
|
|
|
|
|
if (bmePresent) {
|
|
|
|
|
Serial1.print("OK BME280 ");
|
|
|
|
|
Serial1.print(bme.readTemperature());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readPressure() / 100.0F);
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(bme.readHumidity());
|
|
|
|
|
Serial.print("OK BME280 ");
|
|
|
|
|
Serial.print(bme.readTemperature());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readPressure() / 100.0F);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(bme.readHumidity());
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
Serial1.print("OK BME280 0.0 0.0 0.0 0.0");
|
|
|
|
|
Serial.print("OK BME280 0.0 0.0 0.0 0.0");
|
|
|
|
|
}
|
|
|
|
|
mpu6050.update();
|
|
|
|
|
|
|
|
|
|
Serial1.print(" MPU6050 ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroX());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroY());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getGyroZ());
|
|
|
|
|
Serial.print(" MPU6050 ");
|
|
|
|
|
Serial.print(mpu6050.getGyroX());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getGyroY());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getGyroZ());
|
|
|
|
|
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccX());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccY());
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.print(mpu6050.getAccZ());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccX());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccY());
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(mpu6050.getAccZ());
|
|
|
|
|
|
|
|
|
|
sensorValue = read_analog();
|
|
|
|
|
|
|
|
|
|
sensorValue = analogRead(A3);
|
|
|
|
|
//Serial.println(sensorValue);
|
|
|
|
|
Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1));
|
|
|
|
|
|
|
|
|
|
Serial1.print(" XS ");
|
|
|
|
|
Serial1.print(Temp);
|
|
|
|
|
Serial1.print(" ");
|
|
|
|
|
Serial1.println(Sensor2);
|
|
|
|
|
Serial.print(" XS ");
|
|
|
|
|
Serial.print(Temp);
|
|
|
|
|
Serial.print(" ");
|
|
|
|
|
Serial.print(Sensor2);
|
|
|
|
|
Serial.print(" (");
|
|
|
|
|
Serial.print(sensorValue);
|
|
|
|
|
Serial.println(")");
|
|
|
|
|
|
|
|
|
|
float rotation = sqrt(mpu6050.getGyroX()*mpu6050.getGyroX() + mpu6050.getGyroY()*mpu6050.getGyroY() + mpu6050.getGyroZ()*mpu6050.getGyroZ());
|
|
|
|
|
float acceleration = sqrt(mpu6050.getAccX()*mpu6050.getAccX() + mpu6050.getAccY()*mpu6050.getAccY() + mpu6050.getAccZ()*mpu6050.getAccZ());
|
|
|
|
|
@ -246,8 +263,7 @@ void loop() {
|
|
|
|
|
led_set(blueLED, LOW);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delay(100);
|
|
|
|
|
delay(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void eeprom_word_write(int addr, int val)
|
|
|
|
|
@ -314,3 +330,15 @@ void led_set(int ledPin, bool state)
|
|
|
|
|
digitalWrite(ledPin, state);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int read_analog()
|
|
|
|
|
{
|
|
|
|
|
int sensorValue;
|
|
|
|
|
#if defined __AVR_ATmega32U4__
|
|
|
|
|
sensorValue = analogRead(A3);
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4)
|
|
|
|
|
sensorValue = analogRead(PA7);
|
|
|
|
|
#endif
|
|
|
|
|
return(sensorValue);
|
|
|
|
|
}
|
|
|
|
|
|