Send all sensor values from robot to base

This commit is contained in:
Rihards Skuja 2018-01-20 20:43:41 +02:00
parent 4ea41527f1
commit ea087e182d
No known key found for this signature in database
GPG Key ID: 53FA13A3F7F8571B
4 changed files with 101 additions and 35 deletions

View File

@ -3,7 +3,24 @@
#include <stdint.h>
#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

View File

@ -1,3 +1,5 @@
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
@ -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;
}

View File

@ -3,9 +3,24 @@
#include <stdint.h>
#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

View File

@ -1,3 +1,5 @@
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
@ -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;
}