-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ino
205 lines (175 loc) · 4.48 KB
/
main.ino
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Installed libraries
#include <ArduinoHttpClient.h>
// Arm functions include
#include "arm.h"
// Standard library includes
#include <SPI.h>
#include <Ethernet.h>
// Define enable pin
#define EN 5
// Define direction pins
#define ARM_DIR 3
#define EXTENSION_DIR 6
#define CLAW_DIR 8
// Define step pins
#define ARM_STEP 4
#define EXTENSION_STEP 7
#define CLAW_STEP 9
#define PULSE_WIDTH 20
// Global ethernet variables
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
char server[] = "192.168.0.102";
int port = 8080;
// Global position variables
int armSteps, extensionSteps, clawSteps = 0;
double posX, posY = 0;
double currentArmAngle = 0;
double currentExtensionAngle = 180;
double currentClawAngle = 180;
double targetArmAngle = currentArmAngle;
double targetExtensionAngle = currentExtensionAngle;
double targetClawAngle = currentClawAngle;
// Websocket client
EthernetClient ethernet;
WebSocketClient client = WebSocketClient(ethernet, server, port);
void readSocket()
{
if (client.connected())
{
// Check if a message is available to be received
int messageSize = client.parseMessage();
if (messageSize > 0)
{
String message = client.readString();
if (message == "U")
{
if (fabs(currentClawAngle - targetClawAngle) < 5)
{
targetClawAngle++;
}
}
else if (message == "P")
{
if (fabs(currentClawAngle - targetClawAngle) < 5)
{
targetClawAngle--;
}
}
else
{
// Parse the message to set the new x and y positions
char *token = strtok(message.c_str(), ",");
double x = (double)atof(token);
token = strtok(NULL, ",");
double y = (double)atof(token);
posX = x;
posY = y;
}
}
}
}
void stepArm()
{
const double diff = fabs(targetArmAngle - currentArmAngle);
if (diff > 0.1)
{
if (targetArmAngle > currentArmAngle)
{
step(false, ARM_DIR, ARM_STEP, PULSE_WIDTH);
currentArmAngle += stepsToDegrees(1) / ARM_GEAR_RATIO;
currentExtensionAngle -= stepsToDegrees(1) / (ARM_GEAR_RATIO * ARM_GEAR_RATIO);
}
else if (targetArmAngle < currentArmAngle)
{
step(true, ARM_DIR, ARM_STEP, PULSE_WIDTH);
currentArmAngle -= stepsToDegrees(1) / ARM_GEAR_RATIO;
currentExtensionAngle += stepsToDegrees(1) / (ARM_GEAR_RATIO * ARM_GEAR_RATIO);
}
}
}
void stepExtension()
{
const double diff = fabs(targetExtensionAngle - currentExtensionAngle);
if (diff > 0.1)
{
if (targetExtensionAngle > currentExtensionAngle)
{
step(false, EXTENSION_DIR, EXTENSION_STEP, PULSE_WIDTH);
currentExtensionAngle += stepsToDegrees(1) / EXTENSION_GEAR_RATIO;
currentClawAngle += stepsToDegrees(1) / (EXTENSION_GEAR_RATIO);
}
else if (targetExtensionAngle < currentExtensionAngle)
{
step(true, EXTENSION_DIR, EXTENSION_STEP, PULSE_WIDTH);
currentExtensionAngle -= stepsToDegrees(1) / EXTENSION_GEAR_RATIO;
currentClawAngle -= stepsToDegrees(1) / (EXTENSION_GEAR_RATIO);
}
}
}
void stepClaw()
{
const double diff = fabs(targetClawAngle - currentClawAngle);
if (diff > 0.1)
{
if (targetClawAngle > currentClawAngle)
{
step(false, CLAW_DIR, CLAW_STEP, PULSE_WIDTH);
currentClawAngle += stepsToDegrees(1) / CLAW_GEAR_RATIO;
}
else if (targetClawAngle < currentClawAngle)
{
step(true, CLAW_DIR, CLAW_STEP, PULSE_WIDTH);
currentClawAngle -= stepsToDegrees(1) / CLAW_GEAR_RATIO;
}
}
}
/**
* return true: rotate cw, return false: rotate ccw
*/
boolean checkAngle(double current, double target)
{
if (current < target)
{
return false;
}
else
{
return true;
}
}
void setup()
{
Serial.begin(9600);
Serial.print("arm angle: ");
Serial.println(targetArmAngle);
Serial.print("extension angle: ");
Serial.println(targetExtensionAngle);
// Initialize ip connection
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP!");
}
else
{
Serial.println(Ethernet.localIP());
}
pinMode(ARM_DIR, OUTPUT);
pinMode(ARM_STEP, OUTPUT);
pinMode(EXTENSION_DIR, OUTPUT);
pinMode(EXTENSION_STEP, OUTPUT);
pinMode(CLAW_DIR, OUTPUT);
pinMode(CLAW_STEP, OUTPUT);
pinMode(EN, OUTPUT);
digitalWrite(EN, HIGH);
// Start websocket client
client.begin();
}
void loop()
{
readSocket();
stepArm();
stepExtension();
stepClaw();
targetArmAngle = getArmAngle(posX, posY);
targetExtensionAngle = getExtensionAngle(posX, posY);
}