From c98f84348cfef53fd60e6475015cf6431b2c5951 Mon Sep 17 00:00:00 2001 From: Stefan Mertl Date: Thu, 18 Jan 2018 13:57:42 +0100 Subject: [PATCH] Added the support for the DS18B20 thermometer on the GTS. Added the usage of sleep mode to save power. Changed some debug LED positions. Commented an unused TIMER1 interrupt handler. --- software/arduino/ruwai/ruwai.ino | 92 +++++++++++++++++++++++++++++++++++----- 1 file changed, 82 insertions(+), 10 deletions(-) diff --git a/software/arduino/ruwai/ruwai.ino b/software/arduino/ruwai/ruwai.ino index b6abc08..72e89ce 100644 --- a/software/arduino/ruwai/ruwai.ino +++ b/software/arduino/ruwai/ruwai.ino @@ -29,7 +29,11 @@ #include #include #include +#include +#include #include +#include +#include void data_ready_isr(void); void gps_pps_isr(void); @@ -42,11 +46,20 @@ void set_record_mode(void); void arduino_reconnect(void); void connect_gps(void); void init_state(void); +void init_thermometer(void); +void sleep_now(void); //void send_ruwai_message(uint8_t msg_class, uint8_t msg_id, void* msg, uint8_t length); HardwareSerial & usb_serial = Serial; HardwareSerial & gps_serial = Serial2; //HardwareSerial & ruwai_serial = Serial3; + +// Setup a oneWire bus. +OneWire one_wire(ONEWIREBUS); +DallasTemperature ds_sensors(&one_wire); +DeviceAddress ds_gts; + + uint8_t led = 13; uint8_t count = 0; uint8_t gps_byte_available = 0; @@ -70,7 +83,7 @@ RuwaiControl ruwai_control = RuwaiControl(); GpsLea6T gps = GpsLea6T(gps_serial); //GpsLea6T gps = GpsLea6T(usb_serial); RwAdc adc = RwAdc(S1_PCF_ADC, S1_PCF_PGA1, S1_PCF_PGA2, S1_PCF_PGA3, S1_PCF_PGA4); -Timer16 &timer1 = Timer1; +//Timer16 &timer1 = Timer1; uint8_t test_counter = 0; @@ -96,6 +109,8 @@ void setup(void) { pinMode(49, OUTPUT); digitalWrite(49, pin_state_48); + + // Initialize serial port used for communication with the BBB. ruwai_control.start_serial(RUWAI_SERIAL_BAUDRATE); @@ -125,6 +140,9 @@ void setup(void) { ruwai_control.send_status(RW_STATUS_NOTICE, "Connect to GPS."); connect_gps(); + usb_serial.println("Initializing the thermometer."); + init_thermometer(); + /* gps.set_timepulse2_frequency(2048000, 2048000); set_record_mode(); @@ -137,7 +155,7 @@ void setup(void) { usb_serial.println("Activate the Watchdog Timer."); ruwai_control.send_status(RW_STATUS_NOTICE, "Enable the watchdog timer."); wdt_enable(WDTO_4S); - + // TODO: Get the ADC configuration from ruwaicom. //adc.begin(ADC_MODE_LOWSPEED_CLKDIV_0, ADC_FORMAT_SPI_TDM_FIXED); //adc.begin(ADC_MODE_HIGHRES, ADC_FORMAT_SPI_TDM_FIXED); @@ -278,6 +296,38 @@ void connect_gps(void) ruwai_control.send_status(RW_STATUS_NOTICE, "Finished configuration of the GPS."); } +void +init_thermometer(void) +{ + // Connect to the DS18B20 thermometer. + usb_serial.println("Locating oneWire devices..."); + ds_sensors.begin(); + usb_serial.print("Found "); + usb_serial.print(ds_sensors.getDeviceCount(), DEC); + usb_serial.println(" devices."); + if (!ds_sensors.getAddress(ds_gts, 0)) + { + usb_serial.println("Unable to find address for OneWire sensor 0."); + ruwai_control.send_status(RW_STATUS_ERROR, "Unable to find address for OneWire sensor 0."); + } + // Set the sensor resolution to 10 bit (0.25 degree). + ds_sensors.setResolution(ds_gts, 10); + int resolution = ds_sensors.getResolution(ds_gts); + usb_serial.print("Device 0 Resolution: "); + usb_serial.println(resolution, DEC); + usb_serial.print("Requesting temperature..."); + ds_sensors.requestTemperaturesByAddress(ds_gts); + usb_serial.println("...done."); + float temp_c = ds_sensors.getTempC(ds_gts); + usb_serial.print("Temperature: "); + usb_serial.println(temp_c); + + rw_soh_envdata_t msg; + msg.temp_gts = (int32_t) (temp_c * 100); + ruwai_control.send_message(RW_CLASS_SOH, RW_SOH_ID_ENVDATA, &msg, sizeof(msg)); + +} + void arduino_reconnect(void) { @@ -318,6 +368,8 @@ void arduino_reconnect(void) status_msg += VERSION; ruwai_control.send_status(RW_STATUS_NOTICE, status_msg); + init_thermometer(); + set_control_mode(); usb_serial.println("Activate the Watchdog Timer."); @@ -349,6 +401,26 @@ void set_record_mode(void) } +void sleep_now(void) +{ + //set_sleep_mode(SLEEP_MODE_STANDBY); + set_sleep_mode(SLEEP_MODE_IDLE); + sleep_enable(); + + // Disable peripherals that are not needed. + power_adc_disable(); + power_spi_disable(); + power_timer0_disable(); + power_timer1_disable(); + power_twi_disable(); + + sleep_mode(); + + // Upon waking up. + sleep_disable(); + power_all_enable(); +} + void loop(void) { @@ -458,7 +530,7 @@ void control_loop(void) void record_loop(void) { - + digitalWrite(46, HIGH); bool gps_parsed = false; bool ruwai_parsed = false; bool bbb_serial_free = false; @@ -529,9 +601,9 @@ void record_loop(void) gps_byte_available = gps_serial.available(); if (gps_byte_available > 0) { - digitalWrite(46, HIGH); + digitalWrite(47, HIGH); gps_parsed = gps.read(); - digitalWrite(46, LOW); + digitalWrite(47, LOW); } @@ -601,7 +673,6 @@ void record_loop(void) ***************************************************/ if (gps.nav_position_changed && bbb_serial_free) { - digitalWrite(47, HIGH); //status_msg = "[gps_pos] "; //status_msg = status_msg + gps.nav_position.lon + "," + gps.nav_position.lat + "," + gps.nav_position.height + "," + gps.nav_position.h_msl + "," + gps.nav_position.h_acc + "," + gps.nav_position.v_acc; rw_soh_gpspos_t msg; @@ -611,11 +682,8 @@ void record_loop(void) msg.height = gps.nav_position.height; ruwai_control.send_message(RW_CLASS_SOH, RW_SOH_ID_GPSPOS, &msg, sizeof(msg)); gps.nav_position_changed = false; - digitalWrite(47, LOW); } - - - + /*************************************************** @@ -646,6 +714,8 @@ void record_loop(void) parsed = false; */ } + digitalWrite(46, LOW); + sleep_now(); } @@ -724,6 +794,7 @@ send_samples_with_timestamp() // The timer1 interrupt service routine. +/* ISR(TIMER1_OVF_vect) { debug_print(); @@ -732,6 +803,7 @@ ISR(TIMER1_OVF_vect) //gps.poll_cfg_msg(NMEA_CLASS_STD, NMEA_STD_ID_GGA); gps.poll_cfg_tp5(TIMEPULSE2); } +*/ // The timer1 interrupt service routine. /* -- 2.11.4.GIT