#include "trtSettings.h"
#include "trtkernel_1284.c"
#include <stdio.h>
#include <avr/sleep.h>
#include <util/delay.h>
// serial communication library
// Don't mess with the semaphores
#define SEM_RX_ISR_SIGNAL 1
#define SEM_STRING_DONE 2 // user hit
#include "trtUart.h"
#include "trtUart.c"
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
#include <string.h>
#include <util/delay.h> // needed for lcd_lib
void initialize(void);
void servo(float degree);
// 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;
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;
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;
Assistant
Here are a few things you could try to improve your code:
Add comments to explain the overall logic and flow of the program. Comments will help anyone reading the code understand what it is doing.
Break the code up into smaller, more modular functions. For example, you could extract the sound calculation logic into its own function rather than having it all in the task function. This makes the code more readable and maintainable.
Add some error checking and validation of inputs/outputs. For example, check that the degree values are within valid ranges before setting the servo position.
Consider using more descriptive variable names. Names like "dt1", "cmpMatch" don't clearly convey what they represent.
Add logging/debug statements that can be enabled with a flag to help debug issues. Printing values at certain points can help validate calculations and trace bugs.
User
port as much of the code to python as you can. we are using an esp-wroom-32 instead of the weak atmega chip originally used
Assistant
Here is one way to port the key parts of the code to Python for an ESP32:
// --- 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();
//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);