forked from Sensorica/PVCharacterization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvclib.cpp
158 lines (127 loc) · 2.41 KB
/
pvclib.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "Arduino.h"
#include "pvclib.h"
#if HWTYPE == PVCSERVO
#include <Servo.h>
#include "pvcservo.h"
#endif
#if HWTYPE == PVCSTEPPER
// TODO: Add stepper includes
#endif
int xPosition = 0;
int yPosition = 0;
const char goToCmd = 'G';
const char readCmd = 'R';
const char scanCmd = 'S';
const int dataPin = A0;
void pvcSetup()
{
#if HWTYPE == PVCSERVO
pvcServoSetup();
#endif
#if HWTYPE == PVCSTEPPER
// TODO: Add stepper setup
#endif
}
void pvcLoop()
{
byte cmd[10];
clearCmd(cmd);
getCmd(cmd);
printCmd(cmd);
runCmd(cmd);
}
void getCmd(byte cmd[10])
{
int x = 0;
while (Serial.available() > 0) {
cmd[x] = Serial.read();
x++;
delay(100);
}
}
void runCmd(byte cmd[10])
{
if (cmd[0] != '\n')
{
char letter = cmd[0];
if (letter == goToCmd)
{
goTo(cmd);
}
else if (letter == readCmd)
{
readValue(cmd);
}
else
{
Serial.println("Invalid command");
}
}
}
void printCmd(byte cmd[10])
{
if (cmd[0] != '\n')
{
Serial.print("Cmd:");
for (int i = 0; i < 10; i++)
{
if (cmd[i] != '\n')
Serial.print(char(cmd[i]));
}
Serial.println();
}
}
void clearCmd(byte cmd[10])
{
for (int i = 0; i < 10; i++)
{
cmd[i] = '\n';
}
}
void goTo(byte cmd[10])
{
Serial.println("Going to...");
char axis = char(cmd[1]);
// Get the number of degrees (up to 3 digits)
char buffer[3];
buffer[0] = cmd[2];
buffer[1] = cmd[3];
buffer[2] = cmd[4];
int degrees = atoi(buffer);
if (axis == 'X')
xPosition = degrees;
else if (axis == 'Y')
yPosition = degrees;
Serial.print("Axis:");
Serial.println(axis);
Serial.print("Degrees:");
Serial.println(degrees);
gimbalGo(xPosition, yPosition);
}
void readValue(byte cmd[10])
{
Serial.println("Reading value...");
int numberOfReadings = 1;
if (cmd[1] != '\n')
{
char buffer[3];
buffer[0] = cmd[1];
buffer[1] = cmd[2];
buffer[2] = cmd[3];
numberOfReadings = atoi(buffer);
}
Serial.print("Number of readings:");
Serial.println(numberOfReadings);
for (int i = 0; i < numberOfReadings; i++)
{
int reading = analogRead(dataPin);
// Start the line with "D;" to indicate the rest of the line is data
Serial.print("D:");
Serial.print(reading);
Serial.print(";X:");
Serial.print(xPosition);
Serial.print(";Y:");
Serial.print(yPosition);
Serial.println(";");
}
}