|
|
|
@ -6,12 +6,15 @@ |
|
|
|
* Author: jonas |
|
|
|
* Author: jonas |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdbool.h> |
|
|
|
#include "splitflap.h" |
|
|
|
#include "splitflap.h" |
|
|
|
#include "fsl_debug_console.h" |
|
|
|
#include "fsl_debug_console.h" |
|
|
|
#include "McuULN2003.h" |
|
|
|
#include "McuULN2003.h" |
|
|
|
#include <stdbool.h> |
|
|
|
#include <stdbool.h> |
|
|
|
#include "McuWait.h" |
|
|
|
#include "McuWait.h" |
|
|
|
|
|
|
|
#include "McuRTOS.h" |
|
|
|
#include "lib/dict.h" |
|
|
|
#include "lib/dict.h" |
|
|
|
|
|
|
|
#include "McuUtility.h" |
|
|
|
|
|
|
|
|
|
|
|
/* dynamic dictionary for the letters of the dictionary */ |
|
|
|
/* dynamic dictionary for the letters of the dictionary */ |
|
|
|
static dict_t **splitFlapDict; |
|
|
|
static dict_t **splitFlapDict; |
|
|
|
@ -22,6 +25,12 @@ static char* SF_Letters[] = { " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", |
|
|
|
"!", "?", ":"}; |
|
|
|
"!", "?", ":"}; |
|
|
|
/* flag if this module is initialized (physically) */ |
|
|
|
/* flag if this module is initialized (physically) */ |
|
|
|
static bool initialized = false; |
|
|
|
static bool initialized = false; |
|
|
|
|
|
|
|
/* mutex for if the module is currently being moved */ |
|
|
|
|
|
|
|
static SemaphoreHandle_t ongoingMoveMutex = NULL; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* function declarations */ |
|
|
|
|
|
|
|
static bool OngoingMoveMutex_Lock(void); |
|
|
|
|
|
|
|
static void OngoingMoveMutex_Unlock(void); |
|
|
|
|
|
|
|
|
|
|
|
void SF_InitConfig(void){ |
|
|
|
void SF_InitConfig(void){ |
|
|
|
splitFlapDict = dictAlloc(); |
|
|
|
splitFlapDict = dictAlloc(); |
|
|
|
@ -58,12 +67,19 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){ |
|
|
|
splitflap->motor = McuULN2003_InitMotor(&instance->motorConfig); |
|
|
|
splitflap->motor = McuULN2003_InitMotor(&instance->motorConfig); |
|
|
|
splitflap->id = id; |
|
|
|
splitflap->id = id; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create mutex for ongoing move
|
|
|
|
|
|
|
|
ongoingMoveMutex = xSemaphoreCreateRecursiveMutex(); |
|
|
|
|
|
|
|
char text[50] = "Ongoing move SplitFlap "; |
|
|
|
|
|
|
|
McuUtility_strcatNum16s((uint8_t*)text, sizeof(text)+20, id); |
|
|
|
|
|
|
|
vQueueAddToRegistry(ongoingMoveMutex, text); |
|
|
|
|
|
|
|
|
|
|
|
return splitflap; |
|
|
|
return splitflap; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){ |
|
|
|
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){ |
|
|
|
int numStepsMoved = 0; |
|
|
|
int numStepsMoved = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(OngoingMoveMutex_Lock()){ |
|
|
|
// move out of sensor
|
|
|
|
// move out of sensor
|
|
|
|
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == true){ |
|
|
|
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == true){ |
|
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor); |
|
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor); |
|
|
|
@ -87,6 +103,9 @@ bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){ |
|
|
|
McuULN2003_SetPos(((SF_t*)instance)->motor, 0); |
|
|
|
McuULN2003_SetPos(((SF_t*)instance)->motor, 0); |
|
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor); |
|
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OngoingMoveMutex_Unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// success if less than one rotation
|
|
|
|
// success if less than one rotation
|
|
|
|
initialized = (numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND); |
|
|
|
initialized = (numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND); |
|
|
|
|
|
|
|
|
|
|
|
@ -95,6 +114,8 @@ bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){ |
|
|
|
|
|
|
|
|
|
|
|
void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){ |
|
|
|
void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){ |
|
|
|
if(initialized){ |
|
|
|
if(initialized){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(OngoingMoveMutex_Lock()){ |
|
|
|
// run move with acceleration & deceleration
|
|
|
|
// run move with acceleration & deceleration
|
|
|
|
McuULN2003_AccelerationStart(((SF_t*)instance)->motor); |
|
|
|
McuULN2003_AccelerationStart(((SF_t*)instance)->motor); |
|
|
|
while(steps>0){ |
|
|
|
while(steps>0){ |
|
|
|
@ -109,6 +130,9 @@ void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){ |
|
|
|
// required since it is possible that one is still active, which would result in the motor getting hot
|
|
|
|
// required since it is possible that one is still active, which would result in the motor getting hot
|
|
|
|
// no re-init is required
|
|
|
|
// no re-init is required
|
|
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor); |
|
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OngoingMoveMutex_Unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -117,6 +141,8 @@ bool SF_GetMagSensorAtZeroPosition(SF_Handle_t instance){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SF_MoveToFlap(SF_Handle_t instance, char* flap){ |
|
|
|
void SF_MoveToFlap(SF_Handle_t instance, char* flap){ |
|
|
|
|
|
|
|
if(OngoingMoveMutex_Lock()){ |
|
|
|
|
|
|
|
|
|
|
|
// get flap pos from dictonary
|
|
|
|
// get flap pos from dictonary
|
|
|
|
int32_t flapPos = (int32_t)getItem(*splitFlapDict, flap); |
|
|
|
int32_t flapPos = (int32_t)getItem(*splitFlapDict, flap); |
|
|
|
// get current motor pos
|
|
|
|
// get current motor pos
|
|
|
|
@ -132,6 +158,50 @@ void SF_MoveToFlap(SF_Handle_t instance, char* flap){ |
|
|
|
} |
|
|
|
} |
|
|
|
SF_MoveSteps(instance, stepsToReachFlap); |
|
|
|
SF_MoveSteps(instance, stepsToReachFlap); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OngoingMoveMutex_Unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct{ |
|
|
|
|
|
|
|
SF_Handle_t instance; |
|
|
|
|
|
|
|
char* flap; |
|
|
|
|
|
|
|
} MoveToFlap_Param_t; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* pointer to MoveToFlap_Param_t */ |
|
|
|
|
|
|
|
static void SF_MoveToFlapWithParameter(void *pv){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MoveToFlap_Param_t param = *(MoveToFlap_Param_t*)pv; |
|
|
|
|
|
|
|
SF_MoveToFlap(param.instance, param.flap); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MoveToFlap_Param_t taskParameters; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SF_MoveToFlapAsync(SF_Handle_t instance, char* flap){ |
|
|
|
|
|
|
|
if(OngoingMoveMutex_Lock()){ |
|
|
|
|
|
|
|
BaseType_t res; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char taskName[50] = "SF Mv "; |
|
|
|
|
|
|
|
McuUtility_strcatNum16s((uint8_t*)taskName, sizeof(taskName)+20, ((SF_t*)instance)->id); |
|
|
|
|
|
|
|
taskParameters.instance = instance; |
|
|
|
|
|
|
|
taskParameters.flap = flap; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
res = xTaskCreate( SF_MoveToFlapWithParameter, |
|
|
|
|
|
|
|
taskName, |
|
|
|
|
|
|
|
500/sizeof(StackType_t), |
|
|
|
|
|
|
|
&taskParameters, |
|
|
|
|
|
|
|
tskIDLE_PRIORITY, |
|
|
|
|
|
|
|
NULL); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(res != pdPASS) // task creation not successful?
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
PRINTF("Creation of %s failed", taskName); |
|
|
|
|
|
|
|
for(;;) {} // Endless loop
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OngoingMoveMutex_Unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int32_t SF_GetMotorPosition(SF_Handle_t instance){ |
|
|
|
int32_t SF_GetMotorPosition(SF_Handle_t instance){ |
|
|
|
@ -139,7 +209,27 @@ int32_t SF_GetMotorPosition(SF_Handle_t instance){ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void SF_Deinit(SF_Handle_t instance){ |
|
|
|
void SF_Deinit(SF_Handle_t instance){ |
|
|
|
|
|
|
|
vSemaphoreDelete(ongoingMoveMutex); |
|
|
|
McuULN2003_DeinitMotor(((SF_t*)instance)->motor); |
|
|
|
McuULN2003_DeinitMotor(((SF_t*)instance)->motor); |
|
|
|
McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor); |
|
|
|
McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor); |
|
|
|
initialized = false; |
|
|
|
initialized = false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* HELPERS */ |
|
|
|
|
|
|
|
static bool OngoingMoveMutex_Lock(void){ |
|
|
|
|
|
|
|
/* aquire mutex */ |
|
|
|
|
|
|
|
if(xSemaphoreTakeRecursive(ongoingMoveMutex, pdMS_TO_TICKS(20)) != pdTRUE){ |
|
|
|
|
|
|
|
return false; /* timeout? */ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void OngoingMoveMutex_Unlock(void){ |
|
|
|
|
|
|
|
/* give back mutex */ |
|
|
|
|
|
|
|
if(xSemaphoreGiveRecursive(ongoingMoveMutex) != pdTRUE){ |
|
|
|
|
|
|
|
/* issue */ |
|
|
|
|
|
|
|
PRINTF("Could not give back ongoing move mutex for splitflap"); |
|
|
|
|
|
|
|
for(;;); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|