aboutsummaryrefslogtreecommitdiff
path: root/zumo/zumo.ino
blob: 9e7958113b08959f21c10277a83316102a1e9f0b (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
#include <Zumo32U4.h>
#include <Arduino.h>
#include <Wire.h>

#include "control.h"
#include "protocol.h"
#include "pid.h"

dui_state_t g_dui_target_state = {
	.steer = 0.0f,
	.Speed = 0.0f,
	.current_sign = DUI_SIGN_NONE,
	.speed_mod = 1.f,
};
dui_state_t g_dui_current_state = {
	.steer = 0.f,
	.Speed = 0.f,
	.current_sign = DUI_SIGN_NONE,
	.speed_mod = 1.f,
};

void setup() {
	pinMode(DUI_PINOUT_NICLA_TX, OUTPUT);
	pinMode(DUI_PINOUT_NICLA_RX, INPUT_PULLUP);
	Serial.begin(11500);
}

void loop() {
	static unsigned char cmd_old = 0x00;
	for (unsigned int i = 0; i < 1000; i++) {
		digitalWrite(DUI_PINOUT_NICLA_TX, LOW);
		unsigned char cmd = uart_read();
		if (cmd == 0x00) continue;
		if (cmd == cmd_old) handle_cmd(cmd, &g_dui_target_state);
		cmd_old = cmd;
	}
	digitalWrite(DUI_PINOUT_NICLA_TX, HIGH);

	apply_pid(&g_dui_target_state, &g_dui_current_state);
	g_dui_current_state.current_sign = g_dui_target_state.current_sign;

	apply_state(&g_dui_current_state);
}