summaryrefslogtreecommitdiff
path: root/debug/motordriver/motordriver.ino
blob: e98c6bc1cfbc669f2cba10dc72dcb856fd03efe3 (plain)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

#include <Adafruit_MCP23X17.h>
//moveohmi = https://robotics.stackexchange.com/questions/7829/how-to-program-a-three-wheel-omni
//https://www.pololu.com/product/2997
//https://www.pololu.com/product/4861
//
// motor one/A  front
#define  m1enA 3   // GPA3
#define  m1enB 2   // GPA2
#define  m1pwm1 0  // GPA0
#define  m1pwm2 1  // GPA1
#define  m1encA 12 // GPB4
#define  m1encB 13 // GPB5
// motor two/B  left
#define  m2enA 7   // GPA7
#define  m2enB 6   // GPA6
#define  m2pwm1 4  // GPA4
#define  m2pwm2 5  // GPA5
#define  m2encA 14 // GPB6
#define  m2encB 15 // GPB7
// motor three/C    right
#define  m3enA 11  // GPB3
#define  m3enB 10  // GPB2
#define  m3pwm1 8  // GPB0
#define  m3pwm2 9  // GPB1
#define  m3encA 24   // teensy port 24
#define  m3encB 9   // teensy port 9

double sp = 1.000; //speed in 0-1

double dirA = 0.00;
double dirB = 0.00;
double dirC = 0.00;
double spdA = 0.00;
double spdB = 0.00;
double spdC = 0.00;
double dirrad = 0.00;
double pi = 3.1415926535897;

double Arpm = 0.00;
volatile long Aencount = 0;
unsigned long ALtime = 0;
unsigned long ACtime = 0;
unsigned long APtime = 0;
double Brpm = 0.00;
volatile long Bencount = 0;
unsigned long BLtime = 0;
unsigned long BCtime = 0;
unsigned long BPtime = 0;
double Crpm = 0.00;
volatile long Cencount = 0;
unsigned long CLtime = 0;
unsigned long CCtime = 0;
unsigned long CPtime = 0;

Adafruit_MCP23X17 mcp2;

void setup()
{
 if (!mcp2.begin_I2C(0x22)) {
  Serial.println("Error initializing MCP23017 at 0x22.");
  while (1);
 }
 // set all the motor control pins to outputs
 mcp2.pinMode(m1enA, OUTPUT);
 mcp2.pinMode(m1enB, OUTPUT);
 mcp2.pinMode(m2enA, OUTPUT);
 mcp2.pinMode(m2enB, OUTPUT);
 mcp2.pinMode(m3enA, OUTPUT);
 mcp2.pinMode(m3enB, OUTPUT);

 mcp2.pinMode(m1pwm1, OUTPUT);
 mcp2.pinMode(m1pwm2, OUTPUT);
 mcp2.pinMode(m2pwm1, OUTPUT);
 mcp2.pinMode(m2pwm2, OUTPUT);
 mcp2.pinMode(m3pwm1, OUTPUT);
 mcp2.pinMode(m3pwm2, OUTPUT);

 //set encoder pins to input
 mcp2.pinMode(m1encA, INPUT);
 mcp2.pinMode(m1encB, INPUT);
 mcp2.pinMode(m2encA, INPUT);
 mcp2.pinMode(m2encB, INPUT);
 pinMode(m3encA, INPUT);
 pinMode(m3encB, INPUT);
 attachInterrupt(digitalPinToInterrupt(m1encA), AencoderISR, CHANGE);
 attachInterrupt(digitalPinToInterrupt(m1encB), AencoderISR, CHANGE);
 attachInterrupt(digitalPinToInterrupt(m2encA), BencoderISR, CHANGE);
 attachInterrupt(digitalPinToInterrupt(m2encB), BencoderISR, CHANGE);
 attachInterrupt(digitalPinToInterrupt(m3encA), CencoderISR, CHANGE);
 attachInterrupt(digitalPinToInterrupt(m3encB), CencoderISR, CHANGE);
}
void softpwm(int pin, int value){


}
void AencoderISR() {
  Aencount++;
}
void BencoderISR() {
  Bencount++;
}
void CencoderISR() {
  Cencount++;
}
void Areadenc() {
  ACtime = micros();
  APtime = ACtime - ALtime;
  ALtime = ACtime;
  Arpm = (Aencount / 211.2) * (60000000.0 / APtime);
  Serial.print("Motor A RPM: ");
  Serial.print(Arpm);
  Serial.print("    Sample period:");
  Serial.print(APtime);
  Serial.print("ms, encoder count:");
  Serial.println(Aencount);
  Aencount = 0;
}
void Breadenc() {
  BCtime = micros();
  BPtime = BCtime - BLtime;
  BLtime = BCtime;
  Brpm = (Bencount / 211.2) * (60000000.0 / BPtime);
  Serial.print("Motor B RPM: ");
  Serial.print(Brpm);
  Serial.print("    Sample period: ");
  Serial.print(BPtime);
  Serial.print(" ms, encoder count: ");
  Serial.println(Bencount);
  Bencount = 0;
}
void Creadenc() {
  CCtime = micros();
  CPtime = CCtime - CLtime;
  CLtime = CCtime;
  Crpm = (Cencount / 211.2) * (60000000.0 / CPtime);
  Serial.print("Motor C RPM: ");
  Serial.print(Crpm);
  Serial.print("    Sample period: ");
  Serial.print(CPtime);
  Serial.print(" ms, encoder count: ");
  Serial.println(Cencount);
  Cencount = 0;
}

