-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoaoBot.c
156 lines (120 loc) · 2.71 KB
/
JoaoBot.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
#include "LS_defines.h"
#include "LS_ATmega328.h"
//#include <avr/io.h>
//#include "LS_i2c_master.h"
//#include "twi_master.h"
#include "i2cmaster.h"
#include "l3g4200d.h"
#include "adxl345.h"
#include "Motor.h"
#include "PID_v1.h"
#include <math.h>
#define SAD 0x69<<1
double ax = 0;
double ay = 0;
double az = 0;
double gx = 0;
double gy = 0;
double gz = 0;
volatile FloatType angle = 0;
volatile FloatType outmot = 0;
volatile FloatType setpoint = 0;
ISR(TIMER2_OVF_vect);
PidType PID;
volatile int init_calib = 0;
volatile double angle_off = 0;
int main() {
usartInit(57600);
usartEnableReceiver();
usartEnableTransmitter();
usartStdio();
setBit(DDRB, M1_DIR);
setBit(DDRD, M1_PWM);
setBit(DDRD, M2_DIR);
setBit(DDRD, M2_PWM);
clrBit(PORTB, M1_DIR);
clrBit(PORTD, M1_PWM);
clrBit(PORTD, M2_PWM);
clrBit(PORTD, M2_DIR);
timer0FastPWMMaxMode();
timer0ClockPrescaller64();
timer0OC0ANonInvertedMode();
timer0OC0BNonInvertedMode();
timer0SetCompareAValue(0);
timer0SetCompareBValue(0);
timer0DeactivateCompareAInterrupt();
timer0DeactivateCompareBInterrupt();
timer2ClockPrescaller256();
timer2NormalMode();
timer2OC2AOff();
timer2OC2BOff();
timer2ActivateOverflowInterrupt();
i2c_init();
adxl345_init();
l3g4200d_init();
l3g4200d_setoffset(0.11, -1.71, -0.46);
//l3g4200d_settemperatureref();
PID_init(&PID, 15,0.2,1, 0);
PID_SetMode(&PID, 1);
PID_SetOutputLimits(&PID, -255, 255);
PID.mySetpoint = 0;
motor1(0);
motor2(0);
// printf("\e[1;1H\e[2J");
// printf("STARTED!\r\n");
// _delay_ms(200);
// motor1(120);
// printf("Motor1 = 120\r\n");
// _delay_ms(500);
// motor1(-120);
// printf("Motor1 = -120\r\n");
// _delay_ms(500);
// motor1(120);
// printf("Motor1 = 120\r\n");
// _delay_ms(500);
// motor1(0);
// printf("Motor1 = 0\r\n");
sei();
while(1);
return 0;
}
ISR(TIMER2_OVF_vect) {
if (init_calib == 100) {
angle_off = angle;
init_calib++;
} else {
init_calib++;
}
l3g4200d_getdata(&gx,&gy,&gz);
adxl345_getdata(&ax, &ay, &az);
angle = (0.25)*(angle + gy*0.004096) + (0.75)*((asin(az)*180)/3.14159);
PID.myInput = angle;// - angle_off;
PID_Compute(&PID);
if (angle > 45 || angle < -45) {
motor1(0);
motor2(0);
} else {
// if (angle > 10) {
// motor1(-175);
// motor2(-175);
// } else if (angle < -10) {
// motor1(175);
// motor2(175);
// } else if (angle > 5) {
// motor1(-80);
// motor2(-80);
// } else if (angle < -5) {
// motor1(80);
// motor2(80);
// } else if (angle > -5 && angle < 5) {
// motor1(0);
// motor2(0);
// }
motor1(PID.myOutput);
motor2(PID.myOutput);
}
printf("\e[1;1H\e[2J");
printf("FUCKING ANGLE: %f\r\n", angle);
// printf("ANGLE w/ offset: %f\r\n", angle-angle_off);
printf("PID output: %f\r\n", PID.myOutput);
}