diff --git a/config b/config index 1973426a..9b3a372d 100755 --- a/config +++ b/config @@ -180,6 +180,8 @@ elif [ "$1" = "-h" ]; then echo " -q Change the Squelch setting for command receiver" echo " -F Change the rx and tx frequency" echo " -H Chnage the Balloon mode" + echo " -p Display payload sensor data" + echo " -v Display voltage and current data" echo exit @@ -665,7 +667,22 @@ elif [ "$1" = "-H" ]; then echo sudo systemctl restart cubesatsim - + +elif [ "$1" = "-p" ]; then + + echo "Real-time output from the serial port from the Pico:" + echo +# sleep 2 + timeout 2 cat /dev/serial0 > /dev/null + + timeout 3 cat /dev/serial0 + +elif [ "$1" = "-v" ]; then + + echo "Real-time output from the INA219 voltage and current sensors:" + echo + /home/pi/CubeSatSim/telem + fi # sudo systemctl restart cubesatsim diff --git a/main.c b/main.c index 4872165d..dba9612c 100644 --- a/main.c +++ b/main.c @@ -611,6 +611,8 @@ int main(int argc, char * argv[]) { fgets(cmdbuffer, 1000, file1); fprintf(stderr, "Python read Result: %s\n", cmdbuffer); +// serialPuts(uart_fd, cmdbuffer); // write INA data to Pico over serial + const char space[2] = " "; token = strtok(cmdbuffer, space); @@ -2140,13 +2142,15 @@ void loop() { */ int get_payload_serial(int debug_camera) { - + index1 = 0; flag_count = 0; start_flag_detected = FALSE; start_flag_complete = FALSE; end_flag_detected = FALSE; jpeg_start = 0; + + serialFlush (uart_fd); // flush serial buffer so latest payload is read // #ifdef GET_IMAGE_DEBUG if (debug_camera) diff --git a/rpitx.py b/rpitx.py index fd6d9795..6246033f 100644 --- a/rpitx.py +++ b/rpitx.py @@ -341,15 +341,16 @@ if __name__ == "__main__": if (command_tx == True): # battery_saver_mode output(txLed, txLedOn) - if (txc): - output(pd, 1) - output (ptt, 0) - sleep(0.1) - system("aplay -D hw:CARD=Headphones,DEV=0 /home/pi/CubeSatSim/morse.wav") - sleep(0.1) - output (ptt, 1) - output(pd, 0) - else: +# if (txc): +# output(pd, 1) +# output (ptt, 0) +# sleep(0.1) +# system("aplay -D hw:CARD=Headphones,DEV=0 /home/pi/CubeSatSim/morse.wav") +# sleep(0.1) +# output (ptt, 1) +# output(pd, 0) +# else: + if (True): if (debug_mode == 1): # system("echo 'hi hi de " + callsign + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f 434.9e3") system("echo 'hi hi de " + callsign + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3") diff --git a/stempayload/Payload_BME280_MPU6050_XS/Payload_BME280_MPU6050_XS.ino b/stempayload/Payload_BME280_MPU6050_XS/Payload_BME280_MPU6050_XS.ino index f0111f9e..7b5ed4ae 100644 --- a/stempayload/Payload_BME280_MPU6050_XS/Payload_BME280_MPU6050_XS.ino +++ b/stempayload/Payload_BME280_MPU6050_XS/Payload_BME280_MPU6050_XS.ino @@ -1,16 +1,20 @@ -// 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 +// code for Pico or Pro Micro or STM32 on the CubeSat Simulator STEM Payload board +// works wih CubeSatSim software v1.3.2 or later +// extra sensors can be added in payload_extension.cpp #include #include #include #include #include +#include + #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; MPU6050 mpu6050(Wire); - +TinyGPSPlus gps; + long timer = 0; int bmePresent; int RXLED = 17; // The RX LED has a defined Arduino pin @@ -18,11 +22,21 @@ int greenLED = 9; int blueLED = 8; int Sensor1 = 0; float Sensor2 = 0; -void eeprom_word_write(int addr, int val); -short eeprom_word_read(int addr); +void ee_prom_word_write(int addr, int val); +short ee_prom_word_read(int addr); int first_time = true; int first_read = true; +bool check_for_wifi(); +bool wifi = false; +int led_builtin_pin; +#define PICO_W // define if Pico W board. Otherwise, compilation fail for Pico or runtime fail if compile as Pico W +#if defined ARDUINO_ARCH_RP2040 +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 __AVR_ATmega32U4__ float T2 = 26.3; // Temperature data point 1 float R2 = 167; // Reading data point 1 @@ -39,15 +53,53 @@ float R1 = 695; // Reading data point 2 int sensorValue; float Temp; float rest; - + +char sensor_end_flag[] = "_END_FLAG_"; +char sensor_start_flag[] = "_START_FLAG_"; +bool show_gps = true; // set to false to not see all messages +float flon = 0.0, flat = 0.0, flalt = 0.0; +void get_gps(); + +extern void payload_setup(); // sensor extension setup function defined in payload_extension.cpp +extern void payload_loop(); // sensor extension read function defined in payload_extension.cpp + void setup() { - Serial.begin(9600); // Serial Monitor for testing +#ifdef ARDUINO_ARCH_RP2040 + Serial1.setRX(1); + delay(100); + Serial1.setTX(0); + delay(100); +#endif + + Serial.begin(115200); // Serial Monitor for testing - Serial1.begin(115200); // Pi UART faster speed -// Serial1.begin(9600); // Pi UART faster speed + Serial1.begin(115200); // Pi UART faster spd +// Serial1.begin(9600); // Pi UART faster spd + + delay(10000); Serial.println("Starting!"); + +#ifdef ARDUINO_ARCH_RP2040 + Serial.println("This code is for the Raspberry Pi Pico hardware."); + + Serial.println("Starting Serial2 for optional GPS on JP12"); +// Serial2.begin(9600); // serial from - some modules need 115200 + + // pinMode(0, INPUT); + // pinMode(1, INPUT); + + // set all Pico GPIO connected pins to input + for (int i = 6; i < 22; i++) { + pinMode(i, INPUT); + } + pinMode(26, INPUT); + pinMode(27, INPUT); + pinMode(28, INPUT); + pinMode(15, INPUT_PULLUP); // squelch + +#endif blink_setup(); @@ -70,7 +122,7 @@ void setup() { } mpu6050.begin(); - + if (eeprom_word_read(0) == 0xA07) { Serial.println("Reading gyro offsets from EEPROM\n"); @@ -87,8 +139,12 @@ void setup() { } else { +#ifdef ARDUINO_ARCH_RP2040 + Serial.println("Calculating gyro offsets\n"); + mpu6050.calcGyroOffsets(true); +#endif +#ifndef ARDUINO_ARCH_RP2040 Serial.println("Calculating gyro offsets and storing in EEPROM\n"); - mpu6050.calcGyroOffsets(true); eeprom_word_write(0, 0xA07); @@ -100,21 +156,29 @@ void setup() { Serial.println(((float)eeprom_word_read(1)) / 100.0, DEC); Serial.println(((float)eeprom_word_read(2)) / 100.0, DEC); Serial.println(((float)eeprom_word_read(3)) / 100.0, DEC); +#endif } -/**/ + payload_setup(); // sensor extension setup function defined in payload_extension.cpp } void loop() { - + + blink(50); + if (Serial1.available() > 0) { - blink(50); - char result = Serial1.read(); -// Serial1.println(result); -// Serial1.println("OK"); - + Serial.print("Received serial data!!!\n"); + delay(10); + while (Serial1.available() > 0) { + char result = Serial1.read(); + Serial.print(result); + } + Serial.println(" "); + } + { // if (result == '?') { if (bmePresent) { + Serial1.print(sensor_start_flag); Serial1.print("OK BME280 "); Serial1.print(bme.readTemperature()); Serial1.print(" "); @@ -123,9 +187,21 @@ void loop() { 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(sensor_start_flag); 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(); @@ -136,22 +212,59 @@ void loop() { Serial1.print(" "); Serial1.print(mpu6050.getGyroZ()); - Serial1.print(" "); - Serial1.print(mpu6050.getAccX()); - Serial1.print(" "); - Serial1.print(mpu6050.getAccY()); - Serial1.print(" "); - Serial1.print(mpu6050.getAccZ()); + Serial1.print(" "); + Serial1.print(mpu6050.getAccX()); + Serial1.print(" "); + Serial1.print(mpu6050.getAccY()); + Serial1.print(" "); + Serial1.print(mpu6050.getAccZ()); + + Serial.print(" MPU6050 "); + Serial.print(mpu6050.getGyroX()); + Serial.print(" "); + Serial.print(mpu6050.getGyroY()); + Serial.print(" "); + Serial.print(mpu6050.getGyroZ()); - sensorValue = read_analog(); + Serial.print(" "); + Serial.print(mpu6050.getAccX()); + Serial.print(" "); + Serial.print(mpu6050.getAccY()); + Serial.print(" "); + Serial.print(mpu6050.getAccZ()); + + sensorValue = read_analog(); // Serial.println(sensorValue); Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1)); - Serial1.print(" XS "); - Serial1.print(Temp); +// Serial1.print(" GPS 0 0 0 TMP "); + + Serial1.print(" GPS "); + Serial1.print(flat,4); Serial1.print(" "); - Serial1.println(Sensor2); + Serial1.print(flon,4); + Serial1.print(" "); + Serial1.print(flalt,2); + + Serial1.print(" TMP "); + Serial1.print(Temp); + +// Serial1.print(" "); +// Serial1.println(Sensor2); + + Serial.print(" GPS "); + Serial.print(flat,4); + Serial.print(" "); + Serial.print(flon,4); + Serial.print(" "); + Serial.print(flalt,2); + +// Serial.print(" GPS 0 0 0 TMP "); + Serial.print(" TMP "); + Serial.print(Temp); +// Serial.print(" "); +// Serial.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()); @@ -174,7 +287,13 @@ void loop() { else led_set(blueLED, LOW); } - + + payload_loop(); // sensor extension read function defined in payload_extension.cpp + +// Serial1.println(" "); + Serial1.println(sensor_end_flag); + Serial.println(" "); + } if (Serial.available() > 0) { @@ -183,8 +302,8 @@ void loop() { // Serial.println(result); // Serial.println("OK"); // Serial.println(counter++); - - if (result == 'R') { +#ifndef ARDUINO_ARCH_RP2040 + if (result == 'R') { Serial1.println("OK"); delay(100); first_read = true; @@ -196,74 +315,17 @@ void loop() { first_time = true; setup(); } - - if ((result == '?') || first_time == true) - { - 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()); - } else - { - Serial.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()); - - Serial.print(" "); - Serial.print(mpu6050.getAccX()); - Serial.print(" "); - Serial.print(mpu6050.getAccY()); - Serial.print(" "); - Serial.print(mpu6050.getAccZ()); - - sensorValue = read_analog(); - - Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1)); - - 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()); -// Serial.print(rotation); -// Serial.print(" "); -// Serial.println(acceleration); - - if (first_read == true) { - first_read = false; - rest = acceleration; - } - - if (acceleration > 1.2 * rest) - led_set(greenLED, HIGH); - else - led_set(greenLED, LOW); - - if (rotation > 5) - led_set(blueLED, HIGH); - else - led_set(blueLED, LOW); - } +#endif } - delay(100); + +#ifdef ARDUINO_ARCH_RP2040 + Serial.print("Squelch: "); + Serial.println(digitalRead(15)); +#endif + +// delay(1000); not needed due to gps 1 second polling delay + get_gps(); + } void eeprom_word_write(int addr, int val) @@ -292,6 +354,23 @@ void blink_setup() pinMode(greenLED, OUTPUT); pinMode(blueLED,OUTPUT); #endif + +#if defined ARDUINO_ARCH_RP2040 + if (check_for_wifi()) { + wifi = true; + led_builtin_pin = LED_BUILTIN; // use default GPIO for Pico W + pinMode(LED_BUILTIN, OUTPUT); +// configure_wifi(); + } else { + led_builtin_pin = 25; // manually set GPIO 25 for Pico board +// pinMode(25, OUTPUT); + pinMode(led_builtin_pin, OUTPUT); + + pinMode(18, OUTPUT); + pinMode(19, OUTPUT); + } +#endif + } void blink(int length) @@ -304,8 +383,13 @@ void blink(int length) digitalWrite(RXLED, LOW); // set the RX LED ON TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF #endif - - delay(length); // wait for a lenth of time + +#if defined ARDUINO_ARCH_RP2040 + if (wifi) + digitalWrite(LED_BUILTIN, HIGH); // set the built-in LED ON + else + digitalWrite(led_builtin_pin, HIGH); // set the built-in LED ON +#endif #if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) digitalWrite(PC13, HIGH); // turn the LED off by making the voltage LOW @@ -315,6 +399,16 @@ void blink(int length) digitalWrite(RXLED, HIGH); // set the RX LED OFF TXLED0; //TX LED macro to turn LED ON #endif + +#if defined ARDUINO_ARCH_RP2040 + if (wifi) + digitalWrite(LED_BUILTIN, LOW); // set the built-in LED OFF + else + digitalWrite(led_builtin_pin, LOW); // set the built-in LED OFF + +// delay(length); // wait for a lenth of time +#endif + } void led_set(int ledPin, bool state) @@ -329,6 +423,13 @@ void led_set(int ledPin, bool state) #if defined __AVR_ATmega32U4__ digitalWrite(ledPin, state); #endif + +#ifdef ARDUINO_ARCH_RP2040 + if (ledPin == greenLED) + digitalWrite(19, state); + else if (ledPin == blueLED) + digitalWrite(18, state); +#endif } int read_analog() @@ -339,6 +440,80 @@ int read_analog() #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 +#if defined ARDUINO_ARCH_RP2040 + sensorValue = analogRead(28); #endif return(sensorValue); } + +bool check_for_wifi() { + +#ifndef PICO_W + + Serial.println("WiFi disabled in software"); + return(false); // skip check if not Pico W board or compilation will fail + +#endif + + pinMode(29, INPUT); + const float conversion_factor = 3.3f / (1 << 12); + uint16_t result = analogRead(29); +// Serial.printf("ADC3 value: 0x%03x, voltage: %f V\n", result, result * conversion_factor); + + if (result < 0x10) { + Serial.println("\nPico W detected!\n"); + return(true); + } + else { + Serial.println("\nPico detected!\n"); + return(false); + } +} + +void get_gps() { + + bool newData = false; + unsigned long start = millis(); + +// for (unsigned long start = millis(); millis() - start < 1000;) // 5000;) + while ((millis() - start) < 1000) // 5000;) + { + while (Serial2.available()) + { + char c = Serial2.read(); + if (show_gps) + Serial.write(c); // uncomment this line if you want to see the GPS data flowing + if (gps.encode(c)) // Did a new valid sentence come in? + newData = true; + } + } + if (newData) { + Serial.printf("GPS read new data in ms: %d\n", millis() - start); +// float flon = 0.0, flat = 0.0, flalt = 0.0; +// unsigned long age; +// starting = millis(); +// gps.f_get_position(&flat, &flon, &age); + + Serial.print(F("Location: ")); + if (gps.location.isValid()) + { + Serial.print(gps.location.lat(), 6); + Serial.print(F(",")); + Serial.print(gps.location.lng(), 6); + + flat = gps.location.lat(); + flon = gps.location.lng(); + flalt = gps.altitude.meters(); + } + else + { + Serial.print(F("INVALID")); + } + Serial.print("\r\n"); + + } else +// Serial.printf("GPS read no new data: %d\n", millis() - start); + ; + +} diff --git a/stempayload/Payload_BME280_MPU6050_XS/payload_extension.cpp b/stempayload/Payload_BME280_MPU6050_XS/payload_extension.cpp new file mode 100644 index 00000000..61725282 --- /dev/null +++ b/stempayload/Payload_BME280_MPU6050_XS/payload_extension.cpp @@ -0,0 +1,20 @@ +// Use this template for adding additional sensors +// see Payload_BME280_MPU6050_XS_Extended for an example + +// put your library includes here +#include "Arduino.h" + +// put your globals here + +// put your setup code here +void payload_setup() { + + Serial.println("Starting new sensor!"); + +} + +// put your loop code here +// Very Important: only use print, not println!! +void payload_loop() { + +} diff --git a/stempayload/Payload_BME280_MPU6050_XS_Extended/Payload_BME280_MPU6050_XS_Extended.ino b/stempayload/Payload_BME280_MPU6050_XS_Extended/Payload_BME280_MPU6050_XS_Extended.ino index f7b391b4..7825f2d1 100644 --- a/stempayload/Payload_BME280_MPU6050_XS_Extended/Payload_BME280_MPU6050_XS_Extended.ino +++ b/stempayload/Payload_BME280_MPU6050_XS_Extended/Payload_BME280_MPU6050_XS_Extended.ino @@ -1,49 +1,108 @@ +// code for Pico or Pro Micro or STM32 on the CubeSat Simulator STEM Payload board +// works wih CubeSatSim software v1.3.2 or later +// extra sensors can be added in payload_extension.cpp + #include #include #include #include #include -#include "Adafruit_SI1145.h" -#include +#include #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; MPU6050 mpu6050(Wire); -Adafruit_SI1145 uv = Adafruit_SI1145(); -Adafruit_LIS3MDL lis3mdl; +TinyGPSPlus gps; long timer = 0; int bmePresent; -int uvPresent; -int magPresent; int RXLED = 17; // The RX LED has a defined Arduino pin int greenLED = 9; int blueLED = 8; -void eeprom_word_write(int addr, int val); -short eeprom_word_read(int addr); +int Sensor1 = 0; +float Sensor2 = 0; +void ee_prom_word_write(int addr, int val); +short ee_prom_word_read(int addr); int first_time = true; int first_read = true; +bool check_for_wifi(); +bool wifi = false; +int led_builtin_pin; +#define PICO_W // define if Pico W board. Otherwise, compilation fail for Pico or runtime fail if compile as Pico W + +#if defined ARDUINO_ARCH_RP2040 +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 __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; -float magRaw = 0; -float magRawAbs = 0; -void setup() { +char sensor_end_flag[] = "_END_FLAG_"; +char sensor_start_flag[] = "_START_FLAG_"; +bool show_gps = true; // set to false to not see all messages +float flon = 0.0, flat = 0.0, flalt = 0.0; +void get_gps(); - Serial.begin(9600); // Serial Monitor for testing +extern void payload_setup(); // sensor extension setup function defined in payload_extension.cpp +extern void payload_loop(); // sensor extension read function defined in payload_extension.cpp - Serial1.begin(115200); // Pi UART faster speed +void setup() { +#ifdef ARDUINO_ARCH_RP2040 + Serial1.setRX(1); + delay(100); + Serial1.setTX(0); + delay(100); +#endif + + Serial.begin(115200); // Serial Monitor for testing + + Serial1.begin(115200); // Pi UART faster spd +// Serial1.begin(9600); // Pi UART faster spd + + delay(10000); + Serial.println("Starting!"); - blink_setup(); +#ifdef ARDUINO_ARCH_RP2040 + Serial.println("This code is for the Raspberry Pi Pico hardware."); + + Serial.println("Starting Serial2 for optional GPS on JP12"); +// Serial2.begin(9600); // serial from - some modules need 115200 + // pinMode(0, INPUT); + // pinMode(1, INPUT); + + // set all Pico GPIO connected pins to input + for (int i = 6; i < 22; i++) { + pinMode(i, INPUT); + } + pinMode(26, INPUT); + pinMode(27, INPUT); + pinMode(28, INPUT); + pinMode(15, INPUT_PULLUP); // squelch + +#endif + + blink_setup(); // sensor extension setup function defined in payload_extension.cpp + blink(500); delay(250); blink(500); @@ -58,82 +117,77 @@ void setup() { if (bme.begin(0x76)) { bmePresent = 1; } else { - Serial.println("BME280 sensor fault"); + Serial.println("Could not find a valid BME280 sensor, check wiring!"); bmePresent = 0; } - - if (! uv.begin()) { - Serial.println("Si1145 sensor fault"); - uvPresent = 0; - } else { - uvPresent = 1; - } - - if (! lis3mdl.begin_I2C()) { - Serial.println("LIS3MDL sensor fault"); - magPresent = 0; - } else { - magPresent = 1; - } - + mpu6050.begin(); - + if (eeprom_word_read(0) == 0xA07) { Serial.println("Reading gyro offsets from EEPROM\n"); - + float xOffset = ((float)eeprom_word_read(1)) / 100.0; float yOffset = ((float)eeprom_word_read(2)) / 100.0; float zOffset = ((float)eeprom_word_read(3)) / 100.0; - + Serial.println(xOffset, DEC); Serial.println(yOffset, DEC); Serial.println(zOffset, DEC); - + mpu6050.setGyroOffsets(xOffset, yOffset, zOffset); } else { +#ifdef ARDUINO_ARCH_RP2040 + Serial.println("Calculating gyro offsets\n"); + mpu6050.calcGyroOffsets(true); +#endif +#ifndef ARDUINO_ARCH_RP2040 Serial.println("Calculating gyro offsets and storing in EEPROM\n"); - mpu6050.calcGyroOffsets(true); - + eeprom_word_write(0, 0xA07); eeprom_word_write(1, (int)(mpu6050.getGyroXoffset() * 100.0) + 0.5); eeprom_word_write(2, (int)(mpu6050.getGyroYoffset() * 100.0) + 0.5); eeprom_word_write(3, (int)(mpu6050.getGyroZoffset() * 100.0) + 0.5); - + Serial.println(eeprom_word_read(0), HEX); Serial.println(((float)eeprom_word_read(1)) / 100.0, DEC); Serial.println(((float)eeprom_word_read(2)) / 100.0, DEC); Serial.println(((float)eeprom_word_read(3)) / 100.0, DEC); +#endif } - pinMode(greenLED, OUTPUT); - pinMode(blueLED, OUTPUT); + payload_setup(); } - + void loop() { - if ((Serial.available() > 0) || first_time == true) { - blink(50); - char result = Serial.read(); - - 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(); + blink(50); + + if (Serial1.available() > 0) { + Serial.print("Received serial data!!!\n"); + delay(10); + while (Serial1.available() > 0) { + char result = Serial1.read(); + Serial.print(result); } - if ((result == '?') || first_time == true) + Serial.println(" "); + } + { +// if (result == '?') { - first_time = false; if (bmePresent) { + Serial1.print(sensor_start_flag); + 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(" "); @@ -144,9 +198,26 @@ void loop() { Serial.print(bme.readHumidity()); } else { + Serial1.print(sensor_start_flag); + 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()); + + Serial1.print(" "); + Serial1.print(mpu6050.getAccX()); + Serial1.print(" "); + Serial1.print(mpu6050.getAccY()); + Serial1.print(" "); + Serial1.print(mpu6050.getAccZ()); Serial.print(" MPU6050 "); Serial.print(mpu6050.getGyroX()); @@ -154,206 +225,295 @@ void loop() { Serial.print(mpu6050.getGyroY()); Serial.print(" "); Serial.print(mpu6050.getGyroZ()); - + Serial.print(" "); - Serial.print(mpu6050.getAccX()); + Serial.print(mpu6050.getAccX()); Serial.print(" "); - Serial.print(mpu6050.getAccY()); - Serial.print(" "); - Serial.print(mpu6050.getAccZ()); - - sensorValue = analogRead(A3); - Temp = T1 + (sensorValue - R1) * ((T2 - T1) / (R2 - R1)); - - Serial.print(" XS "); - Serial.print(Temp); + Serial.print(mpu6050.getAccY()); Serial.print(" "); - if (uvPresent) { - Serial.print(uv.readVisible()); - Serial.print(" "); - Serial.print(uv.readIR()); - Serial.print(" "); - } else - { - Serial.print("0.0 0.0 "); - } - if (magPresent) { - lis3mdl.read(); - magRaw = (((lis3mdl.x + lis3mdl.y + lis3mdl.z) / 3)); - magAbs = abs(magRaw); - Serial.println(magAbs); - } else - { - Serial.println("0.0"); - } - - 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()); - - if (acceleration > 1.2) + Serial.print(mpu6050.getAccZ()); + + sensorValue = read_analog(); + +// Serial.println(sensorValue); + Temp = T1 + (sensorValue - R1) *((T2 - T1)/(R2 - R1)); + +// Serial1.print(" GPS 0 0 0 TMP "); + + Serial1.print(" GPS "); + Serial1.print(flat,4); + Serial1.print(" "); + Serial1.print(flon,4); + Serial1.print(" "); + Serial1.print(flalt,2); + + Serial1.print(" TMP "); + Serial1.print(Temp); + +// Serial1.print(" "); +// Serial1.println(Sensor2); + + Serial.print(" GPS "); + Serial.print(flat,4); + Serial.print(" "); + Serial.print(flon,4); + Serial.print(" "); + Serial.print(flalt,2); + +// Serial.print(" GPS 0 0 0 TMP "); + Serial.print(" TMP "); + Serial.print(Temp); +// Serial.print(" "); +// Serial.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()); +// Serial.print(rotation); +// Serial.print(" "); +// Serial.println(acceleration); + + if (first_read == true) { + first_read = false; + rest = acceleration; + } + + if (acceleration > 1.2 * rest) led_set(greenLED, HIGH); - else + else led_set(greenLED, LOW); - - if (rotation > 5) + + if (rotation > 5) led_set(blueLED, HIGH); - else + else led_set(blueLED, LOW); } - } - if (Serial1.available() > 0) { + payload_loop(); // sensor extension read function defined in payload_extension.cpp - blink(50); - char result = Serial1.read(); +// Serial1.println(" "); + Serial1.println(sensor_end_flag); + Serial.println(" "); + + } - if (result == 'R') { + if (Serial.available() > 0) { + blink(50); + char result = Serial.read(); +// Serial.println(result); +// Serial.println("OK"); +// Serial.println(counter++); +#ifndef ARDUINO_ARCH_RP2040 + if (result == 'R') { Serial1.println("OK"); delay(100); first_read = true; setup(); } - - if (result == '?') - { - 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()); - } else - { - Serial1.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()); - - Serial1.print(" "); - Serial1.print(mpu6050.getAccX()); - Serial1.print(" "); - Serial1.print(mpu6050.getAccY()); - Serial1.print(" "); - Serial1.print(mpu6050.getAccZ()); - - sensorValue = analogRead(A3); - Temp = T1 + (sensorValue - R1) * ((T2 - T1) / (R2 - R1)); - - Serial1.print(" XS "); - Serial1.print(Temp); - Serial1.print(" "); - if (uvPresent) { - Serial1.print(uv.readVisible()); - Serial1.print(" "); - Serial1.print(uv.readIR()); - Serial1.print(" "); - } else - { - Serial1.print("0.0 0.0 "); - } - if (magPresent) { - lis3mdl.read(); - magRaw = (((lis3mdl.x + lis3mdl.y + lis3mdl.z) / 3)); - magAbs = abs(magRaw); - Serial1.println(magAbs); - } else - { - Serial1.println("0.0"); - } - - - 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()); - - if (first_read == true) { - first_read = false; - rest = acceleration; - } - - if (acceleration > 1.2 * rest) - led_set(greenLED, HIGH); - else - led_set(greenLED, LOW); - - if (rotation > 5) - led_set(blueLED, HIGH); - else - led_set(blueLED, LOW); - } - } - + else if (result == 'C') { + Serial.println("Clearing stored gyro offsets in EEPROM\n"); + eeprom_word_write(0, 0x00); + first_time = true; + setup(); + } +#endif + } + +#ifdef ARDUINO_ARCH_RP2040 + Serial.print("Squelch: "); + Serial.println(digitalRead(15)); +#endif + +// delay(1000); not needed due to gps 1 second polling delay + get_gps(); + } - + void eeprom_word_write(int addr, int val) { EEPROM.write(addr * 2, lowByte(val)); EEPROM.write(addr * 2 + 1, highByte(val)); } - + short eeprom_word_read(int addr) { return ((EEPROM.read(addr * 2 + 1) << 8) | EEPROM.read(addr * 2)); } - -void blink_setup() + +void blink_setup() { -#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) +#if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) // initialize digital pin PB1 as an output. pinMode(PC13, OUTPUT); pinMode(PB9, OUTPUT); pinMode(PB8, OUTPUT); #endif - + #if defined __AVR_ATmega32U4__ pinMode(RXLED, OUTPUT); // Set RX LED as an output // TX LED is set as an output behind the scenes pinMode(greenLED, OUTPUT); - pinMode(blueLED, OUTPUT); + pinMode(blueLED,OUTPUT); +#endif + +#if defined ARDUINO_ARCH_RP2040 + if (check_for_wifi()) { + wifi = true; + led_builtin_pin = LED_BUILTIN; // use default GPIO for Pico W + pinMode(LED_BUILTIN, OUTPUT); +// configure_wifi(); + } else { + led_builtin_pin = 25; // manually set GPIO 25 for Pico board +// pinMode(25, OUTPUT); + pinMode(led_builtin_pin, OUTPUT); + + pinMode(18, OUTPUT); + pinMode(19, OUTPUT); + } #endif -} +} + void blink(int length) { #if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) digitalWrite(PC13, LOW); // turn the LED on (HIGH is the voltage level) #endif - + #if defined __AVR_ATmega32U4__ digitalWrite(RXLED, LOW); // set the RX LED ON TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF -#endif - - delay(length); // wait for a lenth of time +#endif +#if defined ARDUINO_ARCH_RP2040 + if (wifi) + digitalWrite(LED_BUILTIN, HIGH); // set the built-in LED ON + else + digitalWrite(led_builtin_pin, HIGH); // set the built-in LED ON +#endif + #if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) digitalWrite(PC13, HIGH); // turn the LED off by making the voltage LOW #endif - + #if defined __AVR_ATmega32U4__ digitalWrite(RXLED, HIGH); // set the RX LED OFF TXLED0; //TX LED macro to turn LED ON -#endif -} +#endif +#if defined ARDUINO_ARCH_RP2040 + if (wifi) + digitalWrite(LED_BUILTIN, LOW); // set the built-in LED OFF + else + digitalWrite(led_builtin_pin, LOW); // set the built-in LED OFF + +// delay(length); // wait for a lenth of time +#endif + +} + void led_set(int ledPin, bool state) { #if defined(ARDUINO_ARCH_STM32F0) || defined(ARDUINO_ARCH_STM32F1) || defined(ARDUINO_ARCH_STM32F3) || defined(ARDUINO_ARCH_STM32F4) || defined(ARDUINO_ARCH_STM32L4) if (ledPin == greenLED) digitalWrite(PB9, state); else if (ledPin == blueLED) - digitalWrite(PB8, state); + digitalWrite(PB8, state); #endif - + #if defined __AVR_ATmega32U4__ - digitalWrite(ledPin, state); + digitalWrite(ledPin, state); +#endif + +#ifdef ARDUINO_ARCH_RP2040 + if (ledPin == greenLED) + digitalWrite(19, state); + else if (ledPin == blueLED) + digitalWrite(18, 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 +#if defined ARDUINO_ARCH_RP2040 + sensorValue = analogRead(28); +#endif + return(sensorValue); +} + +bool check_for_wifi() { + +#ifndef PICO_W + + Serial.println("WiFi disabled in software"); + return(false); // skip check if not Pico W board or compilation will fail + +#endif + + pinMode(29, INPUT); + const float conversion_factor = 3.3f / (1 << 12); + uint16_t result = analogRead(29); +// Serial.printf("ADC3 value: 0x%03x, voltage: %f V\n", result, result * conversion_factor); + + if (result < 0x10) { + Serial.println("\nPico W detected!\n"); + return(true); + } + else { + Serial.println("\nPico detected!\n"); + return(false); + } +} + +void get_gps() { + + bool newData = false; + unsigned long start = millis(); + +// for (unsigned long start = millis(); millis() - start < 1000;) // 5000;) + while ((millis() - start) < 1000) // 5000;) + { + while (Serial2.available()) + { + char c = Serial2.read(); + if (show_gps) + Serial.write(c); // uncomment this line if you want to see the GPS data flowing + if (gps.encode(c)) // Did a new valid sentence come in? + newData = true; + } + } + if (newData) { + Serial.printf("GPS read new data in ms: %d\n", millis() - start); +// float flon = 0.0, flat = 0.0, flalt = 0.0; +// unsigned long age; +// starting = millis(); +// gps.f_get_position(&flat, &flon, &age); + + Serial.print(F("Location: ")); + if (gps.location.isValid()) + { + Serial.print(gps.location.lat(), 6); + Serial.print(F(",")); + Serial.print(gps.location.lng(), 6); + + flat = gps.location.lat(); + flon = gps.location.lng(); + flalt = gps.altitude.meters(); + } + else + { + Serial.print(F("INVALID")); + } + Serial.print("\r\n"); + + } else +// Serial.printf("GPS read no new data: %d\n", millis() - start); + ; + } diff --git a/stempayload/Payload_BME280_MPU6050_XS_Extended/payload_extension.cpp b/stempayload/Payload_BME280_MPU6050_XS_Extended/payload_extension.cpp new file mode 100644 index 00000000..b88dd680 --- /dev/null +++ b/stempayload/Payload_BME280_MPU6050_XS_Extended/payload_extension.cpp @@ -0,0 +1,71 @@ +// Use this template for adding additional sensors + +// put your library includes here +#include "Adafruit_SI1145.h" +#include + +// put your globals here +Adafruit_SI1145 uv = Adafruit_SI1145(); +Adafruit_LIS3MDL lis3mdl; + +int uvPresent; +int magPresent; + +float magRaw = 0; +float magRawAbs = 0; + +// put your setup code here +void payload_setup() { + + Serial.println("Starting Si1145 sensor!"); + + if (! uv.begin()) { + Serial.println("Si1145 sensor fault"); + uvPresent = 0; + } else { + uvPresent = 1; + } + + Serial.println("Starting LIS3MDL sensor!"); + + if (! lis3mdl.begin_I2C()) { + Serial.println("LIS3MDL sensor fault"); + magPresent = 0; + } else { + magPresent = 1; + } +} + +// put your loop code here +// Very Important: only use print, not println!! +void payload_loop() { + + if (uvPresent) { + Serial1.print(" SI "); // chose a 2-3 letter hint for your sensor + Serial1.print(uv.readVisible()); // Serial1 sends the sensor data to the Pi Zero for transmission + Serial1.print(" "); + Serial1.print(uv.readIR()); + + Serial.print(" SI "); + Serial.print(uv.readVisible()); // Serial sends the sensor data to the Serial Monitor for debugging + Serial.print(" "); + Serial.print(uv.readIR()); + } else { + Serial1.print(" SI 0.0 0.0"); + Serial.print(" SI 0.0 0.0"); + } + if (magPresent) { + lis3mdl.read(); + magRaw = (((lis3mdl.x + lis3mdl.y + lis3mdl.z) / 3)); + magRawAbs = abs(magRaw); + + Serial1.print(" LI "); + Serial1.print(magRawAbs); + + Serial1.print(" LI "); + Serial.print(magRawAbs); + } else { + Serial1.print(" LI 0.0"); + Serial.print(" LI 0.0"); + } +}