changed the serial protocol and cleaned up the code, fixed a bug in the timer (PWM got inverted sometimes)

This commit is contained in:
Lucas Pleß 2014-06-03 16:29:55 +02:00
parent 37031ed445
commit c66c8a0def
3 changed files with 97 additions and 114 deletions

View File

@ -12,111 +12,75 @@
#define POWER_MAX 400
#define SERVO_STEPS 125
#define BUFSIZE 32
#define BUFSIZE 10
volatile uint16_t syscounter = 0;
// values send over uart from powerboard
typedef struct {
uint16_t voltage_gen;
uint16_t voltage_reg;
uint16_t current_gen;
struct {
uint8_t loadsw : 1;
uint8_t gensw : 1;
uint8_t batsw : 1;
};
} PWR_DATA;
static PWR_DATA pd;
volatile uint16_t power;
uint8_t data_count = 0;
char data_in[BUFSIZE];
void reset_input_buffer(void) {
data_count = 0;
memset(data_in, 0, BUFSIZE);
}
static void update_servo_position() {
uint32_t power;
power = ( (pd.voltage_gen / 100) * (pd.current_gen / 100) ) / 100;
set_servo(power & 0xffff);
}
static void timer_init(void) {
// CTC Mode for Timer 1 (16Bit) with prescale of 64
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10);
OCR1A = 2250; // set Servo to max. position
TIMSK |= _BV(OCIE1A); // enable timer interrupt
TCCR1A |= (1<<COM1A0); // toggle OCR1A on overflow
TCCR1A |= _BV(COM1A0); // toggle OCR1A on overflow
// CTC Mode for Timer 0 (8Bit) with prescale of 1024
TCCR0B |= _BV(CS02) | _BV(CS00); // prescaler
TCCR0A |= _BV(WGM01); // CTC mode
TIMSK |= _BV(OCIE0A); // enable timer interrupt
OCR0A = 78; // gives us ~100ms interval
}
static void ports_init(void) {
DDRB |= _BV(PB3);
}
static void process_command(char *command) {
if(command[0] == 'A') {
// command must be in the following format:
// A$voltage_gen,$voltage_reg,$current_gen,loadsw,batsw,gensw\n
// examples:
// A24500,13400,12000,1,1,1B
// A19900,11000,15000,1,1,1B
// A20000,12300,10000,1,1,1B
char *token;
uint8_t tokencounter = 0;
token = strtok(command, ",");
while( token ) {
switch(tokencounter) {
case 0:
// skip the A in front of the number
pd.voltage_gen = atoi(++token);
break;
case 1:
pd.voltage_reg = atoi(token);
break;
case 2:
pd.current_gen = atoi(token);
break;
case 3:
if(atoi(token) == 1) pd.loadsw = 1;
else pd.loadsw = 0;
break;
case 4:
if(atoi(token) == 1) pd.batsw = 1;
else pd.batsw = 0;
break;
case 5:
if(atoi(token) == 1) pd.gensw = 1;
else pd.gensw = 0;
break;
}
tokencounter++;
token = strtok(NULL, ",");
}
update_servo_position();
}
}
static void work_uart() {
uint16_t c = uart_getc();
if ( !(c & UART_NO_DATA) ) {
data_in[data_count] = (c & 0xff);
char cur = c & 0xff;
if (data_in[data_count] == 'B') {
process_command(data_in);
data_in[data_count] = cur;
//uart_print_uint8(cur);
data_count++;
if(data_count >= BUFSIZE) { // buffer overflow
reset_input_buffer();
}
data_count++;
if(data_count >= BUFSIZE) { // buffer overflow
#ifdef DEBUG
for(uint8_t i=0;i<BUFSIZE;i++) {
uart_print_uint8(data_in[i]);
uart_putc('\r');
uart_putc('\n');
}
uart_puts_P(" -- \r\n");
#endif
if (cur == '\n' && data_count > 1) { // \n
power = atol(data_in);
if(power > POWER_MAX) power = POWER_MAX;
#ifdef DEBUG
uart_puts_P("power = ");
uart_print_uint16(power);
uart_puts_P("\r\n");
#endif
set_servo(power);
reset_input_buffer();
}
}
@ -129,34 +93,40 @@ static void work_uart() {
*/
void set_servo(uint16_t display) {
if( display < 1 ) display = POWER_MIN;
if( display > POWER_MAX ) display = POWER_MAX;
display = POWER_MAX-display; // invert the value / servo
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 / ((POWER_MAX * 10) / SERVO_STEPS);
display = display + SERVO_STEPS;
#ifdef DEBUG
uart_puts_P("display = ");
uart_print_uint16(display);
uart_puts_P("\r\n");
#endif
//cli(); // read and write atomic
TIMSK &= ~_BV(OCIE1A);
if( display < 125 ) display = 125; // just make sure, the timer
if( display < 125 ) display = 125; // just make sure, the timer
if( display > 250 ) display = 250; // is never out ouf 20ms grid
OCR1A = 2500-display;
TIMSK |= _BV(OCIE1A);
// check if timer is currently in the small pulse, then sleep here 2ms
// and do again
if(OCR1A < 2250) {
_delay_ms(2);
}
cli(); // read and write atomic
OCR1A = 2500-display;
sei();
}
/**
* \brief the method moves the servo one complete cycle
*/
void demo_display(void) {
static void demo_display(void) {
set_servo(0);
wait(100);
set_servo(400);
@ -179,12 +149,17 @@ int main(void) {
while(1) {
work_uart();
if((syscounter % 25) == 0) { // about every 500ms
if(syscounter >= 100) {
reset_input_buffer();
uart_putc('a'); // send a to receive values from master box
}
syscounter = 0;
syscounter %= 1000;
#ifdef DEBUG
uart_puts_P("OCR1A = ");
uart_print_uint16(OCR1A);
uart_puts_P("\r\n");
#endif
}
}
return(0);
@ -195,10 +170,11 @@ int main(void) {
* the system clock varies a bit, but this does not matter
*/
ISR(TIMER1_COMPA_vect) {
syscounter++;
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) {
syscounter++;
}

View File

@ -47,5 +47,10 @@ void uart_print_uint16(uint16_t x) {
uart_putc(48 + (x % 100 / 10 ));
uart_putc(48 + (x % 10));
}
void uart_print_uint8(uint8_t x) {
uart_putc(48 + (x % 1000 / 100 ));
uart_putc(48 + (x % 100 / 10 ));
uart_putc(48 + (x % 10));
}

View File

@ -6,30 +6,32 @@ import time
ser = serial.Serial("/dev/ttyUSB0", 38400)
voltage = 20000
current = 0
power = 0
incdec = 0
while 1:
tdata = ser.read()
time.sleep(1)
data_left = ser.inWaiting()
tdata += ser.read(data_left)
out = "A%05d,12300,%05d,1,1,1B" % (voltage, current)
print out
ser.write(out)
if tdata == 'a':
print power
ser.write(str(power) + '\n')
#ser.write('100\n')
ser.flush()
if incdec == 0:
current = current + 1000
if incdec == 0:
power = power + 10
else:
power = power - 10
if power > 400:
power = 400
incdec = 1
if power < 0:
power = 0
incdec = 0
else:
current = current - 1000
if current > 20000:
current = 20000
incdec = 1
if current < 0:
current = 0
incdec = 0
data_left = ser.inWaiting()
tdata += ser.read(data_left)
print tdata