-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkybot_servos.c
169 lines (147 loc) · 6.7 KB
/
Skybot_servos.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
157
158
159
160
161
162
163
164
165
166
167
168
169
/* ************************************************************************** */
/* UNIVERSIDAD DE MALAGA DEPARTAMENTO DE TECNOLOGIA ELECTRONICA */
/* http://www.uma.es http://www.dte.uma.es */
/* ========================================================================== */
/* PROGRAMA : SkyBotFirmware */
/* VERSION : 1.0 */
/* TARGET : Kit TIVA Launchpad IDE CCSv8 */
/* RECURSOS : */
/* AUTOR : Ignacio Herrero Reder & Michele La Malva Moreno */
/* ************************************************************************** */
#include <stdint.h>
#include <stdbool.h>
// Librerias que se incluyen tipicamente para configuracion de perifericos y pinout
#include "inc/hw_memmap.h"
#include "driverlib/pin_map.h"
// Libreria de control del sistema
#include "driverlib/sysctl.h"
// Incluir librerias de periférico y otros que se vaya a usar para control PWM y gestion de botones
#include "driverlib/gpio.h"
#include "driverlib/pwm.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "event_groups.h"
#include "Skybot_servos.h"
uint32_t ui32Period, ui32DutyCycle[2];
int direction[2];
extern EventGroupHandle_t TickServoDone;
extern QueueHandle_t QueueServoTicksRequest[2];
extern QueueHandle_t QueueServoTicksDone[2];
extern QueueHandle_t QueueServoSpeed[2];
void acelerar_robot(int izquierda,int derecha){
acelerar_motor(MOTOR_DERECHO,derecha);
acelerar_motor(MOTOR_IZQUIERDO,izquierda);
}
void acelerar_velocidad(int izquierda,int derecha){
xQueueOverwrite(QueueServoSpeed[MOTOR_IZQUIERDO],&izquierda);
xQueueOverwrite(QueueServoSpeed[MOTOR_DERECHO],&derecha);
}
void acelerar_motor(int motor, int speed){
if(speed > 100) speed = 100;
else if(speed < -100) speed = -100;
if(motor == MOTOR_DERECHO){
ui32DutyCycle[MOTOR_DERECHO] = SPEED_TICK_RIGHT(speed) ;
direction[MOTOR_DERECHO] = (speed>=0) ? 1 : -1;
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6, ui32DutyCycle[MOTOR_DERECHO] );
}else{
ui32DutyCycle[MOTOR_IZQUIERDO] = SPEED_TICK_LEFT(speed) ;
direction[MOTOR_IZQUIERDO] = (speed>=0) ? 1 : -1;
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7, ui32DutyCycle[MOTOR_IZQUIERDO] );
}
}
bool motor_stopped(int motor){
return (ui32DutyCycle[motor] == STOPCOUNT);
}
void mover_robot(int distancia){
if(distancia != 0){
int ticks = CM_TO_TICK(distancia);
xEventGroupClearBits(TickServoDone,0b11);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_DERECHO],&ticks);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_IZQUIERDO],&ticks);
xEventGroupWaitBits(TickServoDone,0b11,pdTRUE,pdTRUE,portMAX_DELAY);
int TicksDone = 0;
while(abs(TicksDone) < abs(ticks)){ xQueuePeek(QueueServoTicksDone[MOTOR_DERECHO],&TicksDone,portMAX_DELAY); }
TicksDone = 0;
while(abs(TicksDone) < abs(ticks)){ xQueuePeek(QueueServoTicksDone[MOTOR_IZQUIERDO],&TicksDone,portMAX_DELAY); }
}
}
void mover_motor(int motor, int distancia){
if(distancia != 0){
int ticks = CM_TO_TICK(distancia);
xQueueOverwrite(QueueServoTicksRequest[motor],&ticks);
int TicksDone = 0;
while(TicksDone < ticks){ xQueuePeek(QueueServoTicksDone[motor],&TicksDone,portMAX_DELAY); }
}
}
void mover_motor_IT(int motor, int distancia){
if(distancia != 0){
int ticks = CM_TO_TICK(distancia);
xQueueOverwrite(QueueServoTicksRequest[motor],&ticks);
}
}
void girar_robot(int grados){
if(grados != 0){
int ticks_left = DEGREES_TO_TICK(grados);
int ticks_right = -ticks_left;
xEventGroupClearBits(TickServoDone,0b11);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_DERECHO],&ticks_right);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_IZQUIERDO],&ticks_left);
xEventGroupWaitBits(TickServoDone,0b11,pdTRUE,pdTRUE,portMAX_DELAY);
int TicksDone = 0;
while(abs(TicksDone) < abs(ticks_right)){ xQueuePeek(QueueServoTicksDone[MOTOR_DERECHO],&TicksDone,portMAX_DELAY);}
TicksDone = 0;
while(abs(TicksDone) < abs(ticks_left)){ xQueuePeek(QueueServoTicksDone[MOTOR_IZQUIERDO],&TicksDone,portMAX_DELAY); }
}
}
void mover_robot_IT(int distancia){
if(distancia != 0){
int ticks = CM_TO_TICK(distancia);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_DERECHO],&ticks);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_IZQUIERDO],&ticks);
}
}
void girar_robot_IT(int grados){
if(grados != 0){
int ticks_left = DEGREES_TO_TICK(grados);
int ticks_right = -ticks_left;
xQueueOverwrite(QueueServoTicksRequest[MOTOR_DERECHO],&ticks_right);
xQueueOverwrite(QueueServoTicksRequest[MOTOR_IZQUIERDO],&ticks_left);
}
}
void configServos_init(void){
//Configure PWM Options
//Configure PWM Clock to match system
SysCtlPWMClockSet(SYSCTL_PWMDIV_16);
//Inicializa el puerto F (Para puertos PWM)
//The Tiva Launchpad has two modules (0 and 1). Module 1 covers the PF2 and PF3
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);
SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_PWM1);
// Configuramos PF2 y PF3 como PWM
// Desactivado solo para tarea de LEDS
GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
// Comentar para tarea final
// ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
//PWM_GEN_3 Covers M1PWM6 and M1PWM7 See page 207 4/11/13 DriverLib doc
GPIOPinConfigure(GPIO_PF2_M1PWM6);
GPIOPinConfigure(GPIO_PF3_M1PWM7);
PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
// Ponemos valores personalizados
ui32Period = FREQ_PWM;
// Motor Derecho
ui32DutyCycle[MOTOR_DERECHO] = STOPCOUNT;
// Motor Izquierdo
ui32DutyCycle[MOTOR_IZQUIERDO] = STOPCOUNT;
//Set the Period (expressed in clock ticks)
PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, ui32Period);
// Enable the PWM generator
PWMGenEnable(PWM1_BASE, PWM_GEN_3);
// Turn on the Output pins
PWMOutputState(PWM1_BASE, PWM_OUT_6_BIT|PWM_OUT_7_BIT, true);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,ui32DutyCycle[MOTOR_DERECHO]);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,ui32DutyCycle[MOTOR_IZQUIERDO]);
direction[MOTOR_DERECHO] = 1;
direction[MOTOR_IZQUIERDO] = 1;
}