summaryrefslogtreecommitdiff
path: root/main.cpp
blob: 911e76008d8424792c918ef2f21664a039193bdb (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
#include "thread.h"
#include "usbserial.h"
#include "itg3200.h"
#include "bma150.h"
#include "ak8975.h"

#include <ch.h>
#include <hal.h>

class LEDThread : public BaseThread<LEDThread, 128> {
	public:
		noreturn_t thread_main() {
			while (1) {
				palClearPad(GPIOA, 5);
				chThdSleepMilliseconds(500);
				palSetPad(GPIOA, 5);
				chThdSleepMilliseconds(500);
			}
		}
};

LEDThread led_thread;

class USBThread : public BaseThread<USBThread, 256> {
	private:
		typedef enum {W_S, W_N, W_V} w_s_t;
	
	public:
		USBSerial* usbs;
		
		uint8_t data[9];
		
		noreturn_t thread_main() {
			for(int i = 0; i < 9; i++) {
				data[i] = 0;
			}
			
			w_s_t w_s = W_S;
			uint8_t w_n = 0;
			
			while(1) {
				size_t buffer = usbs->getc();
				if(buffer >= 0 && buffer < 256) {
					if(w_s == W_S && buffer == 'S') {
						w_s = W_N;
					} else if(w_s == W_N && buffer >= '1' && buffer <= '9') {
						w_s = W_V;
						w_n = buffer - '1';
					} else if(w_s == W_V) {
						w_s = W_S;
						data[w_n] = buffer;
					} else {
						w_s = W_S;
					}
				}
			}
		}
};

USBThread usb_thread;
USBSerial usbs;

#include "foo.h"

class I2CThread : public BaseThread<I2CThread, 256> {
	public:
		ITG3200 gyro;
		BMA150 acc;
		AK8975 magn;
		
		int16_t x, y, z;
		
		noreturn_t thread_main() {
			I2CSensor::enable_bus();
			
			gyro.init();
			acc.init();
			magn.init();
			
			while (1) {
				gyro.update();
				acc.update();
				magn.update();
				x = gyro.x;
				y = gyro.y;
				z = gyro.z;
				
				
				
				usbprintf(usbs, "%6d, %6d, %6d\r\n", x, y, z);
				
				chThdSleepMilliseconds(100);
			}
		}
};

I2CThread i2c_thread;

void foo(PWMDriver*) {
	
	int16_t thrust, pitch, roll, yaw;
	
	pitch = roll = 0;
	
	uint8_t shift = 8 - (usb_thread.data[8] >> 3);
	
	thrust = usb_thread.data[0] * 10;
	
	yaw = i2c_thread.z >> shift;
	
	
	pwmEnableChannel(&PWMD2, 0, 1000 + thrust + pitch + roll - yaw);
	pwmEnableChannel(&PWMD2, 1, 1000 + thrust + pitch - roll + yaw);
	pwmEnableChannel(&PWMD2, 2, 1000 + thrust - pitch + roll + yaw);
	pwmEnableChannel(&PWMD2, 3, 1000 + thrust - pitch - roll - yaw);
}

static PWMConfig pwmcfg = {
	1000000,
	1000000 / 50,
	foo,
	{
		{PWM_OUTPUT_ACTIVE_HIGH, NULL},
		{PWM_OUTPUT_ACTIVE_HIGH, NULL},
		{PWM_OUTPUT_ACTIVE_HIGH, NULL},
		{PWM_OUTPUT_ACTIVE_HIGH, NULL}
	},
	0
};

int main(void) {
	halInit();
	chSysInit();
	
	led_thread.start();
	
	usbs.init();
	
	i2c_thread.start();
	
	pwmStart(&PWMD2, &pwmcfg);
	palSetPadMode(GPIOA, 0, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
	palSetPadMode(GPIOA, 1, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
	palSetPadMode(GPIOA, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
	palSetPadMode(GPIOA, 3, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
	
	pwmEnableChannel(&PWMD2, 0, 1000);
	pwmEnableChannel(&PWMD2, 1, 1000);
	pwmEnableChannel(&PWMD2, 2, 1000);
	pwmEnableChannel(&PWMD2, 3, 1000);
	
	usb_thread.usbs = &usbs;
	usb_thread.start();
	
	while (1) {
		chThdSleepMilliseconds(1000);
	}
}