Skip to content
Snippets Groups Projects
Commit 2442e433 authored by Michael DM Dryden's avatar Michael DM Dryden
Browse files

Added position tracking and homing code

parent 043ee0ec
No related merge requests found
......@@ -12,12 +12,15 @@
#include <avr/interrupt.h>
//Internal global variables
bool current_dir = 1;
uint16_t step_counter = 0;
volatile bool current_dir = 1;
volatile uint16_t step_counter = 0;
int32_t z_position = 0;
//Internal function declarations
void single_step(void);
void multi_step(bool dir, uint16_t steps, uint16_t steps_per_s);
uint16_t multi_step(bool dir, uint16_t steps, uint16_t steps_per_s);
//uint16_t mm_step(double mm, double mm_per_s, bool dir);
bool home(void);
void single_step(void){
static bool last_dir = 1;
......@@ -28,6 +31,13 @@ void single_step(void){
last_dir = current_dir;
}
if (current_dir) {
++z_position;
}
else {
--z_position;
}
ioport_set_pin_level(PIN_STEP, 1);
delay_us(10);
ioport_set_pin_level(PIN_STEP, 0);
......@@ -36,7 +46,7 @@ void single_step(void){
return;
}
void multi_step(bool dir, uint16_t steps, uint16_t us_per_step){
uint16_t multi_step(bool dir, uint16_t steps, uint16_t us_per_step){
current_dir = dir;
step_counter = 0;
......@@ -50,14 +60,38 @@ void multi_step(bool dir, uint16_t steps, uint16_t us_per_step){
TCNT1 = 0;
TCCR1B |= TC1_DIV; // Start Timer
while (step_counter < steps); // wait until all steps done
while (step_counter < steps){ // wait until all steps done
if (ioport_get_pin_level(PIN_ZMIN) == 0) {
TIMSK1 &= ~(1 << OCIE1A);
TCCR1B &= ~(0b00000111);
return step_counter;
}
}
// stop timer
TIMSK1 &= ~(1 << OCIE1A);
TCCR1B &= ~(0b00000111);
return 0;
}
//uint16_t mm_step(double mm, double mm_per_s, bool dir){
// multi_step(dir, (uint16_t)mm, (uint16_t)mm_per_s); // mm/steps_per_mm, mm_per_s -> us_per_step
//}
//
//void home(void){
// while (multi_step(0, 1000, )) {
// <#statements#>
// }
//}
bool home(void){
while (multi_step(0, 1600, 400) == 0);
z_position = 0;
return 1;
}
int main(void){
board_init();
......@@ -65,10 +99,10 @@ int main(void){
cpu_irq_enable(); // enable interrupts
delay_ms(500);
while (1) {
multi_step(1, 40, 2000);
multi_step(0, 40, 2000);
home();
delay_s(10);
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment