bikegenerator/schaltungen/displayboard_servo/software/src/main.c

205 lines
4.1 KiB
C

#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <string.h>
#include "utils.h"
#include "main.h"
#include "uart.h"
#define POWER_MIN 0
#define POWER_MAX 400
#define SERVO_STEPS 125
#define BUFSIZE 32
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;
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
}
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);
if (data_in[data_count] == 'B') {
process_command(data_in);
reset_input_buffer();
}
data_count++;
if(data_count >= BUFSIZE) { // buffer overflow
reset_input_buffer();
}
}
}
/**
* \brief set the servo to a position calculated to given power
*
* \param display The power value from 0 to 400 (including bounds)
*/
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 = display * 10; // *10 otherwise we need float
display = display / ( (POWER_MAX*10) / SERVO_STEPS );
display = display + SERVO_STEPS;
uart_puts_P("display = ");
uart_print_uint16(display);
uart_puts_P("\r\n");
//cli(); // read and write atomic
TIMSK &= ~_BV(OCIE1A);
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);
}
/**
* \brief the method moves the servo one complete cycle
*/
void demo_display(void) {
set_servo(0);
wait(100);
set_servo(400);
wait(100);
set_servo(0);
wait(100);
}
int main(void) {
sei();
ports_init();
timer_init();
uart_init(UART_BAUD_SELECT(38400,F_CPU));
reset_input_buffer();
demo_display();
while(1) {
work_uart();
if((syscounter % 25) == 0) { // about every 500ms
reset_input_buffer();
uart_putc('a'); // send a to receive values from master box
}
syscounter %= 1000;
}
return(0);
}
/**
* \brief this is our timer for PWM generation and system clock
* 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
}