summaryrefslogtreecommitdiffstats
path: root/src/actuators.c
blob: dcb64d0365c3c576da0ea8ba01eb3f72aabb17a7 (plain) (blame)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/******************************************************************************
 *	Copyright (C) 2018	Colomar Andrés, Alejandro		      *
 *	Copyright (C) 2018	García Pedroche, Francisco Javier	      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/

/**
 *	@file		actuators.c
 *	@author		Colomar Andrés, Alejandro
 *	@author		García Pedroche, Francisco Javier
 *	@copyright	GPL-2.0-only
 *	@date		2018/dec/26
 *	@brief		Actuators
 *		Set the actuators of the plane to the position received by CAN
 */


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
	#include <stdbool.h>
	#include <stddef.h>
	#include <stdint.h>

	#include "stm32l4xx_hal.h"

	#include "libalx/alx_math.h"

	#include "stm32l4-modules/button.h"
	#include "stm32l4-modules/can.h"
	#include "stm32l4-modules/delay.h"
	#include "stm32l4-modules/errors.h"
	#include "stm32l4-modules/led.h"
	#include "stm32l4-modules/tim.h"
	#include "stm32l4-modules/dev/servo.h"

	#include "actuators.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#define ACT_REFRESH_PERIOD_US	(20000u)
#define PROJECT_ACT_TASKING	(PROJECT_ACT_IT)


/******************************************************************************
 ******* enums ****************************************************************
 ******************************************************************************/
	enum	Project_Act_Tasking {
		PROJECT_ACT_DEL,
		PROJECT_ACT_IT,
		PROJECT_ACT_EV
	};


/******************************************************************************
 ******* structs **************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* variables ************************************************************
 ******************************************************************************/
/* Volatile ------------------------------------------------------------------*/
/* Global --------------------------------------------------------------------*/
/* Static --------------------------------------------------------------------*/
static	bool	init_pending	= true;
static	float	pitch;
static	float	roll;
static	float	yaw;


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
static	int	modules_init		(void);
static	int	modules_deinit		(void);
static	int	proc_actuators_it	(void);
static	int	proc_actuators_ev	(void);
static	int	proc_actuators_delay	(void);
static	int	proc_ref_read		(void);
static	int	proc_actuators_set	(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
	/**
	 * @brief	Initialize actuators process
	 * @return	Error
	 * @note	Sets global variable 'prj_error'
	 */
int	proc_actuators_init	(void)
{

	if (!init_pending)
		return	ERROR_OK;

	if (modules_init())
		goto err;

	init_pending	= false;

	return	ERROR_OK;


err:
	return	ERROR_NOK;
}

	/**
	 * @brief	Deinitialize actuators process
	 * @return	Error
	 * @note	Sets global variable 'prj_error'
	 */
int	proc_actuators_deinit	(void)
{
	int	status;

	if (init_pending)
		return	status;

	init_pending	= true;

	status	= ERROR_OK;

	if (modules_deinit())
		status	= ERROR_NOK;

	return	status;
}

	/**
	 * @brief	Run actuators process
	 * @return	Error
	 * @note	Sets global variable 'prj_error'
	 */
int	proc_actuators	(void)
{

	if (init_pending) {
		if (proc_actuators_init())
			return	ERROR_NOK;
	}

	switch (PROJECT_ACT_TASKING) {
	case PROJECT_ACT_DEL:
		return	proc_actuators_delay();
	case PROJECT_ACT_IT:
		return	proc_actuators_it();
	case PROJECT_ACT_EV:
		return	proc_actuators_ev();
	default:
		return	ERROR_NOK;
	}
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static	int	modules_init		(void)
{

	led_init();
	button_init();
	if (delay_us_init())
		goto err_delay;

	if (can_init())
		goto err_can;
	if (servo_init())
		goto err_servo;

	if (PROJECT_ACT_TASKING != PROJECT_ACT_DEL) {
		if (tim_it_init(ACT_REFRESH_PERIOD_US))
			goto err_tim;
	}

	return	ERROR_OK;


err_tim:
	servo_deinit();
err_servo:
	can_deinit();
err_can:
	delay_us_deinit();
err_delay:
	button_deinit();
	led_deinit();

	return	ERROR_NOK;
}

static	int	modules_deinit		(void)
{
	int	status;

	status	= ERROR_OK;

	if (servo_deinit())
		status	= ERROR_NOK;
	if (can_deinit())
		status	= ERROR_NOK;
	if (tim_it_deinit())
		status	= ERROR_NOK;
	if (delay_us_deinit())
		status	= ERROR_NOK;
	button_deinit();
	led_deinit();

	return	status;
}

static	int	proc_actuators_it	(void)
{

	while (true) {
		__WFI();

		if (*tim_it_timx_interrupt_ptr) {
			if (!proc_ref_read()) {
				if (proc_actuators_set())
					return	ERROR_NOK;
			}

			*tim_it_timx_interrupt_ptr	= false;
		} else if (button_interrupt) {
			led_toggle();

			button_interrupt	= false;
		}
	}
}

static	int	proc_actuators_ev	(void)
{

	while (true) {
		__WFE();

		if (*tim_it_timx_interrupt_ptr) {
			if (!proc_ref_read()) {
				if (proc_actuators_set())
					return	ERROR_NOK;
			}

			*tim_it_timx_interrupt_ptr	= false;
		} else if (button_interrupt) {
			led_toggle();

			button_interrupt	= false;
		}
	}
}

static	int	proc_actuators_delay	(void)
{

	while (true) {
		delay_us(ACT_REFRESH_PERIOD_US);

		if (!proc_ref_read()) {
			if (proc_actuators_set())
				return	ERROR_NOK;
		}
	}
}

static	int	proc_ref_read		(void)
{
	int8_t	plane_pos [CAN_DATA_LEN];

	if (can_msg_read((uint8_t *)plane_pos))
		return	ERROR_NOK;

	pitch	= plane_pos[0];
	roll	= plane_pos[1];
	yaw	= plane_pos[2];

	return	ERROR_OK;
}

static	int	proc_actuators_set	(void)
{
	float	tmp;

	tmp	= alx_scale_linear_f(pitch, -40, 40, -90, 90);
	if (servo_position_set(SERVO_S1, tmp))
		return	ERROR_NOK;

	tmp	= alx_scale_linear_f(roll, -35, 35, -90, -20);
	if (servo_position_set(SERVO_S2, tmp))
		return	ERROR_NOK;

	if (yaw > 0) {
		if (servo_position_set(SERVO_S3, 90))
			return	ERROR_NOK;

		tmp	= alx_scale_linear_f(yaw, 0, 10, -90, 90);
		if (servo_position_set(SERVO_S4, tmp))
			return	ERROR_NOK;
	} else {
		tmp	= alx_scale_linear_f(yaw, -10, 0, -90, 90);
		if (servo_position_set(SERVO_S3, tmp))
			return	ERROR_NOK;

		if (servo_position_set(SERVO_S4, -90))
			return	ERROR_NOK;
	}

	return	ERROR_OK;
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/