Its been a while since I played with my Baby-O boards and have been inspired to get the balancing robot thats been sitting in a box for years rolling arround on two wheels.
I have managed to get two seperate bits of code running. The first project turned the motors on and sent the encoder counts via an XBee to a home brew hyperterminal. The second part of the code was to use the timer 1 interrupt at 25Hz to control a PID loop for the motor speeds. However this code did not function as intended until I commented out the code relating to the serial communications. The simplified interrupt based PID loop code is shown below. Is anyone able to shed light on what is causing the problems with using the interrupt on timer1 and the serial communications?
Many Thanks
Andy
#include <pololu/orangutan.h>
#include <avr/interrupt.h>
int speed1 = 0, speed2 =0;
int demand = 0;
void wait_for_sending_to_finish()
{
while(!serial_send_buffer_empty());
}
ISR(TIMER1_COMPA_vect)
{
//PID Loop
set_motors(speed1,speed2);
}
void process_received_byte(char byte)
{
//demand from Xbee
}
void check_for_new_bytes_received()
{
//receive buffer
}
int main()
{
//serial_set_baud_rate(9600);
// Start receiving bytes in the ring buffer.
//serial_receive_ring(receive_buffer, sizeof(receive_buffer));
encoders_init(16,17,18,19); // the Arduino numbers for ports PC2 - PC5
OCR1A = 0x0C34; //40ms pulses
TCCR1B |= (1 << WGM12);
// Mode 4, CTC on OCR1A
TIMSK1 |= (1 << OCIE1A);
//Set interrupt on compare match
TCCR1B |= (1 << CS12);
// set prescaler to 256 and starts the timer
sei();
// enable interrupts
while(1)
{
// Deal with any new bytes received.
//check_for_new_bytes_received(); //from serial
for (int i = -30 ; i<30 ; i++) //test demand
{
delay_ms(1000);
demand = i*2;
}
}
}