-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotors.c
82 lines (71 loc) · 2.71 KB
/
motors.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
#include "main.h"
#include "common.h"
#define MotorMin 3245
#define MotorMax 5825
#define MotorRange 2580
#define K 0.7071
OCOutput OCO={0.0,0.0,0.0,0.0};
int floatToInt(float v){
if(v>=0){
return(int)(v+0.5f);
}
else{
return(int)(v-0.5f);
}
}
void updateMotors(){
/*
OCO.OC1Output=ICD.throttleInput;
OCO.OC2Output=ICD.throttleInput;
OCO.OC3Output=ICD.throttleInput;
OCO.OC4Output=ICD.throttleInput;
*/
/*
OCO.OC1Output=MotorMin+MotorRange*ICO.throttle-ICO.pitchTarget+ICO.rollTarget+ICO.yawTarget;
OCO.OC2Output=MotorMin+MotorRange*ICO.throttle+ICO.pitchTarget+ICO.rollTarget-ICO.yawTarget;
OCO.OC3Output=MotorMin+MotorRange*ICO.throttle+ICO.pitchTarget-ICO.rollTarget+ICO.yawTarget;
OCO.OC4Output=MotorMin+MotorRange*ICO.throttle-ICO.pitchTarget-ICO.rollTarget-ICO.yawTarget;
*/
OCO.OC1Output=MotorMin+MotorRange*ICO.throttle;
OCO.OC2Output=MotorMin+MotorRange*ICO.throttle;
OCO.OC3Output=MotorMin+MotorRange*ICO.throttle;
OCO.OC4Output=MotorMin+MotorRange*ICO.throttle;
/*
OCO.OC1Output=K*PIDO.roll-K*PIDO.pitch+PIDO.yaw+MotorMin+MotorRange*ICO.throttle;
OCO.OC2Output=K*PIDO.roll+K*PIDO.pitch-PIDO.yaw+MotorMin+MotorRange*ICO.throttle;
OCO.OC3Output=-K*PIDO.roll+K*PIDO.pitch+PIDO.yaw+MotorMin+MotorRange*ICO.throttle;
OCO.OC4Output=-K*PIDO.roll-K*PIDO.pitch-PIDO.yaw+MotorMin+MotorRange*ICO.throttle;*/
if(OCO.OC1Output > MotorMax) {OCO.OC1Output = MotorMax;}
if(OCO.OC2Output > MotorMax) {OCO.OC2Output = MotorMax;}
if(OCO.OC3Output > MotorMax) {OCO.OC3Output = MotorMax;}
if(OCO.OC4Output > MotorMax) {OCO.OC4Output = MotorMax;}
if(OCO.OC1Output < MotorMin) {OCO.OC1Output = MotorMin;}
if(OCO.OC2Output < MotorMin) {OCO.OC2Output = MotorMin;}
if(OCO.OC3Output < MotorMin) {OCO.OC3Output = MotorMin;}
if(OCO.OC4Output < MotorMin) {OCO.OC4Output = MotorMin;}
OC1RS = ~(unsigned int)(OCO.OC1Output);
OC2RS = ~(unsigned int)(OCO.OC2Output);
OC3RS = ~(unsigned int)(OCO.OC3Output);
OC4RS = ~(unsigned int)(OCO.OC4Output);
//printf("\n***OC1RS : %d", OC1RS);
}
void calibrateESCEndpoints(){
int x = 0;
for (x=0; x<400; x++){
OC1RS = ~MotorMax;
OC2RS = ~MotorMax;
OC3RS = ~MotorMax;
OC4RS = ~MotorMax;
__delay_ms(2.5);
LATAbits.LATA0 = !LATAbits.LATA0;
}
for (x=0; x<400; x++){
OC1RS = ~MotorMin;
OC2RS = ~MotorMin;
OC3RS = ~MotorMin;
OC4RS = ~MotorMin;
__delay_ms(2.5);
LATAbits.LATA0 = !LATAbits.LATA0;
}
printf("\nESC endpoints calibrated");
}