// 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);
It seems like you have a program that includes two tasks, sound_calculation and servo_control, which are responsible for calculating the vector and controlling the servo motor, respectively. The program also includes initialization and some setup for the hardware components such as timers, interrupts, and semaphores. The initialize function is responsible for resetting the variables, setting up the servo motor, external interrupts, and sound timer. The sound_calculation task calculates the vector based on the sound received from the microphones and updates the servo motor position accordingly. The servo_control task updates the servo motor position based on the calculated vector. The program then enters an infinite loop where it sleeps, allowing other tasks to run periodically.
In the sound_calculation task, the vector is calculated based on the sound received from the microphones. The sound_vector variable is updated whenever a new sound measurement is taken. Then, the average degree and the number of samples are updated accordingly. If the difference between the sound_vector and the average degree is within a certain window, the servo motor position is updated. If the difference is outside the window, the servo motor position is not updated.
In the servo_control task, the servo motor position is updated based on the calculated vector. The vector is divided by 2 to get the midpoint of the range, and the cmpMatch variable is used to compare the calculated vector with the predefined range of servo positions. If the cmpMatch value falls within the range of valid servo positions, the OCR0A value is set to the cmpMatch value, which updates the servo motor position. If the cmpMatch value is outside the range of valid servo positions, the servo motor position remains unchanged.
The initialize function sets up the necessary hardware components and initializes the variables. The servo_min and servo_max variables are used to define the range of valid servo positions, while the servo_min_ticks and servo_max_ticks variables are used to define the range of valid servo positions in terms of timer comparisons. The servo function is used to set the servo motor position, and it takes a float value representing the degree of rotation.