moved logging to McuLog,

fixed multi split flap issues,
creating task for every split flap per default,
creating queue for every split flap to sent letter to
main
Jonas Arnold 4 years ago
parent e3d082ee17
commit af78f205e1
  1. 22
      ADIS_tinyK22_SplitFlap/source/application.c
  2. 13
      ADIS_tinyK22_SplitFlap/source/main.c
  3. 12
      ADIS_tinyK22_SplitFlap/source/multi-splitflap.c
  4. 3
      ADIS_tinyK22_SplitFlap/source/platform.h
  5. 133
      ADIS_tinyK22_SplitFlap/source/splitflap.c
  6. 2
      ADIS_tinyK22_SplitFlap/source/splitflap.h

@ -5,8 +5,8 @@
* Author: jonas
*/
#include "fsl_debug_console.h"
#include "platform.h"
#include "McuRTOS.h"
#include "application.h"
#include "McuWait.h"
#include "McuLED.h"
@ -17,8 +17,6 @@
#include "multi-splitflap.h"
#include "McuLog.h"
#define APP_DEBUG
/* blue led pins */
#define LED_BLUE_GPIO GPIOC
#define LED_BLUE_PORT PORTC
@ -55,9 +53,9 @@ void App_Init(void){
static void App_Task(void* pv){
McuLog_info("Application Task starting");
PRINTF("Initializing split flap motors.\n");
McuLog_info("Initializing split flap motors.\n");
bool successfulInit0 = SF_MoveMotorToZeroPosition(splitflap0, 20);
PRINTF("Init of motor 0 done. Success = %s\n\n", successfulInit0 ? "true" : "false");
McuLog_info("Init of motor 0 done. Success = %s\n\n", successfulInit0 ? "true" : "false");
#ifndef APP_DEBUG
// if init failed => stop
@ -67,7 +65,7 @@ static void App_Task(void* pv){
#endif
bool successfulInit1 = SF_MoveMotorToZeroPosition(splitflap1, 25);
PRINTF("Init of motor 1 done. Success = %s\n\n", successfulInit1 ? "true" : "false");
McuLog_info("Init of motor 1 done. Success = %s\n\n", successfulInit1 ? "true" : "false");
#ifndef APP_DEBUG
// if init failed => stop
@ -80,7 +78,9 @@ static void App_Task(void* pv){
char sentence[] = "JONAS!";
MultiSplitFlap_Display(sentence);
return;
for(;;)
{}
/* SINGLE SPLIT FLAP TESTING */
// go through the following letters
@ -89,14 +89,14 @@ static void App_Task(void* pv){
// "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
// "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
// "!", "?", ":"};
PRINTF("Amount of letters %i\n", sizeof(letters)/sizeof(letters[0]));
McuLog_info("Amount of letters %i\n", sizeof(letters)/sizeof(letters[0]));
while(1){
for(int i = 0; i < sizeof(letters)/sizeof(letters[0]); i++){
McuWait_Waitms(1000);
PRINTF("Moving to letter '%s', position is %i\n", letters[i], (int)SF_GetMotorPosition(splitflap0));
McuLog_info("Moving to letter '%s', position is %i\n", letters[i], (int)SF_GetMotorPosition(splitflap0));
SF_MoveToFlap(splitflap0, letters[i]);
PRINTF("Position after move is is %i\n", (int)SF_GetMotorPosition(splitflap0));
McuLog_info("Position after move is is %i\n", (int)SF_GetMotorPosition(splitflap0));
}
}
}
@ -114,7 +114,7 @@ void App_Run(void){
if(res != pdPASS) // task creation not successful?
{
PRINTF("Task creation of app task failed");
McuLog_info("Task creation of app task failed");
for(;;) {} // Endless loop
}

@ -38,7 +38,8 @@
#include "pin_mux.h"
#include "clock_config.h"
#include "MK22F51212.h"
#include "fsl_debug_console.h"
#include "McuRTOS.h"
#include "McuLog.h"
/* TODO: insert other include files here. */
#include "application.h"
#include "platform.h"
@ -59,22 +60,22 @@ int main(void) {
#endif
/* init platform */
PRINTF("Initializing Platform...\n");
McuLog_info("Initializing Platform...\n");
PL_Init();
/* init app */
PRINTF("Initializing App...\n");
McuLog_info("Initializing App...\n");
App_Init();
/* run app */
PRINTF("Running App...\n");
McuLog_info("Running App...\n");
App_Run();
/* deinit app */
PRINTF("De-Initializing App...\n");
McuLog_info("De-Initializing App...\n");
App_Deinit();
/* deinit platform */
PRINTF("De-Initializing Platform...\n");
McuLog_info("De-Initializing Platform...\n");
PL_Deinit();
return 0 ;

@ -8,7 +8,8 @@
#include <stdio.h>
#include <stdbool.h>
#include "multi-splitflap.h"
#include "fsl_debug_console.h"
#include "McuRTOS.h"
#include "McuLog.h"
#include "splitflap.h"
/* vars */
@ -41,9 +42,9 @@ void MultiSplitFlap_Deinit(void){
/* PUBLIC FUNCTIONS */
/**********************/
void MultiSplitFlap_AddFlap(SF_Handle_t splitflap){
PRINTF("Adding split flap to multi splitflap combination...\n");
McuLog_info("Adding split flap to multi splitflap combination...\n");
addItem(flapDict, flapKeys[addedFlaps], (SF_Handle_t*)splitflap);
PRINTF("Added flap nr. %i/%i.\n", addedFlaps+1, NUM_FLAPS);
McuLog_info("Added flap nr. %i/%i.\n", addedFlaps+1, NUM_FLAPS);
addedFlaps++;
}
@ -54,7 +55,8 @@ void MultiSplitFlap_Display(char sentence[]){
char letter_move[] = " ";
letter_move[0] = letter;
SF_MoveToFlapAsync(sfHandle, letter_move);
PRINTF("Multi splitflap: Commanded Flap nr. %i to letter '%c'.\n", num+1, letter);
McuLog_info("Multi splitflap: Commanded Flap nr. %i to letter '%c'.\n", num+1, letter);
vTaskDelay(pdMS_TO_TICKS(100));
}
// for testing purposes only
//SF_t* sf0 = (SF_t*)(SF_Handle_t)(getItem(*flapDict, flapKeys[0]));
@ -76,7 +78,7 @@ void initFlapKeys(uint8_t numberOfFlaps){
flapKeys[i] = pvPortMalloc(sizeof(BYTES_PER_KEY));
#else
if((flapKeys[i] = malloc(BYTES_PER_KEY)) == NULL){
PRINTF("Reserving memory for flap num. %i failed!:\n", i);
McuLog_error("Reserving memory for flap num. %i failed!:\n", i);
}
sprintf(flapKeys[i], "%i", i);
#endif

@ -8,6 +8,9 @@
#ifndef PLATFORM_H_
#define PLATFORM_H_
/* SETTINGS */
#define APP_DEBUG
/* Platform initialization */
void PL_Init(void);

@ -6,16 +6,20 @@
* Author: jonas
*/
#include "platform.h"
#include <stdbool.h>
#include "splitflap.h"
#include "fsl_debug_console.h"
#include "McuRTOS.h"
#include "McuULN2003.h"
#include <stdbool.h>
#include "McuWait.h"
#include "McuRTOS.h"
#include "McuLog.h"
#include "lib/dict.h"
#include "McuUtility.h"
#define FLAP_QUEUE_LENGTH (5) /* number of elements in queue */
#define FLAP_QUEUE_SELEM_SIZE (sizeof(Flap_t)) /* size of element */
/* dynamic dictionary for the letters of the dictionary */
static dict_t **splitFlapDict;
/* all letters of the splitflap in the correct order */
@ -27,7 +31,7 @@ static Flap_t SF_Letters[] = { " ", "A", "B", "C", "D", "E", "F", "G", "H", "I",
/* function declarations */
static bool OngoingMoveMutex_Lock(SF_Handle_t instance);
static void OngoingMoveMutex_Unlock(SF_Handle_t instance);
static void SF_MoveToNextFlap(void *pv);
static void SF_Task(void *pv);
/**********************/
/* INIT / DEINIT */
@ -41,12 +45,12 @@ void SF_InitConfig(void){
// add all splitflap flaps to the dictionary, calculating the position automatically
// print out for debugging
PRINTF("Splitflap positioning data is automatically calculated:\n");
McuLog_info("Splitflap positioning data is automatically calculated:\n");
for(int i = 0; i < SPLITFLAP_AMOUNT_OF_SEGMENTS; i ++){
// + 0.5 so the rounding is done correctly
int32_t position = (stepsPerSegment * (float)i + 0.5);
addItem(splitFlapDict, SF_Letters[i], (int32_t*)position);
PRINTF("Letter '%s': Position %i\n", SF_Letters[i], (int)position);
McuLog_info("Letter '%s': Position %i\n", SF_Letters[i], (int)position);
}
}
@ -65,15 +69,43 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){
splitflap->magSensor = McuGPIO_InitGPIO(&instance->magSensorConfig);
splitflap->motor = McuULN2003_InitMotor(&instance->motorConfig);
splitflap->id = id;
splitflap->ongoingMoveMutex = xSemaphoreCreateRecursiveMutex(); // create mutex for ongoing move
splitflap->flapQueue = xQueueCreate(FLAP_QUEUE_LENGTH, FLAP_QUEUE_SELEM_SIZE); // create queue
splitflap->id = id;
#ifndef APP_DEBUG // when not debugging => set to false
splitflap->initialized = false;
splitflap->nextFlap = SF_Letters[0];
#else // when debugging => set to true
splitflap->initialized = true;
#endif
// add mutex to registry
char text[50] = "Ongoing move SplitFlap ";
McuUtility_strcatNum16s((uint8_t*)text, sizeof(text)+20, id);
vQueueAddToRegistry(splitflap->ongoingMoveMutex, text);
// check if queue was created
if(splitflap->flapQueue == NULL){
McuLog_error("Flap queue creation failed for Splitflap <%i>", id);
for(;;){}
}
// if yes: add queue to registry
vQueueAddToRegistry(splitflap->flapQueue, "FlapQueue");
// check if queue was created
if(splitflap->ongoingMoveMutex == NULL){
McuLog_error("Ongoing Move mutex creation failed for Splitflap <%i>", id);
for(;;){}
}
// if yes: add ongoing move mutex to registry
vQueueAddToRegistry(splitflap->ongoingMoveMutex, "OngoingMoveMutex");
// create Task for splitflap
BaseType_t res = xTaskCreate( SF_Task,
"SF",
800/sizeof(StackType_t),
splitflap, // no &, since otherwise pointing on parameter address! we want handle address!
tskIDLE_PRIORITY,
NULL);
if(res != pdPASS) // task creation not successful?
{
McuLog_error("Creation of task for Splitflap <%i> failed", id);
for(;;) {} // Endless loop
}
return splitflap;
}
@ -92,6 +124,11 @@ void SF_Deinit(SF_Handle_t instance){
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){
int numStepsMoved = 0;
if(((SF_t*)instance)->initialized){
McuLog_warn("Tried to initialize motor of Splitflap <%i>, but already initialized => skipping", ((SF_t*)instance)->id);
return true;
}
if(OngoingMoveMutex_Lock(instance)){
// move out of sensor
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == true){
@ -145,6 +182,8 @@ void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){
OngoingMoveMutex_Unlock(instance);
}
} else{
McuLog_error("SF_MoveSteps could not be called for Splitflap <%i> because it is not initialized.", ((SF_t*)instance)->id);
}
}
@ -176,28 +215,8 @@ void SF_MoveToFlap(SF_Handle_t instance, Flap_t flap){
}
void SF_MoveToFlapAsync(SF_Handle_t instance, Flap_t flap){
if(OngoingMoveMutex_Lock(instance)){
BaseType_t res;
char taskName[10] = "SF Mv ";
McuUtility_strcatNum16s((uint8_t*)taskName, sizeof(taskName), ((SF_t*)instance)->id);
// set next flap
((SF_t*)instance)->nextFlap = flap;
res = xTaskCreate( SF_MoveToNextFlap,
taskName,
500/sizeof(StackType_t),
instance, // no &, since otherwise pointing on parameter address! we want handle address!
tskIDLE_PRIORITY,
NULL);
if(res != pdPASS) // task creation not successful?
{
PRINTF("Creation of %s failed", taskName);
for(;;) {} // Endless loop
}
OngoingMoveMutex_Unlock(instance);
if(xQueueSendToBack(((SF_t*)instance)->flapQueue, &flap, pdMS_TO_TICKS(100)) != pdPASS){
McuLog_error("SF_MoveToFlapAsync: Failed to add flap to queue for Splitflap <%i>", ((SF_t*)instance)->id);
}
}
@ -209,14 +228,48 @@ int32_t SF_GetMotorPosition(SF_Handle_t instance){
/* INTERNAL FUNCTIONS */
/**********************/
/* voidpointer to instance of splitflap (SF_Handle_t) */
static void SF_MoveToNextFlap(void *pv){
static void SF_Task(void *pv){
// parse parameter
SF_Handle_t instance = (SF_Handle_t)pv;
PRINTF("Splitflap: Moving Flap nr. %i to letter '%c'.\n", ((SF_t*)instance)->id, (((SF_t*)instance)->nextFlap)[0]);
// move to next flap
SF_MoveToFlap(instance, ((SF_t*)instance)->nextFlap);
// reset next flap
((SF_t*)instance)->nextFlap = SF_Letters[0];
SF_t* splitflap = (SF_t*)instance;
Flap_t nextFlap = " ";
McuLog_info("Splitflap: Task for Splitflap <%i> started.\n", splitflap->id);
for(;;){
// delay task
vTaskDelay(pdMS_TO_TICKS(50));
// if not initialized => Skip everything
if(splitflap->initialized == false){
continue;
}
// check if anything is in queue once (poll once, don't get item)
if(xQueuePeek(splitflap->flapQueue, &nextFlap, 0) != pdPASS){
/* failed to receive => queue empty */
continue;
}
McuLog_info("Splitflap <%i> recognized new flap '%c' in queue to move to.", splitflap->id, nextFlap[0]);
// new flap to move to is available
// check if ongoing move
if(OngoingMoveMutex_Lock(instance)){
// now get the queue item
if(xQueueReceive(splitflap->flapQueue, &nextFlap, 0) != pdPASS){
/* failed to receive => queue empty but previously an iteam was there? error */
McuLog_error("Failed to receive Queue item of Splitflap <%i>.", splitflap->id);
OngoingMoveMutex_Unlock(instance);
continue; // ignore & proceed
}
//=> execute move
McuLog_info("Splitflap <%i> moving to flap '%c'.", splitflap->id, nextFlap[0]);
SF_MoveToFlap(instance, nextFlap);
McuLog_info("Splitflap <%i> move done to flap '%c'.", splitflap->id, nextFlap[0]);
OngoingMoveMutex_Unlock(instance);
}
}
}
@ -235,7 +288,7 @@ static void OngoingMoveMutex_Unlock(SF_Handle_t instance){
/* give back mutex */
if(xSemaphoreGiveRecursive(((SF_t*)instance)->ongoingMoveMutex) != pdTRUE){
/* issue */
PRINTF("Could not give back ongoing move mutex for splitflap %i", ((SF_t*)instance)->id);
McuLog_error("Could not give back ongoing move mutex for splitflap %i", ((SF_t*)instance)->id);
for(;;);
}
}

@ -35,8 +35,8 @@ typedef struct {
McuULN2003_Handle_t motor;
McuGPIO_Handle_t magSensor;
SemaphoreHandle_t ongoingMoveMutex;
QueueHandle_t flapQueue;
bool initialized;
Flap_t nextFlap;
} SF_t;
typedef struct {

Loading…
Cancel
Save