scoutos / scout_avr / src / stepper.cpp @ 86b48573
History | View | Annotate | Download (2.67 KB)
1 |
extern "C" |
---|---|
2 |
{ |
3 |
#include <avr/io.h> |
4 |
#include <util/delay.h> |
5 |
} |
6 |
#include "stepper.h" |
7 |
|
8 |
/* Stepper Motor:
|
9 |
* Provides interface to stepper motor.
|
10 |
* Can set direction. Outputs pulse to stepper upon call of step func
|
11 |
* Also provides variable speed sweep
|
12 |
*/
|
13 |
|
14 |
struct step_t {
|
15 |
unsigned int speed; // time between steps in ms for sweep. Mininum 40ms |
16 |
unsigned int sweep_ms; //time between calls to sweep |
17 |
unsigned int time; |
18 |
int pos; // position in rotation. |
19 |
int dir; // direction. -1 CCW. 1 CW. 0 OFF |
20 |
int ccw;
|
21 |
int cw;
|
22 |
} volatile step;
|
23 |
|
24 |
|
25 |
func step_init(unsigned int call_ms) |
26 |
{ |
27 |
/* set update time for sweep */
|
28 |
step.sweep_ms = call_ms; |
29 |
|
30 |
/* init pos, time, and dir to 0 */
|
31 |
step.pos = 0;
|
32 |
step.time = 0;
|
33 |
step.dir = 0;
|
34 |
|
35 |
//set control pins as output
|
36 |
DDRD |= ((1<<S_STEP) | (1<<S_DIR)); |
37 |
DDRB |= ((1<<S_MS));
|
38 |
|
39 |
//initiate to full steps
|
40 |
PORTB &= (~(1<<S_MS));
|
41 |
|
42 |
//initiate the step pin to be low. stepper steps on low to high
|
43 |
PORTD &= (~(1<<S_STEP));
|
44 |
|
45 |
//return function pointer to sweep to be called ever sweep_us uS.
|
46 |
return &(step_sweep);
|
47 |
} |
48 |
|
49 |
/* set direction pin */
|
50 |
void step_dir(int dir) |
51 |
{ |
52 |
step.dir = dir; |
53 |
switch(dir)
|
54 |
{ |
55 |
case 1: |
56 |
PORTD |= (1<<S_DIR);
|
57 |
break;
|
58 |
case -1: |
59 |
PORTD &= (~(1<<S_DIR));
|
60 |
break;
|
61 |
} |
62 |
} |
63 |
|
64 |
void step_halfstep()
|
65 |
{ |
66 |
if(step.dir==0) return; //do not step if not enabled |
67 |
PORTB |= (1<<S_MS); //enable microstepping |
68 |
__asm__ __volatile__( |
69 |
"nop\n\t"
|
70 |
"nop\n\t"
|
71 |
"nop\n\t"
|
72 |
"nop\n\t"
|
73 |
::);//wait 250 ns for microstepping to enable
|
74 |
PORTD |= (1<<S_STEP); //step once |
75 |
_delay_us(1); //conform with step timing |
76 |
PORTD &= (~(1<<S_STEP)); //bring the step bin back down |
77 |
PORTB &= (~(1<<S_MS)); //disable microstepping |
78 |
if(step.dir==1) step.pos++; //keep track of pos. 1/2 step so add/sub 1 |
79 |
else step.pos--;
|
80 |
} |
81 |
|
82 |
void step_fullstep()
|
83 |
{ |
84 |
if(step.dir==0) return; //do not step if not enabled |
85 |
PORTD |= (1<<S_STEP); //step once |
86 |
_delay_us(1); //conform with step timing |
87 |
PORTD &= (~(1<<S_STEP)); //bring the step bin back down |
88 |
if(step.dir==1) step.pos+=2; // full step so add/sub 2 |
89 |
else step.pos-=2; |
90 |
} |
91 |
|
92 |
void step_flush()
|
93 |
{ |
94 |
PORTD &= (~(1<<S_STEP)); //bring the step bin back down |
95 |
} |
96 |
|
97 |
/* tick every speed ms in sweep mode */
|
98 |
void step_sweep_speed(unsigned int speed) |
99 |
{ |
100 |
if(speed<40) step.speed = 40; |
101 |
else step.speed = speed;
|
102 |
} |
103 |
|
104 |
|
105 |
//ccw must be less than 0 and cw must be greater than 0
|
106 |
void step_sweep_bounds(int ccw, int cw) |
107 |
{ |
108 |
step.ccw = ccw; |
109 |
step.cw = cw; |
110 |
} |
111 |
|
112 |
void step_sweep()
|
113 |
{ |
114 |
step.time += step.sweep_ms; |
115 |
if(step.time >= step.speed)
|
116 |
{ |
117 |
step_fullstep(); |
118 |
if((step.dir == 1) && (step.cw <= step.pos)) step_dir(-1); |
119 |
else if((step.dir == -1) && (step.ccw >= step.pos)) step_dir(1); |
120 |
step.time = 0;
|
121 |
} |
122 |
} |