We now give the complete source code for the satellite-stations in Listing 1.
/*
* satellite_station.c
* Author: Rayhan
*/
#include "radio.h"
#include "uart.h"
#include "roomba.h"
#include
#include
#include
uint8_t this_station_addr[5] = { 0xE4, 0xE4, 0xE4, 0xE4, 0xE4 };
volatile uint8_t rxflag = 0;
radiopacket_t packet;
int16_t previous_radius = 0;
int16_t current_radius = 500;
int16_t previous_speed = 0;
int16_t current_speed = 50;
void reset_radio()
{
PORTB &= ~_BV(PB6);
_delay_ms(100);
PORTB |= _BV(PB6);
_delay_ms(100);
}
void radio_rxhandler(uint8_t pipenumber)
{
rxflag = 1;
}
/*
packet contents: bytes 0:1 radius, 0th byte is the high byte
bytes 2:3 speed, 3rd byte is the high byte
*/
int16_t read_speed(uint8_t* message_content)
{
int16_t speed = message_content[0];
speed = (speed << 8) + message_content[1];
return speed;
}
int16_t read_radius(uint8_t* message_content)
{
int16_t radius = message_content[3];
radius = (radius << 8) + message_content[4];
return radius;
}
int main(void)
{
DDRB |= _BV(PB6) | _BV(PB7);
DDRA |= _BV(PA3);
reset_radio();
Radio_Init();
Radio_Configure_Rx(RADIO_PIPE_0, this_station_addr, ENABLE);
Radio_Configure(RADIO_2MBPS, RADIO_HIGHEST_POWER);
uart1_init(UART_57600);
Roomba_Init();
sei();
while(1)
{
while (rxflag){
if (Radio_Receive(&packet) != RADIO_RX_MORE_PACKETS){
rxflag = 0;
}
previous_speed = current_speed;
current_speed = read_speed(packet.payload.message.messagecontent);
previous_radius = current_radius;
current_radius = read_radius(packet.payload.message.messagecontent);
Roomba_Drive(current_speed, current_radius);
}
}
}
Listing 1:Embedded AVR code for the satellite stations. A full-blown RTOS is not
necessary.