-
Notifications
You must be signed in to change notification settings - Fork 0
/
motor.c
118 lines (94 loc) · 1.93 KB
/
motor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "motor.h"
#include "position.h"
volatile int motor_status = -1;
volatile int motor_adj_status = -1;
volatile int motor_correction = -1;
volatile int motor_correction_dir = -1;
stepper_t *stepper0, *stepper1;
void motor_init(stepper_t *left, stepper_t *right)
{
stepper0 = left;
stepper1 = right;
}
void motor_turn_left()
{
motor_status = MOTOR_TURNING;
int i;
for (i = 0; i < TURN_ADJ; i++)
{
stepper_step(stepper0, BACKWARD);
stepper_step(stepper1, FORWARD);
}
dectection_force_update();
motor_status = MOTOR_STANDBY;
}
void motor_turn_right()
{
motor_status = MOTOR_TURNING;
int i;
for (i = 0; i < TURN_ADJ; i++)
{
stepper_step(stepper0, FORWARD);
stepper_step(stepper1, BACKWARD);
}
dectection_force_update();
motor_status = MOTOR_STANDBY;
}
void motor_turn_180()
{
motor_status = MOTOR_TURNING;
int i;
for (i = 0; i < TURN_180; i++)
{
stepper_step(stepper0, FORWARD);
stepper_step(stepper1, BACKWARD);
}
dectection_force_update();
motor_status = MOTOR_STANDBY;
}
void motor_move_forward()
{
motor_status = MOTOR_MOVING;
int i;
for (i = 1; i < BLOCK; i++)
{
// Don't hit the wall
if (motor_adj_status == MOTOR_WALL)
{
break;
}
stepper_step(stepper0, FORWARD);
stepper_step(stepper1, FORWARD);
if (motor_adj_status == MOTOR_EXP_COR && ((i % motor_correction) == 0))
{
if (motor_correction_dir == MOTOR_ADJ_LEFT)
{
stepper_step(stepper0, FORWARD);
}
else if (motor_correction_dir == MOTOR_ADJ_RIGHT)
{
stepper_step(stepper1, FORWARD);
}
}
}
motor_status = MOTOR_STANDBY;
}
void motor_turn_to_direction(pos_t *current, dir_t dir)
{
if (current->direction == dir)
{
return;
}
else if (position_right_adj_direction(current->direction) == dir)
{
motor_turn_right(stepper0, stepper1);
}
else if (position_left_adj_direction(current->direction) == dir)
{
motor_turn_left(stepper0, stepper1);
}
else
{
motor_turn_180(stepper0, stepper1);
}
}