-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_ethernet.ino
137 lines (128 loc) · 3.01 KB
/
2_ethernet.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
#include "defines.h"
void enableShields(bool enable)
{
digitalWrite(ETH_EN, enable ? HIGH : LOW);
}
void hardresetShields()
{
enableShields(false);
delay(1234);
enableShields(true);
delay(1234);
Ethernet.init(10);
delay(1000);
Ethernet.begin(mac, clientIP);
}
void loopEthernet()
{
Ethernet.maintain();
// NO CONNECTION
if (!eth.connected())
{
eth.stop();
delay(500);
if (eth.connect(serverIP, serverPort))
{
Serial.println("Reconnected to TCP server");
}
else
{
Serial.println("Resetting Ethernet Shield");
hardresetShields();
}
}
else // HAS CONNECTION
{
if (eth.available() > 0)
{
receiveEvent();
}
// check for user input
uint8_t i = 0;
for (SCIMotor *m : motors)
{
if (m->getInput())
{
int pos = m->m()->targetPosition();
byte d[] = {i, pos >> 8, pos};
eth.write(d, 3);
#if DEBUG == true
Serial.print("Send Input: [");
Serial.print(d[0], HEX);
Serial.print(d[1], HEX);
Serial.print(d[2], HEX);
Serial.println("]");
#endif
}
i++;
}
}
}
void receiveEvent()
{
digitalWrite(LED_BUILTIN, HIGH);
int dataSize = eth.available();
byte data[dataSize];
// Read Ethernet Data
for (int i = 0; i < dataSize; i++)
{
data[i] = eth.read();
}
// Parse data package
switch (data[0])
{
case MessageType::PING:
Serial.println("Received Ping");
break;
case MessageType::SETPARAM:
{
Serial.print("Set Param To ");
int val = max(0, (int)word(data[2], data[3]));
Serial.println(val);
switch (data[1])
{
case ParamID::SPEED:
{
for (SCIMotor *m : motors)
m->setMaxSpeed(val);
break;
}
case ParamID::ACCELERATION:
{
for (SCIMotor *m : motors)
m->setAcceleration(val);
break;
}
default:
{
Serial.println("Received faulty parameter data");
break;
}
}
break;
}
case MessageType::ELEMENTPOS:
{
if (dataSize == 10)
{
int val0 = (int)word(data[2], data[3]);
int val1 = (int)word(data[4], data[5]);
int val2 = (int)word(data[6], data[7]);
int val3 = (int)word(data[8], data[9]);
motors[0]->moveTo(val0);
motors[1]->moveTo(val1);
motors[2]->moveTo(val2);
motors[3]->moveTo(val3);
}
else
{
Serial.println("Message to set Elements' Position had incorrect length.");
}
break;
}
default:
Serial.println("Received unknown data package");
break;
}
digitalWrite(LED_BUILTIN, LOW);
}