car_fmw: implement 5 demo car programs

Demo programs:
1. Go forward
2. Go backward
3. Rotate right
4. Rotate left
5. Complicated route
All programs are running in a cycle

+ Refactor delay.h code to have common code style with main.c
This commit is contained in:
Eugene Batalov
2016-07-07 23:27:07 +03:00
parent aa67c399e6
commit c7b88455e5
3 changed files with 67 additions and 42 deletions
-16
View File
@@ -1,16 +0,0 @@
#pragma once
#include "stm32f4_discovery.h"
#include "stm32f4xx_conf.h"
/**
* @brief Delay Function.
* @param nCount:specifies the Delay time length.
* @retval None
*/
inline void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
+61 -26
View File
@@ -1,10 +1,67 @@
#include "car_leds.h" #include "car_leds.h"
#include "car_engine.h" #include "car_engine.h"
#include "delay.h" #include "wait.h"
const uint32_t PROGRAM_DURATION = 0x3FFFFF;
static void program_forward(void)
{
engine_forward();
wait(PROGRAM_DURATION);
}
static void program_backward(void)
{
engine_backward();
wait(PROGRAM_DURATION);
}
static void program_rotation_left(void)
{
engine_turn_left();
wait(PROGRAM_DURATION);
}
static void program_rotation_right(void)
{
engine_turn_right();
wait(PROGRAM_DURATION);
}
static void program_arbitraty_route(void)
{
const int ROUTE_PIECE_DURATION = 6 * PROGRAM_DURATION;
engine_forward();
wait(ROUTE_PIECE_DURATION);
engine_backward();
wait(ROUTE_PIECE_DURATION);
engine_forward();
wait(ROUTE_PIECE_DURATION / 2);
engine_turn_right();
wait(ROUTE_PIECE_DURATION);
engine_forward();
wait(ROUTE_PIECE_DURATION / 2);
engine_backward();
wait(ROUTE_PIECE_DURATION / 2);
engine_turn_left();
wait(ROUTE_PIECE_DURATION);
engine_backward();
wait(ROUTE_PIECE_DURATION / 2);
engine_stop();
wait(ROUTE_PIECE_DURATION / 2);
}
int main(void) int main(void)
{ {
/*!< At this stage the microcontroller clock setting is already configured, /*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup this is done through SystemInit() function which is called from startup
file (startup_stm32f4xx.s) before to branch to application main. file (startup_stm32f4xx.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to To reconfigure the default setting of SystemInit() function, refer to
@@ -14,29 +71,7 @@ int main(void)
leds_init(); leds_init();
engine_init(); engine_init();
const int DELAY = 0x3FFFFF; while(1) {
while (1) program_forward();
{
led_set(LED_GREEN, true);
Delay(DELAY);
engine_forward();
led_set(LED_ORANGE, true);
Delay(DELAY);
engine_stop();
led_set(LED_RED, true);
Delay(DELAY);
engine_backward();
led_set(LED_BLUE, true);
Delay(DELAY);
engine_stop();
led_set(LED_GREEN, false);
led_set(LED_ORANGE, false);
led_set(LED_RED, false);
led_set(LED_BLUE, false);
Delay(DELAY);
} }
} }
+6
View File
@@ -0,0 +1,6 @@
#pragma once
inline void wait(uint32_t loops)
{
while(--loops);
}