From ae9a8d60a656490d06d2436f56ea30e163ac6d03 Mon Sep 17 00:00:00 2001 From: Jakub Kakona Date: Fri, 26 Apr 2024 14:32:16 +0200 Subject: [PATCH] Initial version of TFHT01 testing script. --- sw/TFHT01_test.py | 62 ++++++ sw/arduino/SPS30MAV_small/ArduinoMavlink.cpp | 139 ------------- sw/arduino/SPS30MAV_small/ArduinoMavlink.h | 39 ---- sw/arduino/SPS30MAV_small/SPS30MAV_small.ino | 198 ------------------- sw/arduino/sps30/sps30.ino | 154 --------------- 5 files changed, 62 insertions(+), 530 deletions(-) create mode 100755 sw/TFHT01_test.py delete mode 100644 sw/arduino/SPS30MAV_small/ArduinoMavlink.cpp delete mode 100644 sw/arduino/SPS30MAV_small/ArduinoMavlink.h delete mode 100644 sw/arduino/SPS30MAV_small/SPS30MAV_small.ino delete mode 100644 sw/arduino/sps30/sps30.ino diff --git a/sw/TFHT01_test.py b/sw/TFHT01_test.py new file mode 100755 index 0000000..80ad304 --- /dev/null +++ b/sw/TFHT01_test.py @@ -0,0 +1,62 @@ +#!/usr/bin/python + +# Python library for SHT3101A MLAB module with SHT31 Temperature and relative humidity sensor. + +#uncomment for debbug purposes +#import logging +#logging.basicConfig(level=logging.DEBUG) + +import time +import datetime +import sys +from pymlab import config + +#### Script Arguments ############################################### + +if len(sys.argv) not in (2, 3): + sys.stderr.write("Invalid number of arguments.\n") + sys.stderr.write("Usage: %s PORT ADDRESS\n" % (sys.argv[0], )) + sys.exit(1) + +port = eval(sys.argv[1]) + +if len(sys.argv) == 3: + address = eval(sys.argv[2]) +else: + address = 0x44 + +#### Sensor Configuration ########################################### + +cfg = config.Config( + i2c = { + "port": port, + "device": 'hid', + }, + bus = [ + { + "name": "sht", + "type": "sht31", + "address": address, + }, + ], +) + + +cfg.initialize() + +print ("SHT31 sensor readout example \r\n") +sensor = cfg.get_device("sht") + +#sensor.soft_reset() # TODO, not implemented in pymlab dev branch. +time.sleep(0.1) + +#### Data Logging ################################################### + +try: + while True: + temperature, humidity = sensor.get_TempHum() + sys.stdout.write("Sensor status: %s, Temperature: %0.2f, Humidity: %0.2f\r\n" % (sensor.get_status(), temperature, humidity)) + sys.stdout.flush() + time.sleep(1) +except KeyboardInterrupt: + sys.exit(0) diff --git a/sw/arduino/SPS30MAV_small/ArduinoMavlink.cpp b/sw/arduino/SPS30MAV_small/ArduinoMavlink.cpp deleted file mode 100644 index ee4b4bf..0000000 --- a/sw/arduino/SPS30MAV_small/ArduinoMavlink.cpp +++ /dev/null @@ -1,139 +0,0 @@ -/* - ArduinoMavlink.cpp adopted from PixhawkArduinoMAVLink.cpp - Updated by Roman Dvorak, 2020-10, dvorakroman@thunderfly.cz, ThunderFly s.r.o, - - PixhawkArduinoMAVLink.cpp - Library for using Arduino to recieve Pixhawk's sensor data as well as some other usefull data which you might need. - Created by Shashi Kant, June 23, 2018. -*/ - -#include "ArduinoMavlink.h" - -ArduinoMavlink::ArduinoMavlink(HardwareSerial &hs){ - _MAVSerial = &hs; - MILLIG_TO_MS2 = 9.80665 / 1000.0; - system_id = 1; // Your i.e. Arduino sysid - component_id = 158; // Your i.e. Arduino compid - type = MAV_TYPE_QUADROTOR; - autopilot = MAV_AUTOPILOT_INVALID; -} - -bool ArduinoMavlink::begin(){ - _MAVSerial->begin(57600); - if(_MAVSerial->available()<=0){ - return 0; - }else{ - return 1; - } -} - -// At first we will send some HeartBeats to Pixhawk to check whether it's available or not?? -// After that we will check for whether we are recieving HeartBeats back from Pixhawk if Yes, -// We will note down its sysid and compid to send it a req to Stream Data. -void ArduinoMavlink::Stream(){ - delay(2000); - int flag=1; - Serial.println("Sending Heartbeats..."); - mavlink_message_t msghb; - mavlink_heartbeat_t heartbeat; - uint8_t bufhb[MAVLINK_MAX_PACKET_LEN]; - mavlink_msg_heartbeat_pack(system_id, component_id, &msghb, type, autopilot, MAV_MODE_PREFLIGHT, 0, MAV_STATE_STANDBY); - uint16_t lenhb = mavlink_msg_to_send_buffer(bufhb, &msghb); - delay(1000); - _MAVSerial->write(bufhb,lenhb); - Serial.println("Heartbeats sent! Now will check for recieved heartbeats to record sysid and compid..."); - - // Looping untill we get the required data. - while(flag==1){ - delay(1); - while(_MAVSerial->available()>0){ - mavlink_message_t msgpx; - mavlink_status_t statuspx; - uint8_t ch = _MAVSerial->read(); - if(mavlink_parse_char(MAVLINK_COMM_0, ch, &msgpx, &statuspx)){ - Serial.println("Message Parsing Done!"); - switch(msgpx.msgid){ - case MAVLINK_MSG_ID_HEARTBEAT: - { - mavlink_heartbeat_t packet; - mavlink_msg_heartbeat_decode(&msgpx, &packet); - received_sysid = msgpx.sysid; - received_compid = msgpx.compid; - Serial.println("sysid and compid successfully recorded"); - flag = 0; - break; - } - } - } - } - } - -// Sending request for data stream... -// Serial.println("Now sending request for data stream..."); -// delay(2000); -// mavlink_message_t msgds; -// uint8_t bufds[MAVLINK_MAX_PACKET_LEN]; -// mavlink_msg_request_data_stream_pack(system_id, component_id, &msgds, received_sysid, received_compid, MAV_DATA_STREAM_ALL , 0x05, 1); -// uint16_t lends = mavlink_msg_to_send_buffer(bufds, &msgds); -// delay(1000); -// _MAVSerial->write(bufds,lends); - Serial.println("Request sent! Now you are ready to recieve datas..."); - -} - -void ArduinoMavlink::SendHeartBeat(){ - - Serial.println("Sending Heartbeats..."); - mavlink_message_t msghb; - mavlink_heartbeat_t heartbeat; - uint8_t bufhb[MAVLINK_MAX_PACKET_LEN]; - mavlink_msg_heartbeat_pack(system_id, component_id, &msghb, type, autopilot, MAV_MODE_PREFLIGHT, 0, MAV_STATE_STANDBY); - uint16_t lenhb = mavlink_msg_to_send_buffer(bufhb, &msghb); - _MAVSerial->write(bufhb,lenhb); - - return; -} - -void ArduinoMavlink::SendTunnelData(uint8_t *payload_data, uint8_t payload_length, uint8_t payload_type = 0, uint8_t sysid = 0, uint8_t compid = 0){ - - Serial.println("Sending Tunnel data..."); - mavlink_message_t msgtn; - mavlink_tunnel_t tunnel; - uint8_t buftn[MAVLINK_MAX_PACKET_LEN]; - mavlink_msg_tunnel_pack(system_id, component_id, &msgtn, sysid, compid, payload_type, payload_length, payload_data); - uint16_t lentn = mavlink_msg_to_send_buffer(buftn, &msgtn); - _MAVSerial->write(buftn, lentn); - - return; -} - - -bool ArduinoMavlink::ReadSystemTime(uint64_t *time_unix_usec, uint32_t *time_boot_ms, uint32_t max_delay = 5000){ - unsigned long time_until; - time_until = millis() + max_delay; - - - while(_MAVSerial->available() > 0 ){ - delay(10); - mavlink_message_t msg; - mavlink_status_t status1; - uint8_t ch = _MAVSerial->read(); - - if(mavlink_parse_char(MAVLINK_COMM_0, ch, &msg, &status1)){ - if(msg.msgid == MAVLINK_MSG_ID_SYSTEM_TIME){ - mavlink_system_time_t data; - mavlink_msg_system_time_decode(&msg, &data); - - *time_unix_usec = data.time_unix_usec; - *time_boot_ms = data.time_boot_ms; - - return 1; - } - } - - if(time_until < millis()){ - Serial.print("TimeOut"); - return 0; - } - } - return 0; -} diff --git a/sw/arduino/SPS30MAV_small/ArduinoMavlink.h b/sw/arduino/SPS30MAV_small/ArduinoMavlink.h deleted file mode 100644 index 6284f98..0000000 --- a/sw/arduino/SPS30MAV_small/ArduinoMavlink.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - ArduinoMavlink.h adopted from PixhawkArduinoMAVLink.h - Updated by Roman Dvorak, 2020-10, dvorakroman@thunderfly.cz, ThunderFly s.r.o, - - ArduinoMavlink.h - Library for using Arduino to recieve Pixhawk sensors data. - Created by Shashi Kant, June 23, 2018. -*/ - -#ifndef ArduinoMavlink_h -#define ArduinoMavlink_h - -#include "lib/c_library_v2/common/mavlink.h" -#include "lib/c_library_v2/checksum.h" -#include "lib/c_library_v2/mavlink_types.h" -#include "lib/c_library_v2/protocol.h" -#include -//#include - -class ArduinoMavlink -{ - public: - ArduinoMavlink(HardwareSerial &hs); - bool begin(); - void SendHeartBeat(); - void SendTunnelData(uint8_t *payload_data, uint8_t payload_length, uint8_t payload_type, uint8_t sysid, uint8_t compid); - bool ReadSystemTime(uint64_t *time_unix_usec, uint32_t *time_boot_ms, uint32_t max_delay); - void Stream(); - private: - HardwareSerial* _MAVSerial; - double MILLIG_TO_MS2; - uint8_t system_id; - uint8_t component_id; - uint8_t type; - uint8_t autopilot; - uint8_t received_sysid; - uint8_t received_compid; -}; - -#endif diff --git a/sw/arduino/SPS30MAV_small/SPS30MAV_small.ino b/sw/arduino/SPS30MAV_small/SPS30MAV_small.ino deleted file mode 100644 index 49ccd71..0000000 --- a/sw/arduino/SPS30MAV_small/SPS30MAV_small.ino +++ /dev/null @@ -1,198 +0,0 @@ -#include "ArduinoMavlink.h" -#include -/* - - Mighty 1284p - +---\/---+ - (D 0) PB0 1| |40 PA0 (AI 0 / D24) - (D 1) PB1 2| |39 PA1 (AI 1 / D25) - INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D26) - PWM (D 3) PB3 4| |37 PA3 (AI 3 / D27) - PWM/SS (D 4) PB4 5| |36 PA4 (AI 4 / D28) - MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D29) - PWM/MISO (D 6) PB6 7| |34 PA6 (AI 6 / D30) - PWM/SCK (D 7) PB7 8| |33 PA7 (AI 7 / D31) - RST 9| |32 AREF - VCC 10| |31 GND - GND 11| |30 AVCC - XTAL2 12| |29 PC7 (D 23) - XTAL1 13| |28 PC6 (D 22) - RX0 (D 8) PD0 14| |27 PC5 (D 21) TDI - TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO -RX1/INT0 (D 10) PD2 16| |25 PC3 (D 19) TMS -TX1/INT1 (D 11) PD3 17| |24 PC2 (D 18) TCK - PWM (D 12) PD4 18| |23 PC1 (D 17) SDA - PWM (D 13) PD5 19| |22 PC0 (D 16) SCL - PWM (D 14) PD6 20| |21 PD7 (D 15) PWM - +--------+ -*/ - -HardwareSerial &hs = Serial1; -ArduinoMavlink mav(hs); - -#define PPS 12 // PD4 -boolean led_status = false; -uint8_t data[42]; - -void setup() -{ - int16_t ret; - - pinMode(PPS, INPUT); // sets the digital pin as input - - Serial.begin(9600); - Serial1.begin(57600); - - delay(250); - while(!mav.begin()) - { - Serial.println("Not Connected!"); - delay(250); - } - - sensirion_i2c_init(); - - while (sps30_probe() != 0) - { - Serial.print("SPS sensor probing failed\n"); - delay(500); - } - Serial.print("SPS sensor probing successful\n"); - - ret = sps30_start_measurement(); - if (ret < 0) - { - Serial.print("error starting measurement\n"); - } - Serial.print("measurements started\n"); - - //mav.Stream(); - delay(2000); -} - - -void loop() -{ - struct sps30_measurement m; - char serial[SPS30_MAX_SERIAL_LEN]; - uint16_t data_ready; - int16_t ret; - - while(digitalRead(PPS) == false); // Waiting for 1PPS signal from GPS - - do - { - ret = sps30_read_data_ready(&data_ready); - if (ret < 0) - { - Serial.print("error reading data-ready flag: "); - Serial.println(ret); - } - else if (!data_ready) - { - Serial.print("data not ready, no new measurement available\n"); - delay(100); /* retry in 100 ms */ - } - else - break; - } while (1); - - ret = sps30_read_measurement(&m); - if (ret < 0) { - Serial.print("error reading measurement\n"); - } - else - { - Serial.println(); - - uint32_t MC1p0 = round(m.mc_1p0 * 100); - uint32_t MC2p5 = round((m.mc_2p5 - m.mc_1p0) * 100); - uint32_t MC4p0 = round((m.mc_4p0 - m.mc_2p5) * 100); - uint32_t MC10p0 = round((m.mc_10p0 - m.mc_4p0) * 100); - - Serial.print("PM 0.3 - 1.0: "); - Serial.println(m.mc_1p0); - Serial.print("PM 1.0 - 2.5: "); - Serial.println(m.mc_2p5 - m.mc_1p0); - Serial.print("PM 2.5 - 4.0: "); - Serial.println(m.mc_4p0 - m.mc_2p5); - Serial.print("PM 4.0 - 10.0: "); - Serial.println(m.mc_10p0 - m.mc_4p0); - - uint32_t NC0p5 = round(m.nc_0p5 * 100); - uint32_t NC1p0 = round((m.nc_1p0 - m.nc_0p5) * 100); - uint32_t NC2p5 = round((m.nc_2p5 - m.nc_1p0) * 100); - uint32_t NC4p0 = round((m.nc_4p0 - m.nc_2p5) * 100); - uint32_t NC10p0 = round((m.nc_10p0 - m.nc_4p0) * 100); - - mav.SendHeartBeat(); - - Serial.print("NC 0.3 - 0.5: "); - Serial.println(m.nc_0p5); - Serial.print("NC 0.5 - 1.0: "); - Serial.println(m.nc_1p0 - m.nc_0p5); - Serial.print("NC 1.0 - 2.5: "); - Serial.println(m.nc_2p5 - m.nc_1p0); - Serial.print("NC 2.5 - 4.0: "); - Serial.println(m.nc_4p0 - m.nc_2p5); - Serial.print("NC 4.0 - 10.0: "); - Serial.println(m.nc_10p0 - m.nc_4p0); - - uint32_t TPS = round(m.typical_particle_size * 100); - - Serial.print("Typical partical size: "); - Serial.println(m.typical_particle_size); - - data[0] = (NC0p5) & 0xFF; - data[1] = (NC0p5 >> 8) & 0xFF; - data[2] = (NC0p5 >> 16) & 0xFF; - data[3] = (NC0p5 >> 24) & 0xFF; - data[4] = (NC1p0) & 0xFF; - data[5] = (NC1p0 >> 8) & 0xFF; - data[6] = (NC1p0 >> 16) & 0xFF; - data[7] = (NC1p0 >> 24) & 0xFF; - data[8] = (NC2p5) & 0xFF; - data[9] = (NC2p5 >> 8) & 0xFF; - data[10] = (NC2p5 >> 16) & 0xFF; - data[11] = (NC2p5 >> 24) & 0xFF; - data[12] = (NC4p0) & 0xFF; - data[13] = (NC4p0 >> 8) & 0xFF; - data[14] = (NC4p0 >> 16) & 0xFF; - data[15] = (NC4p0 >> 24) & 0xFF; - data[16] = (NC10p0) & 0xFF; - data[17] = (NC10p0 >> 8) & 0xFF; - data[18] = (NC10p0 >> 16) & 0xFF; - data[19] = (NC10p0 >> 24) & 0xFF; - data[20] = (MC1p0) & 0xFF; - data[21] = (MC1p0 >> 8) & 0xFF; - data[22] = (MC1p0 >> 16) & 0xFF; - data[23] = (MC1p0 >> 24) & 0xFF; - data[24] = (MC2p5) & 0xFF; - data[25] = (MC2p5 >> 8) & 0xFF; - data[26] = (MC2p5 >> 16) & 0xFF; - data[27] = (MC2p5 >> 24) & 0xFF; - data[28] = (MC4p0) & 0xFF; - data[29] = (MC4p0 >> 8) & 0xFF; - data[30] = (MC4p0 >> 16) & 0xFF; - data[31] = (MC4p0 >> 24) & 0xFF; - data[32] = (MC10p0) & 0xFF; - data[33] = (MC10p0 >> 8) & 0xFF; - data[34] = (MC10p0 >> 16) & 0xFF; - data[35] = (MC10p0 >> 24) & 0xFF; - data[36] = (TPS) & 0xFF; - data[37] = (TPS >> 8) & 0xFF; - data[38] = (TPS >> 16) & 0xFF; - data[39] = (TPS >> 24) & 0xFF; - data[40] = 255; - data[41] = 255; - - // data array (max length 128), data array size, data type (0 default - unknown), target sysid, target compid - // For unicast (only for logging purposes) set sysid and compid to match the autopilot. For realtime visualisation, you can - // set sysid and comid to broadcast (0, 0) - mav.SendTunnelData(data, sizeof(data), 0, 0, 0); - - led_status = !led_status; - digitalWrite(13, led_status); - } - //delay(100); -} diff --git a/sw/arduino/sps30/sps30.ino b/sw/arduino/sps30/sps30.ino deleted file mode 100644 index 7dce19a..0000000 --- a/sw/arduino/sps30/sps30.ino +++ /dev/null @@ -1,154 +0,0 @@ -#include -#define PLOTTER_FORMAT -/* - - Mighty 1284p - +---\/---+ - (D 0) PB0 1| |40 PA0 (AI 0 / D24) - (D 1) PB1 2| |39 PA1 (AI 1 / D25) - INT2 (D 2) PB2 3| |38 PA2 (AI 2 / D26) - PWM (D 3) PB3 4| |37 PA3 (AI 3 / D27) - PWM/SS (D 4) PB4 5| |36 PA4 (AI 4 / D28) - MOSI (D 5) PB5 6| |35 PA5 (AI 5 / D29) - PWM/MISO (D 6) PB6 7| |34 PA6 (AI 6 / D30) - PWM/SCK (D 7) PB7 8| |33 PA7 (AI 7 / D31) - RST 9| |32 AREF - VCC 10| |31 GND - GND 11| |30 AVCC - XTAL2 12| |29 PC7 (D 23) - XTAL1 13| |28 PC6 (D 22) - RX0 (D 8) PD0 14| |27 PC5 (D 21) TDI - TX0 (D 9) PD1 15| |26 PC4 (D 20) TDO -RX1/INT0 (D 10) PD2 16| |25 PC3 (D 19) TMS -TX1/INT1 (D 11) PD3 17| |24 PC2 (D 18) TCK - PWM (D 12) PD4 18| |23 PC1 (D 17) SDA - PWM (D 13) PD5 19| |22 PC0 (D 16) SCL - PWM (D 14) PD6 20| |21 PD7 (D 15) PWM - +--------+ -*/ - -void setup() { - int16_t ret; - uint8_t auto_clean_days = 4; - uint32_t auto_clean; - - Serial.begin(9600); - delay(2000); - - sensirion_i2c_init(); - - while (sps30_probe() != 0) { - Serial.print("SPS sensor probing failed\n"); - delay(500); - } - -#ifndef PLOTTER_FORMAT - Serial.print("SPS sensor probing successful\n"); -#endif /* PLOTTER_FORMAT */ - - ret = sps30_set_fan_auto_cleaning_interval_days(auto_clean_days); - if (ret) { - Serial.print("error setting the auto-clean interval: "); - Serial.println(ret); - } - - ret = sps30_start_measurement(); - if (ret < 0) { - Serial.print("error starting measurement\n"); - } - -#ifndef PLOTTER_FORMAT - Serial.print("measurements started\n"); -#endif /* PLOTTER_FORMAT */ - -#ifdef SPS30_LIMITED_I2C_BUFFER_SIZE - Serial.print("Your Arduino hardware has a limitation that only\n"); - Serial.print(" allows reading the mass concentrations. For more\n"); - Serial.print(" information, please check\n"); - Serial.print(" https://github.com/Sensirion/arduino-sps#esp8266-partial-legacy-support\n"); - Serial.print("\n"); - delay(2000); -#endif - - delay(1000); -} - -void loop() { - struct sps30_measurement m; - char serial[SPS30_MAX_SERIAL_LEN]; - uint16_t data_ready; - int16_t ret; - - do { - ret = sps30_read_data_ready(&data_ready); - if (ret < 0) { - Serial.print("error reading data-ready flag: "); - Serial.println(ret); - } else if (!data_ready) - Serial.print("data not ready, no new measurement available\n"); - else - break; - delay(100); /* retry in 100ms */ - } while (1); - - ret = sps30_read_measurement(&m); - if (ret < 0) { - Serial.print("error reading measurement\n"); - } else { - -#ifndef PLOTTER_FORMAT - Serial.print("PM 1.0: "); - Serial.println(m.mc_1p0); - Serial.print("PM 2.5: "); - Serial.println(m.mc_2p5); - Serial.print("PM 4.0: "); - Serial.println(m.mc_4p0); - Serial.print("PM 10.0: "); - Serial.println(m.mc_10p0); - -#ifndef SPS30_LIMITED_I2C_BUFFER_SIZE - Serial.print("NC 0.3 - 0.5: "); - Serial.println(round(m.nc_0p5)); - Serial.print("NC 0.5 - 1.0: "); - Serial.println(round(m.nc_1p0 - m.nc_0p5)); - Serial.print("NC 1.0 - 2.5: "); - Serial.println(round(m.nc_2p5 - m.nc_1p0)); - Serial.print("NC 2.5 - 4.0: "); - Serial.println(round(m.nc_4p0 - m.nc_2p5)); - Serial.print("NC 4.0 - 10.0: "); - Serial.println(round(m.nc_10p0 - m.nc_4p0)); - - Serial.print("Typical partical size: "); - Serial.println(m.typical_particle_size); -#endif - - Serial.println(); - -#else - // since all values include particles smaller than X, if we want to create buckets we - // need to subtract the smaller particle count. - // This will create buckets (all values in micro meters): - // - particles <= 0,5 - // - particles > 0.5, <= 1 - // - particles > 1, <= 2.5 - // - particles > 2.5, <= 4 - // - particles > 4, <= 10 - - Serial.print(m.nc_0p5); - Serial.print(" "); - Serial.print(m.nc_1p0 - m.nc_0p5); - Serial.print(" "); - Serial.print(m.nc_2p5 - m.nc_1p0); - Serial.print(" "); - Serial.print(m.nc_4p0 - m.nc_2p5); - Serial.print(" "); - Serial.print(m.nc_10p0 - m.nc_4p0); - Serial.println(); - - -#endif /* PLOTTER_FORMAT */ - - } - - delay(1000); -}