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.
140 lines
4.4 KiB
140 lines
4.4 KiB
/*
|
|
#include <splitflap_flaps.h>
|
|
* splitflap.c
|
|
*
|
|
* Created on: 29.09.2022
|
|
* Author: jonas
|
|
*/
|
|
|
|
#include "splitflap.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "McuULN2003.h"
|
|
#include <stdbool.h>
|
|
#include "McuWait.h"
|
|
#include "lib/dict.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",
|
|
"!", "?", ":"};
|
|
|
|
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);
|
|
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;
|
|
|
|
return splitflap;
|
|
}
|
|
|
|
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, int8_t offsetSegments){
|
|
int numStepsMoved = 0;
|
|
|
|
// 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
|
|
while(SF_GetMagSensorAtZeroPosition((SF_t*)instance) == false && numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND ){
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor);
|
|
McuWait_Waitms(20);
|
|
numStepsMoved++;
|
|
}
|
|
|
|
// one more segment if not already one round
|
|
if(numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND){
|
|
// +0.5 so that the rounding is done correctly
|
|
int8_t offsetSteps = offsetSegments * SPLITFLAP_STEPS_ONE_ROUND/SPLITFLAP_AMOUNT_OF_SEGMENTS; // (int8_t)((float)offsetSegments*SPLITFLAP_STEPS_PER_SEGMENT + 0.5);
|
|
for(int i=0; i < offsetSteps; i++){
|
|
McuULN2003_IncStep(((SF_t*)instance)->motor);
|
|
McuWait_Waitms(20);
|
|
numStepsMoved++;
|
|
}
|
|
}
|
|
McuULN2003_SetPos(((SF_t*)instance)->motor, 0);
|
|
McuULN2003_PowerOff(((SF_t*)instance)->motor);
|
|
// success if less than one rotation
|
|
return numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND;
|
|
}
|
|
|
|
void SF_MoveSteps(SF_Handle_t instance, int32_t steps){
|
|
// 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);
|
|
}
|
|
|
|
bool SF_GetMagSensorAtZeroPosition(SF_Handle_t instance){
|
|
return McuGPIO_IsLow(((SF_t*)instance)->magSensor);
|
|
}
|
|
|
|
void SF_MoveToFlap(SF_Handle_t instance, char* flap){
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
int32_t SF_GetMotorPosition(SF_Handle_t instance){
|
|
return McuULN2003_GetPos(((SF_t*)instance)->motor);
|
|
}
|
|
|
|
void SF_Deinit(SF_Handle_t instance){
|
|
McuULN2003_DeinitMotor(((SF_t*)instance)->motor);
|
|
McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor);
|
|
}
|
|
|