-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
160 lines (138 loc) · 3.03 KB
/
main.c
File metadata and controls
160 lines (138 loc) · 3.03 KB
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
/*
* Quadcopter flight controller software.
*
* @author Matas Kairaitis
* @author Marko Lazic
* @version 2016-12-12
*/
#include <stdio.h>
#include <pic32mx.h>
#include "timer.h"
#include "gyro.h"
#include "display.h"
#include "pid.h"
unsigned int calibTime = 0;
/*
* Represents a motor.
*/
typedef struct {
int pulse;
int turnOff;
} motor;
/*
* Initializes the required I/O pins.
*/
void pinsInit() {
TRISECLR = 0xF0;
TRISESET = 0x02;
PORTECLR = 2;
}
/*
* Sets output of all motor pins to HIGH.
*/
void setMotorOutputHigh() {
PORTESET = 0xF0;
}
/*
* Sets output of all motor pins to LOW.
*/
void setMotorOutputLow() {
PORTECLR = 0xF0;
}
/*
* Pulses all motors with the given pulse.
*/
void pulseAllMotors(int pulse) {
setMotorOutputHigh();
delayMicroseconds(pulse);
setMotorOutputLow();
}
/*
* Sorts motor structs in ascending order.
*/
void sort(motor list[], int n) {
int c, d;
motor t;
for (c = 0 ; c < ( n - 1 ); c++) {
for (d = 0 ; d < n - c - 1; d++) {
if (list[d].pulse > list[d + 1].pulse) {
t = list[d];
list[d] = list[d + 1];
list[d + 1] = t;
}
}
}
}
/*
* Main loop of the program.
*/
void loop() {
unsigned int startTime = getTime();
updateGyroValues();
pidCalculate(getGyroX() - getXCalib(), getGyroY() - getYCalib(), getGyroZ() - getZCalib());
char buf1[32];
char buf2[32];
char buf3[32];
char buf4[32];
itoa(pidGetPulse1(), buf1, 10);
itoa(pidGetPulse2(), buf2, 10);
itoa(pidGetPulse3(), buf3, 10);
itoa(pidGetPulse4(), buf4, 10);
/*
itoa(getGyroX(), buf1, 10);
itoa(getGyroY(), buf2, 10);
itoa(getGyroZ(), buf3, 10);
itoa(getGyroX(), buf4, 10);
*/
display_string(0, buf1);
display_string(1, buf2);
display_string(2, buf3);
display_string(3, buf4);
display_update();
motor motor1 = {pidGetPulse1(), 1 << 4};
motor motor2 = {pidGetPulse2(), 1 << 5};
motor motor3 = {pidGetPulse3(), 1 << 6};
motor motor4 = {pidGetPulse4(), 1 << 7};
motor motors[4];
motors[0] = motor1;
motors[1] = motor2;
motors[2] = motor3;
motors[3] = motor4;
sort(motors, 4);
// Pulse motors
if (getTime() < 5000000 + calibTime) {
pulseAllMotors(2000);
} else if (getTime() < 7000000 + calibTime) {
pulseAllMotors(1000);
} else {
setMotorOutputHigh();
delayMicroseconds(motors[0].pulse);
PORTECLR = motors[0].turnOff;
delayMicroseconds(motors[1].pulse - motors[0].pulse);
PORTECLR = motors[1].turnOff;
delayMicroseconds(motors[2].pulse - motors[1].pulse);
PORTECLR = motors[2].turnOff;
delayMicroseconds(motors[3].pulse - motors[2].pulse);
PORTECLR = motors[3].turnOff;
}
unsigned int calculationTime = getTime() - startTime;
delayMicroseconds(40000 - calculationTime);
}
/*
* Entry point of the program.
*/
int main() {
gyroBegin();
pinsInit();
timerBegin();
display_init();
asm volatile("ei");
unsigned int calibStart = getTime();
gyroCalibration();
display_string(0, "START");
display_update();
calibTime = getTime() - calibStart;
while (1) {
loop();
}
}