This repository was archived by the owner on Aug 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
143 lines (112 loc) · 3.63 KB
/
main.cpp
File metadata and controls
143 lines (112 loc) · 3.63 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "SpearArm.h"
#include "StepperC.h"
#include "StepperAmis.h"
#include "Motor.h"
#include "DCMotor.h"
#include "DCPotMotor.h"
#include "common.h"
#include <math.h>
#include <string.h>
#include "main.h"
uint8_t stepper_hold;
uint8_t in_home_mode;
// arm needs to be a global variable since the motors need to be modified by interrupt functions
Arm* arm;
typedef enum {
SERIAL_SUCCESS,
SERIAL_ERROR,
} ser_err_t;
static ser_err_t handle_serial(char* buffer);
static void handle_command(char* buffer, uint32_t* armPosition);
#ifndef digitalPinToInterrupt
#define digitalPinToInterrupt(p) ( (p) == 2 ? 0 : ((p) == 3 ? 1 : \
((p) >= 18 && (p) <= 21 ? 23 - (p) : -1)) )
#endif
/*
// define interrupt functions for limit switches on stepper motors
void zeroBase() {
arm->baseMotor.step_number = 0;
arm->baseMotor.current_motor_radian = 0;
}
void zeroElbow() {
arm->elbowMotor.step_number = 0;
arm->elbowMotor.current_motor_radian = 0;
}
void zeroWristPitch() {
arm->wristPitchMotor.step_number = 0;
arm->wristPitchMotor.current_motor_radian = 0;
}
*/
void setup(){
init();
SPI.begin();
Serial.begin(9600);
Serial.println("Initalized");
delay(1000);
}
/*
void attachInterrupts() {
// attach interrupts for stepper motors with limit switches
attachInterrupt(digitalPinToInterrupt(arm->baseMotor.limitSwitchPin), zeroBase, FALLING);
attachInterrupt(digitalPinToInterrupt(arm->elbowMotor.limitSwitchPin), zeroElbow, FALLING);
attachInterrupt(digitalPinToInterrupt(arm->wristPitchMotor.limitSwitchPin), zeroWristPitch, FALLING);
}
*/
int main(){
uint32_t armPosition[NUM_MOTORS];
stepper_hold = 42;
setup();
arm = new Arm();
//attachInterrupts(); // this must be done AFTER arm constructor is called since arm constructor sets pin modes
//arm->baseMotor.setForwardDirection(true);
//arm->wristPitchMotor.setForwardDirection(false);
// initialize steppers that use the amis driver
// StepperAmis elbowMotorAmis(12, 2000);
// move motors that have limit switches until they hit their limit switches
//arm->home();
//Serial.println("home");
char buffer[8] = {0};
armPosition[SHOULDER] = (((double)400) / ((double)1023)) * UINT32_MAX; // ensure shoulder starts at a comfortable location
for(;;){
if (Serial.available() >= 8) {
if (handle_serial(buffer) == SERIAL_SUCCESS) {
handle_command(buffer, armPosition);
}
}
//arm->adjust(armPosition);
arm->shoulderMotor.rotateTowardsRadian(armPosition[SHOULDER]);
Serial.println(arm->shoulderMotor.potPosition);
}
return 0;
}
/**@brief Function to read in serial and reject malformed "packets"
*/
static ser_err_t handle_serial(char* buffer) {
Serial.readBytes(buffer, 8);
// Error checking
if (buffer[0] != 2 || buffer[7] != 3) { // Start or stop bytes are gone
while(Serial.read() != -1);
return SERIAL_ERROR;
}
if (buffer[6] != (uint8_t)(buffer[2] + buffer[3] + buffer[4] + buffer[5])) {
while(Serial.read() != -1);
return SERIAL_ERROR;
}
Serial.println("Packet Accepted.");
return SERIAL_SUCCESS;
}
static void handle_command(char* buffer, uint32_t* armPosition) {
uint32_t angle;
// buffer[1] is command
memcpy(&angle, buffer + 2, sizeof(uint32_t)); // Actual data starts at buffer[2]
// Homing command (254 because thats spear_home)
if (buffer[1] == 254) {
arm->home();
return;
}
if (buffer[1] >= NUM_MOTORS) {
return;
}
// Set the correct motor to the command's angle
armPosition[(uint8_t)buffer[1]] = angle;
}