-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
196 lines (166 loc) · 3.68 KB
/
main.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
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
#include "MTT/includes.h"
volatile bool reset=0;
// select boxes based on uart value
volatile bool
red_ob =0,
yellow_ob =0,
green_ob =0;
// inputs from baumer
volatile bool
baumer0=0,
baumer1=0,
baumer2=0;
// counts from baumer trigger to actuation
volatile int
piston0_buffer=60000,
piston1_buffer=60000,
piston2_buffer=60000;
// select which piston must be actuated
volatile bool
piston0=0,
piston1=0,
piston2=0;
// counts when actuated
volatile int
deactuate0 = 500000,
deactuate1 = 500000,
deactuate2 = 500000;
uint32_t uart_base;
uint32_t uart;
volatile double box_val;
volatile double *box_val_data;
volatile int boxval=0;
void box_handler(void)
{
UARTIntClear(uart_base, UART_INT_RX);
while(UARTCharsAvail(uart_base))
{
int temp_data = UARTCharGetNonBlocking(uart_base);
boxval = temp_data;
red_ob = (temp_data==1);
yellow_ob = (temp_data==2);
green_ob = (temp_data==3);
if (boxval >= 0 && boxval <= 3) {
uart_init_rxint(uart, 9600, box_handler);
uart_base = uart;
}
else {
boxval = 0;
//throw error
}
}
}
void conveyer_reset()
{
piston0 = piston1 = piston2 = 0;
baumer0 = baumer1 = baumer2 = 0;
red_ob = yellow_ob = green_ob = 0;
deactuate0 = deactuate1 = deactuate2 = 500000;
piston0_buffer = piston1_buffer = piston2_buffer = 100000;
}
void box(uint32_t uart_box, volatile double *box_val_var)
{
uart=uart_box;
box_val_data=box_val_var;
uart_init_rxint(uart, 9600, box_handler);
uart_base=uart;
}
void box_init(uint32_t uart_box)
{
box(uart_box, &box_val);
UARTFIFODisable(uart_box);
}
void baumer_set()
{
baumer0 = (GPIO_PORTM_DATA_R &0x40)|baumer0;
baumer1 = ((~GPIO_PORTM_DATA_R) &0x20)|baumer1;
baumer2 = (GPIO_PORTM_DATA_R &0x80)|baumer2;
}
void piston_select()
{
if (red_ob)
{
if (baumer0)
{
piston0_buffer--;
if (!piston0_buffer)
{
conveyer_reset();
piston0 = 1;
}
}
}
if (yellow_ob)
{
if (baumer1)
{
piston1_buffer--;
if (!piston1_buffer)
{
conveyer_reset();
piston1 = 1;
}
}
}
if (green_ob)
{
if (baumer2)
{
piston2_buffer--;
if (!piston2_buffer)
{
conveyer_reset();
piston2 = 1;
}
}
}
}
void piston_control()
{
if (piston0)
{
GPIO_PORTM_DATA_R |= 0X08;
deactuate0--;
if (!deactuate0)
{
GPIO_PORTM_DATA_R &= (~0X08);
conveyer_reset();
} }
if (piston1)
{
GPIO_PORTM_DATA_R &= (~0X01);
deactuate1--;
if (!deactuate1)
{
GPIO_PORTM_DATA_R |= 0X01;
conveyer_reset();
} }
if (piston2)
{
GPIO_PORTM_DATA_R |= 0X04;
deactuate2--;
if (!deactuate2)
{
GPIO_PORTM_DATA_R &= (~0X04);
conveyer_reset();
}
}
}
int main()
{
System80Mhz();
int_disable();
input (portm_gpio, 0XF0);
output(portm_gpio, 0x0F);
input (portg_gpio, 0X07);
box_init(uart4);
GPIO_PORTM_DATA_R &=0;
GPIO_PORTM_DATA_R |=1;
int_enable();
while(1)
{
baumer_set();
piston_select();
piston_control();
}
}