-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch_sensorarduino.ino
133 lines (110 loc) · 2.85 KB
/
sketch_sensorarduino.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
#include <SPI.h>
#include <Ethernet.h>
#define encoder0PinA 2
#define encoder0PinB 7
#define encoder0PinZ 5
#define calibrationA A0
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x2F
};
//network variables
IPAddress ip(10, 7, 0, 190);
IPAddress myDns(10, 0, 0, 15);
IPAddress gateway(10, 7, 0, 20);
IPAddress subnet(255, 255, 255, 0);
volatile long encoder0Pos=0;
volatile long encoderZPos=0;
long newposition;
long oldposition = 0;
long newtime;
long oldtime = 0;
long velnew;
long velold;
float newangle;
int result;
float oldangle;
String currentdirection;
String olddirection;
String encoderDirection;
boolean calibrationtest = false;
float calibrationAvoltage;
EthernetServer server(23);
boolean alreadyConnected = false;
float calibrationstate = 0;
String readString;
void setup() {
pinMode(encoder0PinA, INPUT);
digitalWrite(encoder0PinA, HIGH); // turn on pullup resistor
pinMode(encoder0PinB, INPUT);
digitalWrite(encoder0PinB, HIGH); // turn on pullup resistor
attachInterrupt(0, doEncoder, RISING); // encoDER ON PIN 2
pinMode(calibrationA, INPUT);
// initialize the ethernet device
Ethernet.begin(mac, ip, myDns, gateway, subnet);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
}
void loop() {
//server start
EthernetClient client = server.available();
if (client) {
if (!alreadyConnected) {
client.flush();
client.println("connected");
alreadyConnected = true;
}
newangle = round(((float)encoder0Pos / (float)100) * (float)3.76);
newposition = (long)newangle;
if (currentdirection != olddirection){
client.print("D");
client.println(currentdirection);
}
//if new position is different then old send new angel
if (newangle != oldangle) {
client.print("A");
client.println(round(newangle), DEC);
}
newtime = millis();
velnew = abs((newposition-oldposition)* 1000/(newtime-oldtime));
if (velold != velnew){
client.print ("S");
client.println (velnew);
}
oldposition = newposition;
olddirection = currentdirection;
oldtime = newtime;
oldangle = newangle;
velold = velnew;
delay(250);
}
}
void doEncoder() {
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos--;
currentdirection = "CW";
} else {
encoder0Pos++;
currentdirection = "CCW";
}
//Serial.println(encoder0Pos, DEC);
}
void doEncoderZ() {
if (encoderDirection != currentDirection) {
encoderDirection = currentDirection;
} else {
encoder0Pos;
}
}
int roundto45(int number) {
if (number > 0){
result = number + 45/2;
result = result - (result % 45);
} else if (number < 0){
result = number - 45/2;
result = result + (abs(result) % 45);
} else{
result = 0;
}
return result;
}