-
Notifications
You must be signed in to change notification settings - Fork 0
/
yawDetection.c
executable file
·169 lines (142 loc) · 4.63 KB
/
yawDetection.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
/*
* yawDetection.c
* Function for detection of yaw pin changes and quadrature encoding to gain value in degrees
* Created on: 28/05/2018
* Author: tjl54
*/
// Includes
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "yawDetection.h"
// *** globals
static int32_t yaw_degrees; // Yaw calculation in degrees
volatile static int32_t yaw; // Raw, unconverted yaw value
static int32_t state_yaw = 0; // Tracks current state of the yaw calculation
static int32_t prev_state_yaw = 0; // Tracks previous state of yaw calculation
volatile static int16_t yawRef = 1; // Stores whether the ref signal has been reached
volatile static int16_t g_trigger = 0;
//*************************************************************************
// ISR and Yaw Quadrature encoding
//*************************************************************************
void
handleYaw (void)
{
int32_t PinA;
int32_t PinB;
// Clear the interrupt (documentation recommends doing this early)
GPIOIntClear (GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Reads pin values
PinA = GPIOPinRead (GPIO_PORTB_BASE, GPIO_PIN_0);
PinB = GPIOPinRead (GPIO_PORTB_BASE, GPIO_PIN_1);
// Yaw States
if (!PinA) {
if (!PinB) {
state_yaw = 1; //hold
}
else if (PinB) {
state_yaw = 2; //clockwise
}
}
if (PinA) {
if (PinB) {
state_yaw = 3; //high
}
else if (!PinB) {
state_yaw = 4; //counterclockwise
}
}
// Detects direction moving and increments/decrements yaw
if (prev_state_yaw == 1 && state_yaw == 2) {
yaw++;
}
if (prev_state_yaw == 2 && state_yaw == 3) {
yaw++;
}
if (prev_state_yaw == 3 && state_yaw == 4) {
yaw++;
}
if (prev_state_yaw == 4 && state_yaw == 1) {
yaw++;
}
if (prev_state_yaw == 4 && state_yaw == 3) {
yaw--;
}
if (prev_state_yaw == 3 && state_yaw == 2) {
yaw--;
}
if (prev_state_yaw == 2 && state_yaw == 1) {
yaw--;
}
if (prev_state_yaw == 1 && state_yaw == 4) {
yaw--;
}
prev_state_yaw = state_yaw;
// Sets yaw_degrees
yaw_degrees = (yaw * 360) / 448;
}
// Interrupt handler for yaw interrupt
void yawRefHandler (void)
{
// detect interrupt
GPIOIntClear(GPIO_PORTC_BASE, GPIO_PIN_4);
yawRef = 0;
yaw = 1;
if (!yawRef && g_trigger == 0)
{
yaw = 0;
g_trigger = 1;
}
}
//*************************************************************************
// Yaw Port & Pin Configuration
//*************************************************************************
void
initYaw(void)
{
// Pin Setup
// Enable the GPIOB peripheral for configuration and use
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
// Set up the specific port pin as medium strength current & pull-down config
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
// Set data direction register as input
GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_IN);
// Interrupt Setup
// Register interrupt handler on port B
GPIOIntRegister(GPIO_PORTB_BASE, handleYaw);
// Configure pins to be triggered on BOTH edges
GPIOIntTypeSet(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_BOTH_EDGES);
// Enable interrupts on the pins
GPIOIntEnable(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Enable interrupts on port B
IntEnable(INT_GPIOB);
}
//*********************************************************
// Yaw Reference initialization
//*********************************************************
void initRef (void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);
GPIOPadConfigSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_STRENGTH_2MA,
GPIO_PIN_TYPE_STD_WPU);
GPIODirModeSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_DIR_MODE_IN);
GPIOIntTypeSet(GPIO_PORTC_BASE, GPIO_PIN_4, GPIO_FALLING_EDGE);
GPIOIntRegister(GPIO_PORTC_BASE, yawRefHandler);
GPIOIntEnable(GPIO_PORTC_BASE, GPIO_PIN_4);
IntEnable(INT_GPIOC);
}
// Return calculated yaw value
int getYaw(void)
{
return yaw_degrees;
}
// Return boolean value storing state of yaw ref
int checkYawRef(void)
{
return (yawRef);
}