switched to fast PWM, added workaround for UART transmission errors
This commit is contained in:
parent
1752f5a0a1
commit
39796a93be
|
@ -14,13 +14,17 @@
|
||||||
|
|
||||||
#define BUFSIZE 10
|
#define BUFSIZE 10
|
||||||
|
|
||||||
|
/* ugly workaround for massive out of sequence power values,
|
||||||
|
* USART code seems to forget characters from time to time */
|
||||||
|
#define MAX_ALLOWED_DEVIATION 50
|
||||||
|
#define MAX_FAILS 3
|
||||||
|
|
||||||
#define DEBUG2
|
#define DEBUG2
|
||||||
|
|
||||||
volatile uint16_t syscounter = 0;
|
volatile uint16_t syscounter = 0;
|
||||||
volatile uint16_t power;
|
|
||||||
uint8_t data_count = 0;
|
uint8_t data_count = 0;
|
||||||
char data_in[BUFSIZE];
|
char data_in[BUFSIZE];
|
||||||
|
volatile uint16_t current_pulse_width = 250;
|
||||||
|
|
||||||
void reset_input_buffer(void) {
|
void reset_input_buffer(void) {
|
||||||
data_count = 0;
|
data_count = 0;
|
||||||
|
@ -28,11 +32,14 @@ void reset_input_buffer(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void timer_init(void) {
|
static void timer_init(void) {
|
||||||
// CTC Mode for Timer 1 (16Bit) with prescale of 64
|
// set Timer 1 to fast PWM mode, with prescale of 64, clear OC1A as soon as
|
||||||
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10);
|
// TCNT1 reaches OCR1A, set OC1A as soon as it reaches ICR1
|
||||||
OCR1A = 2250; // set Servo to max. position
|
// NOTE: one tick is equivalent to 8 usecs
|
||||||
TIMSK |= _BV(OCIE1A); // enable timer interrupt
|
TCCR1A = _BV(COM1A1) | _BV(WGM11);
|
||||||
TCCR1A |= _BV(COM1A0); // toggle OCR1A on overflow
|
TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS11) | _BV(CS10);
|
||||||
|
ICR1 = 2500; // period of 20 msecs
|
||||||
|
OCR1A = 250; // inital pulse width of 2 msecs (10% duty cycle)
|
||||||
|
TIMSK |= _BV(TOIE1); // enable overflow interrupt
|
||||||
|
|
||||||
// CTC Mode for Timer 0 (8Bit) with prescale of 1024
|
// CTC Mode for Timer 0 (8Bit) with prescale of 1024
|
||||||
TCCR0B |= _BV(CS02) | _BV(CS00); // prescaler
|
TCCR0B |= _BV(CS02) | _BV(CS00); // prescaler
|
||||||
|
@ -47,6 +54,9 @@ static void ports_init(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void work_uart() {
|
static void work_uart() {
|
||||||
|
static uint16_t power;
|
||||||
|
static uint8_t fail_counter = 0;
|
||||||
|
uint16_t new_power = 0, diff = 0;
|
||||||
uint16_t c = uart_getc();
|
uint16_t c = uart_getc();
|
||||||
|
|
||||||
if ( !(c & UART_NO_DATA) ) {
|
if ( !(c & UART_NO_DATA) ) {
|
||||||
|
@ -70,19 +80,28 @@ static void work_uart() {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (cur == 13 || cur == '\n') {
|
if (cur == 13 || cur == '\n') {
|
||||||
|
new_power = atoi(data_in);
|
||||||
|
new_power = new_power <= POWER_MAX ? new_power : POWER_MAX;
|
||||||
|
diff = new_power > power ? new_power - power : power - new_power;
|
||||||
|
|
||||||
power = atol(data_in);
|
if(diff < MAX_ALLOWED_DEVIATION || fail_counter > MAX_FAILS) {
|
||||||
if(power > POWER_MAX) power = POWER_MAX;
|
#ifdef UART_DEBUG
|
||||||
|
uart_puts_P("Transmitted power = ");
|
||||||
#ifdef DEBUG
|
|
||||||
uart_puts_P("power = ");
|
|
||||||
uart_print_uint16(power);
|
uart_print_uint16(power);
|
||||||
uart_puts_P("\r\n");
|
uart_puts_P("\r\n");
|
||||||
#endif
|
#endif
|
||||||
if(power > 0) {
|
power = new_power;
|
||||||
|
fail_counter = 0;
|
||||||
set_servo(power);
|
set_servo(power);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
#ifdef UART_DEBUG
|
||||||
|
uart_puts_P("Oooooooops!!! Transmitted power = ");
|
||||||
|
uart_print_uint16(new_power);
|
||||||
|
uart_puts_P("\r\n");
|
||||||
|
#endif
|
||||||
|
fail_counter++;
|
||||||
|
}
|
||||||
reset_input_buffer();
|
reset_input_buffer();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,15 +113,10 @@ static void work_uart() {
|
||||||
* \param display The power value from 0 to 400 (including bounds)
|
* \param display The power value from 0 to 400 (including bounds)
|
||||||
*/
|
*/
|
||||||
void set_servo(uint16_t display) {
|
void set_servo(uint16_t display) {
|
||||||
int diff;
|
// invert value and ensure that we don't exceed the limit
|
||||||
|
display = POWER_MAX - (display < POWER_MAX ? display : POWER_MAX);
|
||||||
if( display > POWER_MAX ) display = POWER_MAX;
|
// *10 otherwise we need float
|
||||||
|
display = (display * 10u) / ((POWER_MAX * 10u) / SERVO_STEPS) + SERVO_STEPS;
|
||||||
display = POWER_MAX-display; // invert the value / servo
|
|
||||||
|
|
||||||
display = display * 10; // *10 otherwise we need float
|
|
||||||
display = display / ((POWER_MAX * 10) / SERVO_STEPS);
|
|
||||||
display = display + SERVO_STEPS;
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
uart_puts_P("display = ");
|
uart_puts_P("display = ");
|
||||||
|
@ -110,37 +124,11 @@ void set_servo(uint16_t display) {
|
||||||
uart_puts_P("\r\n");
|
uart_puts_P("\r\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( display < 125 ) display = 125; // just make sure, the timer
|
cli();
|
||||||
if( display > 250 ) display = 250; // is never out ouf 20ms grid
|
current_pulse_width = display;
|
||||||
|
sei();
|
||||||
|
|
||||||
// check if timer is currently in the small pulse, then sleep here 2ms
|
|
||||||
// and do again
|
|
||||||
|
|
||||||
|
|
||||||
if(OCR1A <= 2250) {
|
|
||||||
_delay_ms(10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//cli(); // read and write atomic
|
|
||||||
TIMSK &= ~(_BV(OCIE1A));
|
|
||||||
diff = OCR1A - (2500-display);
|
|
||||||
if(diff <=20 && diff >= -20) {
|
|
||||||
OCR1A = 2500-display;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(diff <=20) {
|
|
||||||
OCR1A = OCR1A+5;
|
|
||||||
}
|
|
||||||
else if ( diff >= -20 ) {
|
|
||||||
OCR1A = OCR1A-5;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//sei();
|
|
||||||
TIMSK |= _BV(OCIE1A);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief the method moves the servo one complete cycle
|
* \brief the method moves the servo one complete cycle
|
||||||
*/
|
*/
|
||||||
|
@ -153,17 +141,16 @@ static void demo_display(void) {
|
||||||
wait(100);
|
wait(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
|
||||||
sei();
|
sei();
|
||||||
|
|
||||||
ports_init();
|
ports_init();
|
||||||
timer_init();
|
timer_init();
|
||||||
uart_init(UART_BAUD_SELECT(38400,F_CPU));
|
uart_init(UART_BAUD_SELECT(38400,F_CPU));
|
||||||
|
|
||||||
reset_input_buffer();
|
reset_input_buffer();
|
||||||
// demo_display();
|
// demo_display();
|
||||||
|
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
work_uart();
|
work_uart();
|
||||||
|
|
||||||
|
@ -183,14 +170,8 @@ int main(void) {
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
ISR(TIMER1_OVF_vect) {
|
||||||
* \brief this is our timer for PWM generation and system clock
|
OCR1A = current_pulse_width;
|
||||||
* the system clock varies a bit, but this does not matter
|
|
||||||
*/
|
|
||||||
ISR(TIMER1_COMPA_vect) {
|
|
||||||
OCR1A = 2500-OCR1A; // Das Servosignal wird aus der Differenz von
|
|
||||||
// Periodenlänge (2500*0,008ms=20ms) und letztem
|
|
||||||
// Vergleichswert (OCR1A) gebildet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(TIMER0_COMPA_vect) {
|
ISR(TIMER0_COMPA_vect) {
|
||||||
|
|
|
@ -13,7 +13,7 @@ while 1:
|
||||||
tdata = ser.read()
|
tdata = ser.read()
|
||||||
|
|
||||||
if tdata == 'a':
|
if tdata == 'a':
|
||||||
print power
|
print 'Local power = ' + str(power)
|
||||||
ser.write(str(power) + '\x0d')
|
ser.write(str(power) + '\x0d')
|
||||||
#ser.write('100\n')
|
#ser.write('100\n')
|
||||||
ser.flush()
|
ser.flush()
|
||||||
|
@ -30,7 +30,9 @@ while 1:
|
||||||
if power < 0:
|
if power < 0:
|
||||||
power = 0
|
power = 0
|
||||||
incdec = 0
|
incdec = 0
|
||||||
#else:
|
else:
|
||||||
|
sys.stdout.write(tdata)
|
||||||
|
sys.stdout.write(ser.readline())
|
||||||
#data_left = ser.inWaiting()
|
#data_left = ser.inWaiting()
|
||||||
#tdata += ser.read(data_left)
|
#tdata += ser.read(data_left)
|
||||||
#print tdata
|
#print tdata
|
||||||
|
|
Loading…
Reference in New Issue