From ea087e182db97db2b0ebbf8581ddd9d7cacb7472 Mon Sep 17 00:00:00 2001 From: Rihards Skuja Date: Sat, 20 Jan 2018 20:43:41 +0200 Subject: [PATCH] Send all sensor values from robot to base --- base_station/include/main.h | 17 ++++++++++ base_station/src/main.c | 35 +++++++++++++++---- robot/include/main.h | 17 +++++++++- robot/src/main.c | 67 ++++++++++++++++++++++--------------- 4 files changed, 101 insertions(+), 35 deletions(-) diff --git a/base_station/include/main.h b/base_station/include/main.h index 0c81e3c..ed46853 100644 --- a/base_station/include/main.h +++ b/base_station/include/main.h @@ -3,7 +3,24 @@ #include +#define MAX_PAYLOAD_SIZE 32 + +enum return_codes { + R_OK, + R_PACKAGE_SIZE, +}; + +typedef struct { + /* uint8_t temp; */ + double temp; + /* uint8_t distance; */ + uint16_t distance; +} data_packet_t; + uint8_t tx_address[5] = {0xD7, 0xD7, 0xD7, 0xD7, 0xD7}; uint8_t rx_address[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7}; + +void convert_raw_to_data(uint8_t *in, data_packet_t *out); + #endif diff --git a/base_station/src/main.c b/base_station/src/main.c index fbe06ec..38f434e 100644 --- a/base_station/src/main.c +++ b/base_station/src/main.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -12,28 +14,47 @@ int main(void) { + uint8_t *rx_raw; + data_packet_t *rx_data; + + // Data packet is too large, should split in multiple packets + if (sizeof(data_packet_t) > MAX_PAYLOAD_SIZE) + return R_PACKAGE_SIZE; + rx_data = malloc(sizeof(data_packet_t)); + rx_raw = malloc(sizeof(data_packet_t)); + init_leds(); nrf24_init(); - nrf24_config(2, 1); // Channel #2, payload: 1 - nrf24_tx_address(tx_address); + nrf24_config(2, sizeof(data_packet_t)); // Channel #2 + nrf24_rx_address(rx_address); nrf24_rx_address(rx_address); // Use green LED as power LED - SET(LED_PORT, LED_GREEN_BIT); + // TODO: need all LEDs for debugging, will use as power when UART debugger + // will arrive + /* SET(LED_PORT, LED_GREEN_BIT); */ - uint8_t data_array[1]; while (1) { if (nrf24_dataReady()) { - nrf24_getData(data_array); + memset(rx_raw, 0, sizeof(data_packet_t)); + nrf24_getData(rx_raw); + memcpy(rx_data, rx_raw, sizeof(data_packet_t)); - if (data_array[0] == 1) + if (rx_data->distance < 10) SET(LED_PORT, LED_BLUE_BIT); - else if (data_array[0] == 0) + else CLR(LED_PORT, LED_BLUE_BIT); + + if (rx_data->temp > 23) + SET(LED_PORT, LED_GREEN_BIT); + else + CLR(LED_PORT, LED_GREEN_BIT); } } + free(rx_data); + free(rx_raw); return 0; } diff --git a/robot/include/main.h b/robot/include/main.h index 97c3bb9..2f7040a 100644 --- a/robot/include/main.h +++ b/robot/include/main.h @@ -3,9 +3,24 @@ #include +#define MAX_PAYLOAD_SIZE 32 + +enum return_codes { + R_OK, + R_PACKAGE_SIZE, +}; + +typedef struct { + double temp; + /* uint8_t temp; */ + /* uint8_t distance; */ + uint16_t distance; +} data_packet_t; + uint8_t tx_address[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7}; uint8_t rx_address[5] = {0xD7, 0xD7, 0xD7, 0xD7, 0xD7}; -void read_temp(void); + +void convert_data_to_raw(data_packet_t *in, uint8_t *out); #endif diff --git a/robot/src/main.c b/robot/src/main.c index 1d83295..86cf8e6 100644 --- a/robot/src/main.c +++ b/robot/src/main.c @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -12,51 +14,62 @@ #include "debug.h" #endif /* DEBUG */ -void read_temp(void) -{ - double d; - - sei(); - for (;;) { - d = ds18b20_gettemp(); - if (d >= 23) - ; - else - ; - - _delay_ms(500); - } -} - int main(void) { + uint8_t *tx_raw; + data_packet_t *tx_data; + + // Data packet is too large, should split in multiple packets + if (sizeof(data_packet_t) > MAX_PAYLOAD_SIZE) + return R_PACKAGE_SIZE; + tx_data = malloc(sizeof(data_packet_t)); + tx_raw = malloc(sizeof(data_packet_t)); + init_leds(); /* init_servos(); */ init_sonar(); nrf24_init(); - nrf24_config(2, 1); // Channel #2, payload: 1 + nrf24_config(2, sizeof(data_packet_t)); // Channel #2 nrf24_tx_address(tx_address); nrf24_rx_address(rx_address); // Use green LED as power LED - SET(LED_PORT, LED_GREEN_BIT); + // TODO: need all LEDs for debugging, will use as power when UART debugger + // will arrive + /* SET(LED_PORT, LED_GREEN_BIT); */ + sei(); /* run_servos(); */ /* read_temp(); */ - uint8_t data_array[1]; + while (1) { uint16_t distance_int_cm = read_sonar(); + double temp = ds18b20_gettemp(); - if (distance_int_cm <= 10 && distance_int_cm != SONAR_ERROR) - data_array[0] = 1; - else - data_array[0] = 0; - nrf24_send(data_array); - while (nrf24_isSending()); + if (distance_int_cm <= 10 && distance_int_cm != SONAR_ERROR) { + SET(LED_PORT, LED_BLUE_BIT); + } else { + CLR(LED_PORT, LED_BLUE_BIT); + } + if (temp >= 23) { + SET(LED_PORT, LED_GREEN_BIT); + } else { + CLR(LED_PORT, LED_GREEN_BIT); + } - _delay_ms(SONAR_DELAY); + memset(tx_data, 0, sizeof(data_packet_t)); + tx_data->distance = distance_int_cm; + tx_data->temp = temp; + + memcpy(tx_raw, tx_data, sizeof(data_packet_t)); + nrf24_send(tx_raw); + // TODO: prevent transmission to block the main loop + while (nrf24_isSending()); // Wait for transmission to finish } - return 0; + free(tx_data); + free(tx_raw); + + return R_OK; }