-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmotor.c
More file actions
105 lines (87 loc) · 2.68 KB
/
motor.c
File metadata and controls
105 lines (87 loc) · 2.68 KB
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
#include "xc.h"
#include "motor.h"
#include "misc.h"
#include "pic32m_builtins.h"
//stepper motor functions
const float cutting_distance = 1; //distance knife will travel
const float wheel_radius = .1;//in cm. Measured diameter = 20.08mm
const float step_angle = 1.8; //in degrees
//from "SM_42BYG011-25"
const float dps = step_angle * wheel_radius; //distance per step
//etc...
//we want one step and one step only (one signal is changed, not both)
//1-16
//17-32
//33-48
//49-64
int motor_states[4][2] = {
{0, 1},
{0, 0},
{1, 0},
{1, 1}
};
static int feeder_index = 0;
static int feeder_direction = -1; // -1 for backward, 1 for forward
static int cutter_index = 0;
static int cutter_direction = 1; // -1 for backward, 1 for forward
void motor_setCutterDirection(int direction) {
switch (direction) {
case 1:
case -1: cutter_direction = direction;
default: cutter_direction = -1;
}
}
void motor_setFeederDirection(int direction) {
switch (direction) {
case 1:
case -1: feeder_direction = direction;
default: feeder_direction = 1;
}
}
void motor_stepCutter() {
__builtin_disable_interrupts(); // Disable interrupts for one step
cutter_index += cutter_direction;
if (cutter_index > 3) {
cutter_index = 0;
} else if (cutter_index < 0) {
cutter_index = 3;
}
PHASE3 = motor_states[cutter_index][0];
PHASE4 = motor_states[cutter_index][1];
__builtin_enable_interrupts(); // Enable interrupts
}
void motor_stepFeeder(int steps) {
// ~1600 us delay max
for (int i = 0; i < steps; i++) {
__builtin_disable_interrupts(); // Disable interrupts for one step
feeder_index += feeder_direction;
if (feeder_index > 3) {
feeder_index = 0;
} else if (feeder_index < 0) {
feeder_index = 3;
}
PHASE1 = motor_states[feeder_index][0];
PHASE2 = motor_states[feeder_index][1];
__builtin_enable_interrupts(); // Enable interrupts
delay_us(5000);
}
}
void motor_feed(int distance) { //distance in centimeters, can only be int input
float dps = step_angle * wheel_radius;
int steps = distance / dps; //dps = distance/step;
motor_stepFeeder(steps);
}
void motor_cut() {
int numSteps = cutting_distance / dps;
for (int i = 0; i < numSteps; i++) {
motor_stepCutter();
delay_us(1900); //shorter delay for faster cutting speed
}
cutter_direction = -(cutter_direction);
delay_ms(250);
for (int i = 0; i < numSteps; i++) {
motor_stepCutter();
delay_us(2200); //slightly relaxed journey back to resting position
}
cutter_direction = -(cutter_direction);
}