added comments, removed debug code, fixed power calculation

This commit is contained in:
Lucas Pleß 2014-06-02 23:54:39 +02:00
parent 21be4e1626
commit 62d1a3bf63
1 changed files with 44 additions and 51 deletions

View File

@ -8,6 +8,8 @@
#include "main.h"
#include "uart.h"
#define POWER_MIN 0
#define POWER_MAX 400
#define BUFSIZE 32
@ -27,16 +29,18 @@ typedef struct {
} PWR_DATA;
static PWR_DATA pd;
uint8_t data_count = 0;
char data_in[BUFSIZE];
extern unsigned char __heap_start;
void reset_input_buffer(void) {
data_count = 0;
memset(data_in, 0, BUFSIZE);
}
static void timer_init(void) {
// clock is 8MHz
TCCR1B |= _BV(WGM12) | _BV(CS11) | _BV(CS10); // CTC Mode for Timer 1 (16Bit) with prescale of 64
OCR1A = 2312; // Neutralposition ((2500-2312)*0.008ms)=1,5ms)
OCR1A = 2250; // Neutralposition ((2500-2312)*0.008ms)=1,5ms)
TIMSK = _BV(OCIE1A);
TCCR1A = (1<<COM1A0); // Togglen bei Compare Match
}
@ -61,12 +65,6 @@ static void process_command(char *command) {
token = strtok(command, ",");
while( token ) {
/*uart_puts_P("\r\ntoken: ");
uart_puts(token);
uart_puts_P("\r\ntokencounter: ");
uart_print_uint16(tokencounter);*/
switch(tokencounter) {
case 0:
pd.voltage_gen = atoi(++token); // skip the A in front of the number
@ -122,30 +120,29 @@ static void work_uart() {
*/
if (data_in[data_count] == 'B') {
//uart_puts_P("got b\r\n");
process_command(data_in);
data_count = 0;
memset(data_in, 0, BUFSIZE);
reset_input_buffer();
}
data_count++;
if(data_count >= BUFSIZE) {
data_count = 0;
memset(data_in, 0, BUFSIZE);
//uart_puts_P("overflow\r\n");
}
reset_input_buffer();
}
}
}
static void set_servo(uint16_t display) {
/**
* \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 = 1;
if( display > 400 ) display = 400;
if( display < 1 ) display = POWER_MIN;
if( display > POWER_MAX ) display = POWER_MAX;
display = POWER_MAX-display; // invert the value / servo
display = display * 10; // shift, since we have to divide by 3,2 (32)
display = display / 32; // instead of dividing by 3,2
@ -159,49 +156,41 @@ static void set_servo(uint16_t display) {
}
static void demo_display(void) {
for(uint16_t i = 0; i<= 40;i++) {
set_servo(i*10);
_delay_ms(50);
}
for(uint16_t i = 40; i> 0;i--) {
set_servo(i*10);
_delay_ms(50);
}
/**
* \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));
memset(data_in, 0, BUFSIZE);
reset_input_buffer();
demo_display();
uint32_t power;
while(1) {
work_uart();
if(syscounter >= 100) {
if(syscounter >= 100) { // about every two seconds
memset(data_in, 0, BUFSIZE);
data_count = 0;
uart_putc('a'); // send a to receive values
//uart_puts_P("RAM="); uart_print_uint16(SP - (uint16_t) &__heap_start); uart_puts_P("\r\n");
uart_putc('a'); // send a to receive values from master box
power = ( (pd.voltage_gen / 100) * (pd.current_gen / 100) ) / 100;
set_servo(power & 0xffff);
set_servo((pd.voltage_gen/1000) * (pd.current_gen/1000));
/*
uart_puts_P("voltage_gen = ");
uart_print_voltage(pd.voltage_gen);
uart_puts_P(" voltage_reg = ");
uart_print_voltage(pd.voltage_reg);
uart_puts_P(" current_gen = ");
uart_print_current(pd.current_gen);
uart_puts_P("\r\n");
*/
syscounter = 0;
}
}
@ -209,7 +198,11 @@ int main(void) {
return(0);
}
SIGNAL(TIMER1_COMPA_vect) {
/**
* \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