This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motorDriver.c
419 lines (245 loc) · 7.87 KB
/
motorDriver.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "motorDriver.h"
#define PRIORITY_INTERRUPT_DRIVE (0u)
#define V_MOD 1023
#define LEFT (1u<<13)
#define RIGHT (1u<<9)
uint32_t tmp = 0;
uint32_t ACTUAL_DISTANCE = 0;
uint8_t FLAG_LIMIT_DIST = 0;
uint32_t LIMIT = 0;
uint8_t speed = 50;
void motorDriverInit(void){
// CLOCK_SETUP 1
// 1 ... Multipurpose Clock Generator (MCG) in PLL Engaged External (PEE) mode
// Reference clock source for MCG module is an external crystal 8MHz
// Core clock = 48MHz, BusClock = 24MHz
//
SIM -> SCGC5 |= SIM_SCGC5_PORTA_MASK
| SIM_SCGC5_PORTC_MASK
| SIM_SCGC5_PORTD_MASK;
//
SIM -> SCGC6 |= SIM_SCGC6_TPM0_MASK;
//
PORTA ->PCR[6] |= PORT_PCR_MUX(3); // TPM0_CH3 - encoder
PORTA ->PCR[13] |= PORT_PCR_MUX(1); // PHASE - Left
PORTC ->PCR[9] |= PORT_PCR_MUX(1); // PHASE - Right
PORTD ->PCR[2] |= PORT_PCR_MUX(4); // TPM0_CH2 - PWM - Right
PORTD ->PCR[4] |= PORT_PCR_MUX(4); // TPM0_CH4 - PWM - Left
PORTD ->PCR[5] |= PORT_PCR_MUX(4); //TPM0_CH5 - encoder
// OUTPUT pin
PTA->PDDR |= (1u<<13);
PTC->PDDR |= (1u<<9);
////////////////////// PWM /////////////////////////////////
//select source reference TMP0
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1); // ?set 'MCGFLLCLK clock or MCGPLLCLK/2'
SIM->SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK;// set "MCGPLLCLK clock with fixed divide by two"
// set "up-counting"
TPM0->SC &= ~TPM_SC_CPWMS_MASK; // default set
// divide by 1
TPM0->SC &= ~TPM_SC_PS_MASK; // the same TPM_SC_PS(0)
// clear counter
TPM0->CNT = 0x00;
// set MOD for PWM period equal 4095 ( 12 bit)
TPM0->MOD = V_MOD;
//////////CHANNEL ENGINE ////////////////////////////////
//Right engine
// set TPM0 channel 2 - "Edge-aligned PWM High-true pulses"
TPM0->CONTROLS[2].CnSC |= TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;
// Default value for Right engine
TPM0->CONTROLS[2].CnV = 0; // STOP
//Left engine
// set TPM0 channel 4 - "Edge-aligned PWM High-true pulses"
TPM0->CONTROLS[4].CnSC |= TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK;
// Default value for Left engine
TPM0->CONTROLS[4].CnV = 0; // STOP
//////////CHANNEL ENCODER////////////////////////////////
//Left Engine
// set TPM0 channel 3 - "Edge-aligned PWM High-true pulses"
//TPM0->CONTROLS[3].CnSC |= TPM_CnSC_ELSA_MASK |
// TPM_CnSC_ELSB_MASK;
// enable interrupt for channel 3
//TPM0->CONTROLS[3].CnSC |= TPM_CnSC_CHIE_MASK;
//Right Engine
// set TPM0 channel 3 - "Edge-aligned PWM High-true pulses"
//TPM0->CONTROLS[5].CnSC |= TPM_CnSC_ELSA_MASK |
// TPM_CnSC_ELSB_MASK;
// enable interrupt for channel 3
//TPM0->CONTROLS[5].CnSC |= TPM_CnSC_CHIE_MASK;
///NVIC_ClearPendingIRQ(TPM0_IRQn); /* Clear NVIC any pending interrupts on TPM0 */
//NVIC_EnableIRQ(TPM0_IRQn); /* Enable NVIC interrupts source for TPM0 module */
//NVIC_SetPriority (TPM0_IRQn, PRIORITY_INTERRUPT_DRIVE); // priority interrupt
// enable counter
TPM0->SC |= TPM_SC_CMOD(1);
}
void TPM0_IRQnHandler(void){
if(FLAG_LIMIT_DIST){
if( LIMIT <= ACTUAL_DISTANCE)
{
stop();
FLAG_LIMIT_DIST = 0;
}
}
////// tutaj jeszcze kod ktory bd liczyl dystans z enkodera////////////////
}
void driveForwardLeftTrack( int predkosc){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTA->PCOR = LEFT; // clear , set 0 mean forward
if(predkosc >= 100){
TPM0->CONTROLS[4].CnV = 1023 + 1;
}
else {
TPM0->CONTROLS[4].CnV = 1023*predkosc/100 ;
}
}
}
void driveForwardDistLeftTrack( int predkosc , int droga){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTA->PCOR = LEFT;
if(predkosc >= 100){
TPM0->CONTROLS[4].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[4].CnV = predkosc/100 * V_MOD;
}
FLAG_LIMIT_DIST = 1;
LIMIT = droga; // info dla enkodera
}
}
void driveForwardRightTrack( int predkosc){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTC->PCOR = RIGHT;
if(predkosc >= 100){
TPM0->CONTROLS[2].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[2].CnV = V_MOD*predkosc/100 ;
}
}
}
void driveForwardDistRightTrack( int predkosc , int droga){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTC->PCOR = RIGHT;
if(predkosc >= 100){
TPM0->CONTROLS[2].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[2].CnV = predkosc/100 * V_MOD;
}
FLAG_LIMIT_DIST = 1;
LIMIT = droga; // info dla enkodera
}
}
void stop(void){
TPM0->CONTROLS[2].CnV = 0; // stop RIGHT
TPM0->CONTROLS[4].CnV = 0; // stop LEFT
}
void driveReverseLeftTrack( int predkosc){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTA->PSOR = LEFT; // clear , set 0 mean forward
if(predkosc >= 100){
TPM0->CONTROLS[4].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[4].CnV = V_MOD *predkosc/100 ;
}
}
}
void driveReverseDistLeftTrack( int predkosc , int droga){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTA->PSOR = LEFT;
if(predkosc >= 100){
TPM0->CONTROLS[4].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[4].CnV = predkosc/100 * V_MOD;
}
FLAG_LIMIT_DIST = 1;
LIMIT = droga; // info dla enkodera
}
}
void driveReverseRightTrack( int predkosc){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTC->PSOR = RIGHT;
if(predkosc >= 100){
TPM0->CONTROLS[2].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[2].CnV = V_MOD*predkosc/100 ;
}
}
}
void driveReverseDistRightTrack( int predkosc , int droga){
ACTUAL_DISTANCE = 0;
if( predkosc >=0){
PTC->PSOR = RIGHT;
if(predkosc >= 100){
TPM0->CONTROLS[2].CnV = V_MOD + 1;
}
else {
TPM0->CONTROLS[2].CnV = predkosc/100 * V_MOD;
}
FLAG_LIMIT_DIST = 1;
LIMIT = droga; // info dla enkodera
}
}
void driveForward(int predkosc){
driveForwardLeftTrack(predkosc);
driveForwardRightTrack(predkosc);
}
void driveForwardDist( int predkosc , int droga){
driveForwardDistLeftTrack( predkosc , droga);
driveForwardDistRightTrack( predkosc , droga);
}
void driveReverse(int predkosc){
driveReverseLeftTrack(predkosc);
driveReverseRightTrack(predkosc);
}
void driveReverseDist( int predkosc , int droga){
driveReverseDistLeftTrack( predkosc , droga);
driveReverseDistRightTrack( predkosc , droga);
}
void acceleration( int oile){
// teraz jeszcze trzeba zrobic powolne przyspieszenie
//LEFT ENGINE
tmp = TPM0->CONTROLS[4].CnV * 100 / V_MOD ;
if(tmp + oile >= 100){
TPM0->CONTROLS[4].CnV = V_MOD + 1;
}
else if(tmp + oile <= 0 ){
TPM0->CONTROLS[4].CnV = 0;
}
else{
TPM0->CONTROLS[4].CnV = tmp + oile;
}
//RIGHT ENGINE
tmp = TPM0->CONTROLS[2].CnV / V_MOD * 100.0 ;
if(tmp + oile >= 100){
TPM0->CONTROLS[2].CnV = V_MOD + 1;
}
else if(tmp + oile <= 0 ){
TPM0->CONTROLS[2].CnV = 0;
}
else{
TPM0->CONTROLS[2].CnV = tmp + oile;
}
}
/////////////////////////////dorzucic sprawdzanie kompasem //////////////////////////////
void turnLeft( int kat, int predkosc ){
//tmp = kat/10 *
//droga = kat/10 * 2 ;
driveReverseDistLeftTrack(predkosc , kat/10 * 2 );
driveForwardDistRightTrack(predkosc , kat/10 * 2 ); // tu wystarczy wywolac funkcje bez zdefinowanej drogi
// poniewaz definicja LIMIT nastepuje w funkcji wczesniej
}
void turnRight( int kat , int predkosc){
driveReverseDistRightTrack(predkosc , kat/10 * 2 );
driveForwardDistLeftTrack(predkosc , kat/10 * 2 );// tu wystarczy wywolac funkcje bez zdefinowanej drogi
// poniewaz definicja LIMIT nastepuje w funkcji wczesniej
}