// 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 and servo control code:
Use a low-pass filter on the sound angle estimates before updating the servo. This will smooth out outliers and noise. A simple moving average filter would work.
Add hysteresis to the servo position updates. Only update the servo if the new angle estimate deviates significantly from the current position. This prevents rapid oscillating.
Impose limits on the servo position to keep it in a valid range.
Track the servo position separately from the angle estimate. The servo may not move to the exact commanded position due to mechanical slop.
Implement a Kalman filter to optimally fuse the noisy sound angle measurements over time. This can give a better position estimate than basic filtering.
Instead of updating the servo position continuously, you could use a control loop architecture. In the loop:
Read sound angle
Estimate current servo position
Calculate error between desired angle and current position
Compute a new control signal to drive the error to zero
Command the servo to that new control signal
For even better performance, implement a PID control loop. The PID gains would need to be tuned to get good response without overshoot.
Look at the variation/jitter in the angle estimates. If it is too high, you may want to increase the microphone spacing to get better resolution.
Add some debug prints or LED indicators to monitor the system state, e.g. servo position, filtered angle, etc. This can help tune performance.
The key things are to filter the measurements, add hysteresis/limits to avoid useless updates, and consider a formal control loop structure. That should give better localization accuracy and servo pointing stability.