Project

General

Profile

Revision 2853d46b

ID2853d46ba3da7879828050adece3cb330e999876
Parent 2b0ba4bb
Child 958699af

Added by Thomas Mullins over 11 years ago

Changed stepper to always step in step_sweep

Also added whole/half step as a separate setting, with a single
step_do_step function. Also added stepper to main.cpp.

View differences:

scout_avr/src/stepper.cpp
12 12
 */
13 13

  
14 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 15
  int pos; // position in rotation.
19 16
  int dir; // direction. -1 CCW. 1 CW. 0 OFF
17
  int step_size; // amount to add to position each step
20 18
  int ccw;
21 19
  int cw;
22
} volatile step;
20
} step;
23 21

  
24 22

  
25
func step_init(unsigned int call_ms)
23
void step_init()
26 24
{
27
  /* set update time for sweep */
28
  step.sweep_ms = call_ms;
29
  
30
  /* init pos, time, and dir to 0 */
25
  /* init pos and dir to 0 */
31 26
  step.pos = 0;
32
  step.time = 0;
33 27
  step.dir = 0;
34 28

  
35 29
  //set control pins as output
......
37 31
  DDRB |= ((1<<S_MS));
38 32

  
39 33
  //initiate to full steps
40
  PORTB &= (~(1<<S_MS));
34
  step_set_size(STEP_WHOLE);
41 35
 
42 36
  //initiate the step pin to be low. stepper steps on low to high
43 37
  PORTD &= (~(1<<S_STEP));
38
}
44 39

  
45
  //return function pointer to sweep to be called ever sweep_us uS. 
46
  return &(step_sweep);
40
void step_set_size(char size)
41
{
42
  if (size == STEP_WHOLE)
43
  {
44
    PORTB &= ~_BV(S_MS);
45
    step.step_size = 2;
46
  }
47
  else
48
  {
49
    PORTB |= _BV(S_MS);
50
    step.step_size = 1;
51
  }
47 52
}
48 53

  
49 54
/* set direction pin */
......
53 58
  switch(dir)
54 59
  {
55 60
    case 1:
56
         PORTD |= (1<<S_DIR);
61
      PORTD |= (1<<S_DIR);
57 62
      break;
58 63
    case -1:
59
         PORTD &= (~(1<<S_DIR));
64
      PORTD &= (~(1<<S_DIR));
60 65
      break;
61 66
  }
62 67
}
63 68

  
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()
69
void step_do_step()
83 70
{
84 71
  if(step.dir==0) return; //do not step if not enabled
85 72
  PORTD |= (1<<S_STEP); //step once 
86 73
  _delay_us(1); //conform with step timing
87 74
  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;
75
  if(step.dir==1) step.pos += step.step_size;
76
  else step.pos -= step.step_size;
90 77
}
91 78

  
92 79
void step_flush()
......
94 81
  PORTD &= (~(1<<S_STEP)); //bring the step bin back down
95 82
}
96 83

  
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 84
//ccw must be less than 0 and cw must be greater than 0
106 85
void step_sweep_bounds(int ccw, int cw)
107 86
{
......
111 90

  
112 91
void step_sweep()
113 92
{
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
  }
93
  step_do_step();
94
  if((step.dir == 1) && (step.cw <= step.pos)) step_dir(-1);
95
  else if((step.dir == -1) && (step.ccw >= step.pos)) step_dir(1);
122 96
}

Also available in: Unified diff