50.
else
51.
set_motors(max_speed, max_speed-power_difference);
52.
}
53.
// A global ring buffer for data coming in.
54.
// read_next_byte() and previous_byte() functions, below.
55.
char buffer[100];
56.
// A pointer to where we are reading from.
57.
unsigned char read_index = 0;
58.
// Waits for the next byte and returns it.
59.
// the music playing and serial_check to keep receiving bytes.
60.
// Calls pid_check() to keep following the line.
61.
char read_next_byte()
62.
{
63.
while(serial_get_received_bytes() == read_index)
64.
{
65.
serial_check();
66.
play_check();
67.
// pid_check takes some time; only run it if we don't have more bytes to process
68.
if(serial_get_received_bytes() == read_index)
69.
pid_check();
70.
}
71.
char ret = buffer[read_index];
72.
read_index ++;
73.
if(read_index >= 100)
74.
read_index = 0;
75.
return ret;
76.
}
77.
// Backs up by one byte in the ring buffer.
78.
void previous_byte()
79.
{
80.
read_index --;
81.
if(read_index == 255)
82.
read_index = 99;
83.
}
84.
// Returns true if and only if the byte is a command byte (>= 0x80).
85.
char is_command(char byte)
86.
{
87.
if (byte < 0)
88.
return 1;
89.
return 0;
90.
}
91.
// Returns true if and only if the byte is a data byte (< 0x80).
92.
char is_data(char byte)
93.
{
94.
if (byte < 0)
95.
return 0;
96.
return 1;
97.
}
98.
// If it's not a data byte, beeps, backs up one, and returns true.
99.
char check_data_byte(char byte)
100.
{
101.
if(is_data(byte))
102.
return 0;
103.
play("o3c");
104.
clear();
105.
print("Bad data");
106.
lcd_goto_xy(0,1);
107.
print_hex_byte(byte);
108.
previous_byte();
109.
return 1;
110.
}
1.
/////////////////////////////////////////////////////////////////////
2.
// COMMAND FUNCTIONS
3.
//
4.
// Each function in this section corresponds to a single serial
5.
// command.
6.
// handling using read_next_byte() and check_data_byte().
7.
// Sends the version of the slave code that is running.
8.
// This function also shuts down the motors and disables PID, so it is
9.
// useful as an initial command.
10.
void send_signature()
11.
{
12.
serial_send_blocking("3pi1.0", 6);
13.
set_motors(0,0);
14.
pid_enabled = 0;
15.
}
16.
// Reads the line sensors and sends their values.
17.
// do either calibrated or uncalibrated readings.
18.
// it only performs a new reading if we are not in PID mode.
The functions are expected to do their own argument
This is used by the
Runs play_check to keep
This function can
When doing calibrated readings,
Otherwise, it sends