summaryrefslogtreecommitdiffstats
path: root/include/main.h
blob: d0ee41b691bfb33068e6b7e694b5276e508a5702 (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
159
160
161
#ifndef MAIN_H_
#define MAIN_H_

#include <API.h>
#include <stdint.h>

typedef struct{
	int x;
	int y;
}vec2;

typedef enum {
	UP,
	DOWN,
	KEY_UP
} Button;

typedef struct {
	unsigned int num;
	struct Side {
		struct Group {
			Button l;
			Button r;
			Button u;
			Button d;
		} front, back;
		vec2 stick;
	} left, right;
} Controller;

typedef struct {
	enum type {
		DIGITAL,
		ANALOG,
		GYRO,
		ULTRASONIC
	} type;
	union data {
		Gyro gyro;
		Ultrasonic sonic;
	} data;
	unsigned int port;
	int value;
	int initial;
} Sensor;

typedef struct {
	bool        kill_req;
	bool		exiting;
	TaskCode    code;
	TaskHandle  handle;
	void       *param;
} Process;

#ifdef __cplusplus
extern "C" {
#endif

#define DEFAULT_TRPM trpm = 1850;
#define EXTRA_TRPM	 trpm = 1900;

#define PI 3.14159265L

#define LCD_PORT uart2

/**
 * Be sure that getIMEPort() matches these values (see sensor.c).
 */

enum MOTOR_MAP {
	CANNON_LEFT = 1,
	CANNON_RIGHT,
	LIFT_PUSHER,
	INTAKE_1,
	INTAKE_2,
	DRIVE_RIGHT,
	DRIVE_LEFT,
	LIFT_1,
	LIFT_2,
	LIFT_ROTATER
};

enum MOTOR_IME_MAP {
	DRIVE_RIGHT_IME = 0,
	DRIVE_LEFT_IME,
	LIFT_ROTATER_IME,
	LIFT_1_IME,
	LIFT_2_IME,
	CANNON_LEFT_IME,
	CANNON_RIGHT_IME
};

extern unsigned char MOTOR_USE_MAP[10];

#define motorTake(p,k)   MOTOR_USE_MAP[p-1] = k;
#define motorFree(p)     MOTOR_USE_MAP[p-1] = 0;

#define motorSetK(p,s,k) if(!MOTOR_USE_MAP[p-1] || MOTOR_USE_MAP[p-1] == k){ motorSet(p,s); }
#define motorSetN(p,s)   motorSetK(p,s,0)

#define motorSetBK(p,s,k,b) motorSetK(p,b.u ? s : b.d ? -s : 0,k)
#define motorSetBN(p,s,b)   motorSetN(p,b.u ? s : b.d ? -s : 0)

#define motorCopyK(p1,p2,k) motorSetK(p1,motorGet(p2),k)
#define motorCopyN(p1,p2)   motorSetN(p1,motorGet(p2))

int getIMEPort(unsigned int port);
int getIME(unsigned int port);
int getIMEVelocity(unsigned int port);

/**
 * Controller library functions
 */

#define keyUp(b)   (b == KEY_UP)
#define keyDown(b) (b == DOWN)

#define onKeyUp(b)   if(keyUp(b))
#define onKeyDown(b) if(keyDown(b))

void setEvent(Controller *c);

/**
 * Sensor library functions
 */

#define LIGHT_THRESH_DEFAULT 50
#define SONIC_THRESH_DEFAULT 5

#define initUltrasonic(p1,p2) initSensor((p2<<16)|p1,ULTRASONIC)
Sensor initSensor(uint32_t port,unsigned char type);

#define getSensor(s) (s.value)
int readSensor(Sensor *s);

#define diffSensor(s) 		(s.value - s.initial)
#define underSensor(s,t)	(diffSensor(s) < -t)
#define overSensor(s,t)		(diffSensor(s) > t)

/**
 * Process library functions
 */

#define taskInit(h,p) h = h ? h : taskCreate(h##Code,TASK_DEFAULT_STACK_SIZE,p,TASK_PRIORITY_DEFAULT)

/**
 * Main function declarations
 */

extern void softwareReset(void);

void autonomous();
void initializeIO();
void initialize();
void operatorControl();

#ifdef __cplusplus
}
#endif

#endif // MAIN_H_