// UART file descriptor c
// putchar and getchar are in uart.c
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
// semaphore to protect shared variable
#define SEM_SHARED 3
int args[3] ;
volatile char timer0_overflows;
volatile char mic_state;
volatile char mic_order [3];
volatile unsigned int sound_time1;
volatile unsigned int sound_time2;
volatile char vector_update_flag;
char servo_update_flag;
float sound_vector;
int servo_min_ticks;
int servo_max_ticks;
#define servo_min 0.535 //period in ms
#define servo_max 2.425 //period in ms
#define speed_sound 34029.0 //speed in cm/sec
#define mic_distance 14.0 //distance in cm
int cur_deg;
int avg_deg;
int num_samples;
int next_deg;
//MICROPHONE #1 - B0
ISR (PCINT1_vect) {
if (!(PINB & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 1; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 1; //update mic order
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 1; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 1 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
}
//MICROPHONE #2 - C0
ISR (PCINT2_vect) {
if (!(PINC & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 2; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 2; //update mic order
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 2; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 2 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
}
//MICROPHONE #3 - D0
ISR (PCINT3_vect) {
if (!(PIND & 0x01)) {
switch (mic_state) {
case 0:
TCNT2= 0; //restart timer
mic_state= 1; //update state
mic_order[0]= 3; //update mic order
break;
code
case 1:
sound_time1= TCNT2; //save timer value1
mic_state= 2; //update state
mic_order[1]= 3; //update mic order;
break;
case 2:
sound_time2= TCNT2; //save timer value2
mic_state= 3;
mic_order[2]= 3; //update mic order
vector_update_flag=1; //UPDATE VECTOR!!
PORTD &= ~(1<<PIND2);
break;
default:
mic_state= -1;
break;
}
}
else { //Mic 3 off => reset
PORTD |= (1<<PIND2);
mic_state= 0;
}
// --- TASK2: Servo Control ----------------------------------------
void servo_control(void* args) {
uint32_t rel, dead ;
int deg_window = 15;
int update_window = 7;
while (1) {
trtWait(SEM_SHARED);
if (servo_update_flag == 1){ //flag to update servo position
servo_update_flag = 0;
//sleep, run at 50Hz
rel = trtCurrentTime() + SECONDS2TICKS(0.02);
dead = trtCurrentTime() + SECONDS2TICKS(0.02);
trtSleepUntil(rel, dead);
}
}
// --- MAIN -----------------------------------------------------------
int main(void) {
initialize();
code
//init the UART -- trt_uart_init() is in trtUart.c
trt_uart_init();
stdout = stdin = stderr = &uart_str;
fprintf(stdout,"\n\r TRT 9feb2009\n\r\n\r");
// start TRT
trtInitKernel(80); // 80 bytes for the idle task stack
// --- create semaphores ----------
// You must creat the first two semaphores if you use the uart
trtCreateSemaphore(SEM_RX_ISR_SIGNAL, 0) ; // uart receive ISR semaphore
trtCreateSemaphore(SEM_STRING_DONE,0) ; // user typed <enter>
// variable protection
trtCreateSemaphore(SEM_SHARED, 1) ; // protect shared variables
// --- creat tasks ----------------
trtCreateTask(sound_calculation, 400, SECONDS2TICKS(0.1), SECONDS2TICKS(0.25), &(args[0]));
trtCreateTask(servo_control, 200, SECONDS2TICKS(0.1), SECONDS2TICKS(0.1), &(args[1]));
// Idle task
// just sleeps the cpu to save power
// every time it executes
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
while (1) {
sleep_cpu();
}
}
// --- SERVO CONTROL -------------------------------------------
void servo(float degree) {
int cmpMatch;
degree= degree/2.0;
// fprintf(stdout,"/n/r %i \n\r",(int)degree);
Here are a few suggestions to improve the sound localization code:
Use better variable names like sound_angle instead of just theta. More descriptive names make the code easier to understand.
Check for divide by zero errors when calculating the sound angle. Add a small epsilon value to the denominator to avoid crashes.
Reduce duplicated code by creating helper functions, for example to calculate the angle based on different microphone orders.
Add more comments explaining the key steps - calculating time differences, determining sound direction, choosing microphone order comparisons.
Print debug messages to check the calculated angle and microphone order before updating the servo. This will help catch issues early.
Limit the angle value between 0-360 degrees. Add bounds checking after calculation to prevent invalid angles.
Average over a window of angles to smooth out outliers before sending to the servo.
Tune deg_window and update_window values in the servo task to balance between responsiveness and stability.
Add state machine or other improvements to handle complex sound environments and multiple overlapping sounds.
Consider using sensor fusion with additional inputs like a camera for more robust sound localization.
The key goals should be to make the code robust and fail-safe, while maintaining good real-time performance for servo control. Let me know if any part needs more clarification!