-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSelfParkingCars.cp
298 lines (228 loc) · 5.98 KB
/
SelfParkingCars.cp
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
#line 1 "C:/Users/Jelena/Desktop/SelfParkingCars/SelfParkingCars.c"
#line 1 "c:/users/jelena/desktop/selfparkingcars/selfparkingcars.h"
#line 22 "c:/users/jelena/desktop/selfparkingcars/selfparkingcars.h"
static int pwmDuty[4];
static unsigned int pwmPeriod[4];
static int wheelInterruptCount[2]={ 200, 200 };
static int stepsLeft = 0, stepsRight = 0;
static const unsigned int sensorPositions[15] = { 2604, 2929, 3255, 3581, 3907, 4232, 4558, 4883,
5208, 5534, 5859, 6185, 6510, 6836, 7161 };
typedef void (*DutyChangeFunctionPtr)(unsigned int);
static void PWM_TIM4_Set_Duty_Wrapper(unsigned int ratio) {
PWM_TIM4_Set_Duty(ratio, _PWM_INVERTED, _PWM_CHANNEL1);
}
static void PWM_TIM9_Set_Duty_Wrapper(unsigned int ratio) {
PWM_TIM9_Set_Duty(ratio, _PWM_INVERTED, _PWM_CHANNEL1);
}
static void PWM_TIM10_Set_Duty_Wrapper(unsigned int ratio) {
PWM_TIM10_Set_Duty(ratio, _PWM_NON_INVERTED, _PWM_CHANNEL1);
}
static void PWM_TIM11_Set_Duty_Wrapper(unsigned int ratio) {
PWM_TIM11_Set_Duty(ratio, _PWM_NON_INVERTED, _PWM_CHANNEL1);
}
static DutyChangeFunctionPtr ChangeDuty[4] = { &PWM_TIM4_Set_Duty_Wrapper, &PWM_TIM9_Set_Duty_Wrapper,
&PWM_TIM10_Set_Duty_Wrapper, &PWM_TIM11_Set_Duty_Wrapper };
#line 1 "c:/users/jelena/desktop/selfparkingcars/sensors.h"
void InitializeSensors();
void AlignRightSensors();
void RotateFrontSensorFront();
void TriggerFrontSensorMeasurement();
void TriggerBackSensorMeasurement();
double GetFrontSensorDistance();
double GetBackSensorDistance();
#line 9 "C:/Users/Jelena/Desktop/SelfParkingCars/SelfParkingCars.c"
sbit SpinDirectionLeftWheel at ODR6_GPIOA_ODR_bit;
sbit SpinDirectionRightWheel at ODR7_GPIOA_ODR_bit;
volatile int startCounting = 0;
volatile int leftWheelStopped = 0, rightWheelStopped = 0;
void InitExternIntRisingEdge_PC1_PC2()
{
GPIO_Digital_Input(&GPIOC_BASE, _GPIO_PINMASK_1);
GPIO_Digital_Input(&GPIOC_BASE, _GPIO_PINMASK_2);
SYSCFGEN_bit = 1;
SYSCFG_EXTICR1 |= 0x00000020 | 0x00000200 ;
EXTI_RTSR |= 0x00000002 | 0x00000004 ;
EXTI_FTSR |= 0x00000002 | 0x00000004 ;
EXTI_IMR |= 0x00000002 | 0x00000004 ;
NVIC_IntEnable(IVT_INT_EXTI1);
NVIC_IntEnable(IVT_INT_EXTI2);
}
void UpdateCurrentDuty(int,int);
void RightWheel_Interrupt() iv IVT_INT_EXTI2 ics ICS_AUTO
{ EXTI_PR.B2 = 1;
if (!startCounting) return;
--wheelInterruptCount[ 0 ];
if (wheelInterruptCount[ 0 ] < 0)
{
ChangeDuty[ 0 ](0);
rightWheelStopped = 1;
}
}
void LeftWheel_Interrupt() iv IVT_INT_EXTI1 ics ICS_AUTO
{ EXTI_PR.B1 = 1;
if (!startCounting) return;
--wheelInterruptCount[ 1 ];
if (wheelInterruptCount[ 1 ] < 0)
{
ChangeDuty[ 1 ](0);
leftWheelStopped = 1;
}
}
void InitLeftWheelPWM_Timer4_CH1_PB6()
{
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_6);
SpinDirectionLeftWheel = 1 ;
pwmPeriod[ 0 ] = PWM_TIM4_Init(50000) ;
pwmDuty[ 0 ] = pwmPeriod[ 0 ]- 650;
ChangeDuty[ 0 ](pwmDuty[ 0 ]);
PWM_TIM4_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM4_CH1_PB6);
}
void InitRightWheelPWM_Timer9_CH1_PE5()
{
GPIO_Digital_Output(&GPIOA_BASE, _GPIO_PINMASK_7);
SpinDirectionRightWheel = 1 ;
pwmPeriod[ 1 ] = PWM_TIM9_Init(25000);
pwmDuty[ 1 ] = pwmPeriod[ 1 ] - 1300;
ChangeDuty[ 1 ](pwmDuty[ 1 ]);
PWM_TIM9_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM9_CH1_PE5);
}
void UpdateCurrentDuty(int updateFor, int component)
{
if (pwmDuty[component]+ updateFor < 0)
{
pwmDuty[component] = 0;
}
else
{
pwmDuty[component] += updateFor;
}
ChangeDuty[component](pwmDuty[component]);
}
int selectedWheel = 1 ;
int wheelDirection = 1 ;
void WheelsSpeedAndDirectionTest()
{
InitLeftWheelPWM_Timer4_CH1_PB6();
InitRightWheelPWM_Timer9_CH1_PE5();
while(1)
{
selectedWheel = wheelDirection;
Delay_ms(3000);
Delay_ms(3000);
if (wheelDirection)
{
selectedWheel = !selectedWheel;
}
else
{
wheelDirection = !wheelDirection;
}
}
}
void WheelsSpeedTest()
{
Delay_ms(2000);
InitRightWheelPWM_Timer9_CH1_PE5();
while(pwmDuty[ 1 ]>0)
{
UpdateCurrentDuty(50, 1 );
Delay_ms(250);
}
}
int ServoPosition = 0;
void ServoPositionTest()
{
while(1)
{
ChangeDuty[ 2 ](sensorPositions[ServoPosition]);
ChangeDuty[ 3 ](sensorPositions[ServoPosition]);
Delay_ms(500);
ServoPosition=(ServoPosition+1)%15;
}
}
void SpinAndBack()
{
Delay_ms(3000);
ChangeDuty[ 1 ](0);
ChangeDuty[ 0 ](0);
SpinDirectionLeftWheel = ~SpinDirectionLeftWheel;
Delay_ms(3000);
ChangeDuty[ 1 ](pwmDuty[ 1 ]);
ChangeDuty[ 0 ](pwmDuty[ 0 ]);
Delay_ms(3000);
ChangeDuty[ 1 ](0);
ChangeDuty[ 0 ](0);
}
void StopWheels()
{
ChangeDuty[ 0 ](0);
ChangeDuty[ 1 ](0);
}
void StartWheels()
{
ChangeDuty[ 1 ](pwmDuty[ 1 ]);
ChangeDuty[ 0 ](pwmDuty[ 0 ]);
}
void DriveWhileParkingNotSpotted()
{
TriggerFrontSensorMeasurement();
TriggerBackSensorMeasurement();
while((GetFrontSensorDistance() - GetBackSensorDistance()) < 15)
{
TriggerFrontSensorMeasurement();
TriggerBackSensorMeasurement();
}
leftWheelStopped = 0;
rightWheelStopped = 0;
wheelInterruptCount[0]=60;
wheelInterruptCount[1]=60;
startCounting = 1;
StartWheels();
while (leftWheelStopped == 0 || rightWheelStopped == 0);
StopWheels();
}
void RotateFor90Right()
{
leftWheelStopped = 0;
rightWheelStopped = 0;
SpinDirectionLeftWheel = 0 ;
SpinDirectionRightWheel = 1 ;
wheelInterruptCount[0]=52;
wheelInterruptCount[1]=52;
startCounting = 1;
StartWheels();
while (leftWheelStopped == 0 || rightWheelStopped == 0);
}
void DriveUntillWall()
{
Delay_ms(100);
SpinDirectionLeftWheel = 1 ;
SpinDirectionRightWheel = 1 ;
startCounting = 0;
TriggerFrontSensorMeasurement();
StartWheels();
while (GetFrontSensorDistance() > 8)
{
TriggerFrontSensorMeasurement();
}
StopWheels();
}
void InitializeWheels()
{
InitLeftWheelPWM_Timer4_CH1_PB6();
InitRightWheelPWM_Timer9_CH1_PE5();
InitExternIntRisingEdge_PC1_PC2();
}
void main() {
InitializeSensors();
AlignRightSensors();
Delay_ms(4000);
InitializeWheels();
DriveWhileParkingNotSpotted();
RotateFor90Right();
RotateFrontSensorFront();
DriveUntillWall();
RotateFor90Right();
while(1);
#line 267 "C:/Users/Jelena/Desktop/SelfParkingCars/SelfParkingCars.c"
}