-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorControl.ino
executable file
·216 lines (161 loc) · 4.17 KB
/
motorControl.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
206
207
208
209
210
211
212
213
214
215
216
/*
* Name: Control.ino
* Created: 1/11/2017 12:15:59 PM
* Author: Connor Minielly & Jon Marcello
*
* Jon's Code preceded by //#J => ...
*
* Connor's code preceded by //#C => ...
*/
//#C => For I2C
#include <Wire.h>
//#C => I2C address of DAC when all pins of DAC go to ground.
const int DAC_address = 0x2C;
//#C => boolean value to represent a coasting state, another to tell the loop when to write values to the DAC.
bool coasting = false;
bool write = false;
int redLED = 5;
int dataLED = 6;
int demoModeSwitch = 7;
int raceModeSwitch = 8;
int brakeSwitch = 4;
int currentPin = A0;
int voltagePin = A1;
int speedPin = 3;
int currentPower = 0;
int previousPower = 0;
float currentCalibration = 0;
float wheelRadius = 0.254; // meters
const float pi = 3.14159265359;
double speed = 0.0;
bool brakeActive = false;
bool speedPinStep = false;
unsigned long previousMillis = 0;
long unsigned int AMP_TENTH_SECONDS = 0.0;
float AMP_HOURS = 0.0;
float current = 0.0;
int x = -1;
// Log Variables
double maxSpeed = 0.0;
float maxCurrent = 0.0;
void writeToDAC(int data) {
if(data < 0 || data > 255 || data == currentPower) return;
Wire.beginTransmission(DAC_address);
Wire.write(0x00);
Wire.write(data);
Wire.endTransmission();
}
//#C => triggers when the arduino recives I2C communications from master.
//#C => basic set up of I2C communication line, runs first in practise.
void setup()
{
pinMode(dataLED, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(demoModeSwitch, INPUT);
pinMode(raceModeSwitch, INPUT);
pinMode(brakeSwitch, INPUT);
pinMode(currentPin, INPUT);
pinMode(speedPin, INPUT);
Wire.begin(8);
Wire.onReceive(speedChange);
Wire.onRequest(sendStatus);
Serial.begin(9600);
writeToDAC(0);
attachInterrupt(1, calculateSpeed, FALLING);
cli();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 1561;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A);
sei();
currentCalibration = analogRead(currentPin);
//currentCalibration = 121.0;
}
void sendStatus() {
String encodedMessage = String(getCarMode()) + " " + String(AMP_HOURS) + " " + String(current) + " " + String((int)round(speed)) + " " + String(currentPower) + " 0";
Serial.println(encodedMessage);
Wire.write(encodedMessage.c_str());
}
void speedChange(int bytes)
{
x = Wire.read();
Serial.println(x);
currentPower = x;
}
int getCarMode() {
//******
// 0 = normal
// 1 = race
// 2 = demo
//******
if(digitalRead(demoModeSwitch)) {
if(!brakeActive) {
digitalWrite(redLED, HIGH);
}
return 2;
} else if(!brakeActive) {
digitalWrite(redLED, LOW);
}
if(digitalRead(raceModeSwitch)) {
return 1;
}
return 0;
}
ISR(TIMER1_COMPA_vect) {
current = calculateAmps();
if(current < 0.0) current = 0.0;
if(current > maxCurrent) maxCurrent = current;
AMP_TENTH_SECONDS += current;
AMP_HOURS = AMP_TENTH_SECONDS / 36000.0;
}
float calculateAmps() {
return mapfloat(analogRead(currentPin), currentCalibration, 1024, 0, 200);
}
float mapfloat(long x, long in_min, long in_max, long out_min, long out_max)
{
return (float)(x - in_min) * (out_max - out_min) / (float)(in_max - in_min) + out_min;
}
float calculateVoltage() {
return (analogRead(voltagePin) * 30.0) / 1024.0;
}
void calculateSpeed() {
unsigned long currentMillis = millis();
float rotationDistance = (2.0 * pi * wheelRadius) / 4.0;
float velocity = ((rotationDistance / ((currentMillis - previousMillis) / 1000.0)) * 18.0) / 5.0;
previousMillis = currentMillis;
if(currentMillis - previousMillis > 500) {
velocity = 0.0;
previousMillis = currentMillis;
}
speed = velocity;
if(speed > maxSpeed) maxSpeed = speed;
}
void loop()
{
brakeActive = !digitalRead(brakeSwitch);
if(brakeActive) {
//previousPower = currentPower;
writeToDAC(0);
digitalWrite(redLED, HIGH);
Serial.println("BRK");
currentPower = 0;
} else {
//currentPower = previousPower;
digitalWrite(redLED, LOW);
}
getCarMode();
if(millis() - previousMillis > 750) {
speed = 0;
}
if (x != -1) {
x = map(x,0,100,0,255);
if(!brakeActive) {
writeToDAC(x);
}
Serial.println(x);
x = -1;
}
}