/****************************************************
RADIO.C is a collection of basic network protocol functions needed to send
simple messages between 'bots.
*****************************************************/
/* Global variables */
int transmitting = 0; /* is the bot transmitting? */
int recieving = 0; /* is the bot recieving? */
int buffer = 0; /* location of recieved byte */
int in_port = 4; /* port used to listen for radio recieving */
int motor_port = 3; /* motor port used */
int high_thresh = 30; /* threshold for a "high" bit */
long high_delay = 100L; /* time for a high bit */
long low_delay = 60L; /* time for a low bit */
long start_delay = 150L; /* time to indicate a new packet */
long delay = 20L; /* time between packets */
long leeway = 15L; /* allowed leeway */
#define BYTESIZE 8 /* size of sent byte (can easily change to 16 or > */
#define value() (analog(in_port)) /* quick macro */
/****************************************************/
/*************************************************/
/* initialize the necessary radio ports */
int init_radio (int port, int high)
{
in_port = port;
high_thresh = high;
}
/****************************************************/
/* transmit() turns a number into a sequence of bits, and sends them */
int transmit(int number)
{
int bit[BYTESIZE];
int i;
set_ir_transmit_frequency(1000);
/* turn the number into bits */
itob(number, bit);
printf("\n%d: ", number); /* some debugging stuff */
printbits(bit);
/* key the microphone (start transmitting) */
key_mic();
/* send packet header */
start_packet();
/* send stream of bits (least significant first) */
for (i=0; i < BYTESIZE; i++) {
send_bit(bit[i]);
}
/* send packet footer (necessary?) */
end_packet();
/* end transmitting */
unkey_mic();
}
/****************************************************/
/* monitor_radio() is where the real work is at. */
void monitor_radio()
{
long last_time = mseconds(),
time;
int bits[BYTESIZE],
count = 0,
done = 0;
set_beeper_pitch(500.0);
while (1) {
if (value() > high_thresh) {
last_time = mseconds();
beeper_on();
while (value() > high_thresh);
time = mseconds() - last_time;
beeper_off();
if ((time > (start_delay - leeway)) &&
(time < (start_delay + leeway))) {
/* then it's a start of a new packet */
recieving = 1;
reset_bits(bits);
count = 0;
done = 0;
} else if ((time > (high_delay - leeway)) &&
(time < (high_delay + leeway)) &&
recieving) {
/* then its a 1 */
bits[count] = 1;
count++;
printf("1");
} else if ((time > (low_delay - leeway)) &&
(time < (low_delay + leeway)) &&
recieving) {
/* then its a zero */
bits[count] = 0;
count++;
printf("0");
} /* otherwise, it's just noise */
}
if (((mseconds() - last_time) >= 200L) &&
(!(done)) || (count > BYTESIZE-1)) {
done = 1;
recieving = 0;
buffer = btoi(bits);
count = 0;
printf(" : %d\n", buffer);
}
}
}
void key_mic()
{
motor(motor_port, 100);
}
void unkey_mic()
{
motor(motor_port, 0);
}
void start_packet()
{
ir_transmit_on();
msleep(start_delay);
ir_transmit_off();
msleep(delay);
}
void end_packet()
{
/* if we count bits, do we really need a packet footer? */
}
void send_bit(int bit)
{
if(bit == 1) {
ir_transmit_on();
msleep(high_delay);
ir_transmit_off();
} else if (bit == 0) {
ir_transmit_on();
msleep(low_delay);
ir_transmit_off();
}
msleep(delay);
}
void printbits(int bits[])
{
int i;
for (i=0; i < BYTESIZE; i++) {
printf("%d", bits[i]);
}
}
void reset_bits(int bits[])
{
int i = 0;
for (i=0; i < BYTESIZE; i++) {
bits[i] = 0;
}
}
/*** Handy Math Routines ************************************/
int itob(int number, int bits[])
{
int temp = number;
int i;
for (i=0; i < BYTESIZE; i++) {
bits[i] = temp % 2;
temp = temp / 2;
}
}
int btoi(int bits[])
{
int temp = 0,
i;
for (i=0; i < BYTESIZE; i++) {
if (bits[i] == 1)
temp += power(2, i);
}
return temp;
}
int power(int a, int b)
{
int i, temp = a;
if (b == 0)
temp = 1;
else
for (i=1; i < b; i++) {
temp = a * temp;
}
return temp;
}
|