void Amove(double sped) {
  spdA = abs(sped)*255;
  mcp2.digitalWrite(m1enA, HIGH);
  mcp2.digitalWrite(m1enB, LOW);
  if (sped < 0) {
    Serial.print("Motor A back ");
    mcp2.analogWrite(m1pwm1, spdA);
    mcp2.analogWrite(m1pwm2, 0);
  } else {
    Serial.print("Motor A forward ");
    mcp2.analogWrite(m1pwm1, 0);
    mcp2.analogWrite(m1pwm2, spdA);
  } 
  Serial.println(spdA);
}
void Bmove(double sped) {
  spdB = abs(sped)*255;
  mcp2.digitalWrite(m2enA, HIGH);
  mcp2.digitalWrite(m2enB, LOW);
  if (sped < 0) {
    Serial.print("Motor B back ");
    mcp2.analogWrite(m2pwm1, spdB);
    mcp2.analogWrite(m2pwm2, 0);
  } else {
    Serial.print("Motor B forward ");
    mcp2.analogWrite(m2pwm1, 0);
    mcp2.analogWrite(m2pwm2, spdB);
  } 
  Serial.println(spdB);
}
void Cmove(double sped) {
  spdC = abs(sped) * 255;
  mcp2.digitalWrite(m3enA, HIGH);
  mcp2.digitalWrite(m3enB, LOW);
  if (sped < 0) {
    Serial.print("Motor C back ");
    mcp2.analogWrite(m3pwm1, spdC);
    mcp2.analogWrite(m3pwm2, 0);
  } else {
    Serial.print("Motor C forward ");
    mcp2.analogWrite(m3pwm1, 0);
    mcp2.analogWrite(m3pwm2, spdC);
  } 
  Serial.println(spdC);
}

void moveohmi(int speed, int direction){
  Amove(0);
  Bmove(0);
  Cmove(0);

  double dirrad = (direction*71.0000) / 4068.0000;  
  Serial.println(dirrad);
  //vector ontb in x en y
  double y = sin(dirrad);
  double x = cos(dirrad);
  Serial.println((String)"x:"+x+" y:"+y);
  double spdeA = y*speed;
  double spdeB = (-0.500*x - 0.8660054*y)*speed;
  double spdeC = (-0.500*x + 0.8660054*y)*speed;
  Amove(spdeA);
  Bmove(spdeB);
  Cmove(spdeC);
  Serial.println();
  Serial.println();
}
void rotate(int rodeg, int speed){
//use compas heading
}
void demo(){
  Amove(sp);
  Bmove(sp);
  Cmove(sp); 
  Serial.println("seperate A B C rotation");
  delay(1000);
  
  Amove(0);
  Bmove(0);
  Cmove(0);
  Serial.println("seperate A B C stop");
  delay(1000);

  Amove(-sp);
  Bmove(-sp);
  Cmove(-sp);
  Serial.println("seperate A B C -rotation");
  delay(1000);

  moveohmi(0, 0);
  Serial.println("ohmi stop");
  delay(1000);

  rotate(90, sp);
  Serial.println("rotation function 90deg");
  rotate(90, sp);
  Serial.println("rotation function -90deg");
  delay(1000);

  moveohmi(sp, 35);
  Serial.println("ohmi move 35deg");
  delay(1000);

  moveohmi(sp, 240);
  Serial.println("ohmi move 215deg");
  delay(1000);

  for(int i = 0; i<360; i++){
    moveohmi(sp, i);
    Serial.println(i);
    delay(50);
  }
  Serial.println("ohmi move circle");
  delay(1000);
}
void loop(){
  demo();
  delay(100);

}