diff options
author | Christian Pointner <equinox@spreadspace.org> | 2013-09-23 18:53:26 +0000 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2013-09-23 18:53:26 +0000 |
commit | c7ec79e08096ca81b690de154adab9c25a78e47a (patch) | |
tree | b6af57ff64c11906592c97a033b72e89d878d8e2 | |
parent | fixed \n\r to \r\n (diff) |
fixed stepper stop
fixed limits
git-svn-id: https://svn.spreadspace.org/avr/trunk@228 aa12f405-d877-488e-9caf-2d797e2a1cc7
-rw-r--r-- | tuer-rfid/limits.c | 32 | ||||
-rw-r--r-- | tuer-rfid/limits.h | 1 | ||||
-rw-r--r-- | tuer-rfid/stepper.c | 25 | ||||
-rw-r--r-- | tuer-rfid/tuer-rfid.c | 1 |
4 files changed, 34 insertions, 25 deletions
diff --git a/tuer-rfid/limits.c b/tuer-rfid/limits.c index 6f567af..4e14b05 100644 --- a/tuer-rfid/limits.c +++ b/tuer-rfid/limits.c @@ -25,40 +25,48 @@ #include "limits.h" #include <LUFA/Drivers/Peripheral/ADC.h> +#include <stdio.h> + #define LIMITS_ADC_CHAN_NUM 8 #define LIMITS_ADC_CHAN ADC_CHANNEL8 #define LIMITS_RINGBUF_SIZE 5 /* HINT: this is compared to a sliding sum not an average! */ -#define LIMITS_TH_CLOSE 250 -#define LIMITS_TH_OPEN 1000 +#define LIMITS_TH_CLOSE 500 +#define LIMITS_TH_OPEN 3200 void limits_init(void) { ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128); ADC_SetupChannel(LIMITS_ADC_CHAN_NUM); - ADC_StartReading(ADC_REFERENCE_INT2560MV | ADC_LEFT_ADJUSTED | LIMITS_ADC_CHAN); + ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | LIMITS_ADC_CHAN); } -limits_t limits_get(void) +static uint16_t sum = 0; + +void limits_task(void) { - static uint8_t s = 0; - static uint8_t r[LIMITS_RINGBUF_SIZE] = { 0 }; + static uint16_t r[LIMITS_RINGBUF_SIZE] = { 0 }; static uint8_t idx = 0; if(ADC_IsReadingComplete()) { - r[idx] = (ADC_GetResult()>>8); + r[idx] = ADC_GetResult(); idx = (idx + 1) % LIMITS_RINGBUF_SIZE; - s = 0; + cli(); + sum = 0; uint8_t i; - for(i=0; i<LIMITS_RINGBUF_SIZE; ++i) s += r[i]; + for(i=0; i<LIMITS_RINGBUF_SIZE; ++i) sum += r[i]; + sei(); } +} - if(s < LIMITS_TH_CLOSE) +limits_t limits_get(void) +{ + if(sum < LIMITS_TH_CLOSE) return close; - if(s < LIMITS_TH_OPEN) - return moving; + if(sum < LIMITS_TH_OPEN) + return moving; return open; } diff --git a/tuer-rfid/limits.h b/tuer-rfid/limits.h index f1363ab..3b5eaa3 100644 --- a/tuer-rfid/limits.h +++ b/tuer-rfid/limits.h @@ -27,6 +27,7 @@ typedef enum { moving, open, close, both } limits_t; void limits_init(void); +void limits_task(void); limits_t limits_get(void); const char* limits_to_string(limits_t limits); diff --git a/tuer-rfid/stepper.c b/tuer-rfid/stepper.c index f267645..5ee5943 100644 --- a/tuer-rfid/stepper.c +++ b/tuer-rfid/stepper.c @@ -56,7 +56,7 @@ uint8_t step_table [] = #define STEPPER_OUTPUT_BITMASK (~(0xF << STEPPER_FIRST_BIT )) volatile uint16_t step_cnt = 0; -#define STEP_CNT_STOP (LENGTH_STEP_TABLE*210) +#define STEP_CNT_STOP (LENGTH_STEP_TABLE*172) #define STEP_CNT_OFF (STEP_CNT_STOP + 100) stepper_direction_t step_direction = dir_open; @@ -65,6 +65,11 @@ inline void stepper_stop(void) STEPPER_PORT &= ~(0xF << STEPPER_FIRST_BIT | 1<<STEPPER_ENABLE_A_BIT | 1<<STEPPER_ENABLE_B_BIT); TCCR1B = 0; // no clock source TIMSK1 = 0; // disable timer interrupt + + if(step_direction == dir_open) + eventqueue_push(open_fin); + else + eventqueue_push(close_fin); } static inline uint8_t stepper_handle(void) @@ -75,12 +80,12 @@ static inline uint8_t stepper_handle(void) stepper_output <<= STEPPER_FIRST_BIT; STEPPER_PORT = (STEPPER_PORT & STEPPER_OUTPUT_BITMASK ) | stepper_output; - limits_t l = limits_get(); - if((step_direction == dir_open && l == open) || - (step_direction == dir_close && l == close) || l == both) - step_cnt = STEP_CNT_STOP + 1; - if(step_cnt < STEP_CNT_STOP) { + limits_t l = limits_get(); + if((step_direction == dir_open && l == open) || + (step_direction == dir_close && l == close) || l == both) + step_cnt = STEP_CNT_STOP + 1; + step_idx += (step_direction == dir_open) ? 1 : -1; step_idx %= LENGTH_STEP_TABLE; } else if(step_cnt == STEP_CNT_STOP) { @@ -89,14 +94,8 @@ static inline uint8_t stepper_handle(void) } step_cnt++; - if(step_cnt >= STEP_CNT_OFF) { - if(step_direction == dir_open) - eventqueue_push(open_fin); - else - eventqueue_push(close_fin); - + if(step_cnt >= STEP_CNT_OFF) return 0; - } return 1; } diff --git a/tuer-rfid/tuer-rfid.c b/tuer-rfid/tuer-rfid.c index 32cf5c7..60d2974 100644 --- a/tuer-rfid/tuer-rfid.c +++ b/tuer-rfid/tuer-rfid.c @@ -141,6 +141,7 @@ int main(void) anyio_task(); manual_task(); ajar_task(); + limits_task(); int16_t bytes_received = anyio_bytes_received(); if(bytes_received > 0) |