You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
6.5 KiB
235 lines
6.5 KiB
/*
|
|
#include <splitflap_flaps.h>
|
|
* splitflap.c
|
|
*
|
|
* Created on: 29.09.2022
|
|
* Author: jonas
|
|
*/
|
|
|
|
#include <stdbool.h>
|
|
#include "splitflap.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "McuULN2003.h"
|
|
#include <stdbool.h>
|
|
#include "McuWait.h"
|
|
#include "McuRTOS.h"
|
|
#include "lib/dict.h"
|
|
#include "McuUtility.h"
|
|
|
|
/* dynamic dictionary for the letters of the dictionary */
|
|
static dict_t **splitFlapDict;
|
|
/* all letters of the splitflap in the correct order */
|
|
static char* SF_Letters[] = { " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
|
|
"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",
|
|
"!", "?", ":"};
|
|
/* flag if this module is initialized (physically) */
|
|
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){
|
|
splitFlapDict = dictAlloc();
|
|
((dict_t*)splitFlapDict)->key=NULL;
|
|
((dict_t*)splitFlapDict)->value=NULL;
|
|
((dict_t*)splitFlapDict)->next=NULL;
|
|
float stepsPerSegment = SPLITFLAP_STEPS_PER_SEGMENT; // do division once
|
|
|
|
// add all splitflap flaps to the dictionary, calculating the position automatically
|
|
// print out for debugging
|
|
PRINTF("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);
|
|
}
|
|
}
|
|
|
|
void SF_DeInitConfig(void){
|
|
dictDealoc(splitFlapDict);
|
|
}
|
|
|
|
SF_Handle_t SF_Init(SF_Config_t* instance, int id){
|
|
SF_t* splitflap;
|
|
|
|
#if SPLITFLAP_CONFIG_USE_FREERTOS_HEAP
|
|
splitflap = (SF_t*)pvPortMalloc(sizeof(SF_t)); /* get a new device descriptor */
|
|
#else
|
|
splitflap = (SF_t*)malloc(sizeof(SF_t)); /* get a new device descriptor */
|
|
#endif
|
|
|
|
splitflap->magSensor = McuGPIO_InitGPIO(&instance->magSensorConfig);
|
|
splitflap->motor = McuULN2003_InitMotor(&instance->motorConfig);
|
|
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;
|
|
}
|
|
|
|
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){
|
|
int numStepsMoved = 0;
|
|
|
|
if(OngoingMoveMutex_Lock()){
|
|
// move out of sensor
|
|
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == true){
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor);
|
|
McuWait_Waitms(20);
|
|
}
|
|
|
|
// turn until sensor is on and not reached one full rotation already (timeout)
|
|
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == false && numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND ){
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor);
|
|
McuWait_Waitms(20);
|
|
numStepsMoved++;
|
|
}
|
|
|
|
// offset after init
|
|
if(numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND){
|
|
for(int i=0; i < offsetSteps; i++){
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor);
|
|
McuWait_Waitms(20);
|
|
}
|
|
}
|
|
McuULN2003_SetPos(((SF_t*)instance)->motor, 0);
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor);
|
|
|
|
OngoingMoveMutex_Unlock();
|
|
}
|
|
|
|
// success if less than one rotation
|
|
initialized = (numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND);
|
|
|
|
return initialized;
|
|
}
|
|
|
|
void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){
|
|
if(initialized){
|
|
|
|
if(OngoingMoveMutex_Lock()){
|
|
// run move with acceleration & deceleration
|
|
McuULN2003_AccelerationStart(((SF_t*)instance)->motor);
|
|
while(steps>0){
|
|
if(McuULN2003_StepCallback(((SF_t*)instance)->motor, true)){
|
|
steps--;
|
|
}
|
|
McuWait_Waitms(1);
|
|
}
|
|
McuULN2003_AccelerationEnd(((SF_t*)instance)->motor);
|
|
|
|
// Power off disables all outputs of the ULN,
|
|
// required since it is possible that one is still active, which would result in the motor getting hot
|
|
// no re-init is required
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor);
|
|
|
|
OngoingMoveMutex_Unlock();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool SF_GetMagSensorAtZeroPosition(SF_Handle_t instance){
|
|
return McuGPIO_IsLow(((SF_t*)instance)->magSensor);
|
|
}
|
|
|
|
void SF_MoveToFlap(SF_Handle_t instance, char* flap){
|
|
if(OngoingMoveMutex_Lock()){
|
|
|
|
// get flap pos from dictonary
|
|
int32_t flapPos = (int32_t)getItem(*splitFlapDict, flap);
|
|
// get current motor pos
|
|
int32_t currentPos = SF_GetMotorPosition(instance) % SPLITFLAP_STEPS_ONE_ROUND;
|
|
// calc steps to move
|
|
int32_t stepsToReachFlap = 0;
|
|
// not already there
|
|
if(flapPos != currentPos){
|
|
if(flapPos < currentPos){
|
|
stepsToReachFlap = SPLITFLAP_STEPS_ONE_ROUND-currentPos+flapPos;
|
|
}else if(flapPos > currentPos){
|
|
stepsToReachFlap = flapPos - currentPos;
|
|
}
|
|
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){
|
|
return McuULN2003_GetPos(((SF_t*)instance)->motor);
|
|
}
|
|
|
|
void SF_Deinit(SF_Handle_t instance){
|
|
vSemaphoreDelete(ongoingMoveMutex);
|
|
McuULN2003_DeinitMotor(((SF_t*)instance)->motor);
|
|
McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor);
|
|
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(;;);
|
|
}
|
|
}
|
|
|