introduced HardwareIdentifier_t and SetupIdentifier_t,

cleaned up,
added splitflap_positions.h file for positions (future)
main
Jonas Arnold 4 years ago
parent 1aa39368cf
commit 082e31e9f1
  1. 24
      ADIS_tinyK22_SplitFlap/source/application.c
  2. 63
      ADIS_tinyK22_SplitFlap/source/splitflap.c
  3. 32
      ADIS_tinyK22_SplitFlap/source/splitflap.h
  4. 24
      ADIS_tinyK22_SplitFlap/source/splitflap_positions.h

@ -53,8 +53,8 @@ void App_Init(void){
static void App_Task(void* pv){ static void App_Task(void* pv){
McuLog_info("Application Task starting"); McuLog_info("Application Task starting");
McuLog_info("Initializing split flap motors.\n"); McuLog_info("Initializing split flap motors.");
bool successfulInit0 = SF_MoveMotorToZeroPosition(splitflap0, 20); bool successfulInit0 = SF_MoveMotorToZeroPosition(splitflap0);
McuLog_info("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 #ifndef APP_DEBUG
@ -64,7 +64,7 @@ static void App_Task(void* pv){
} }
#endif #endif
bool successfulInit1 = SF_MoveMotorToZeroPosition(splitflap1, 25); bool successfulInit1 = SF_MoveMotorToZeroPosition(splitflap1);
McuLog_info("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 #ifndef APP_DEBUG
@ -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", // "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", // "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
// "!", "?", ":"}; // "!", "?", ":"};
McuLog_info("Amount of letters %i\n", sizeof(letters)/sizeof(letters[0])); McuLog_info("Amount of letters %i", sizeof(letters)/sizeof(letters[0]));
while(1){ while(1){
for(int i = 0; i < sizeof(letters)/sizeof(letters[0]); i++){ for(int i = 0; i < sizeof(letters)/sizeof(letters[0]); i++){
McuWait_Waitms(1000); McuWait_Waitms(1000);
McuLog_info("Moving to letter '%s', position is %i\n", letters[i], (int)SF_GetMotorPosition(splitflap0)); McuLog_info("Moving to letter '%s', position is %i", letters[i], (int)SF_GetMotorPosition(splitflap0));
SF_MoveToFlap(splitflap0, letters[i]); SF_MoveToFlap(splitflap0, letters[i]);
McuLog_info("Position after move is is %i\n", (int)SF_GetMotorPosition(splitflap0)); McuLog_info("Position after move is is %i", (int)SF_GetMotorPosition(splitflap0));
} }
} }
} }
@ -158,11 +158,13 @@ void configureSplitflaps(void){
/* create config instance for splitflap 0 */ /* create config instance for splitflap 0 */
SF_Config_t sf0Config; SF_Config_t sf0Config;
sf0Config.setupIdentifier = 0;
sf0Config.hardwareIdentifier = 34;
sf0Config.magSensorConfig = magSensor0Config; sf0Config.magSensorConfig = magSensor0Config;
sf0Config.motorConfig = motor0Config; sf0Config.motorConfig = motor0Config;
/* initialize splitflap 0 */ /* instanciate splitflap 0 */
splitflap0 = SF_Init(&sf0Config, 0); splitflap0 = SF_Init(&sf0Config);
/* SPLITFLAP 1 */ /* SPLITFLAP 1 */
/* configure the motor */ /* configure the motor */
@ -193,9 +195,11 @@ void configureSplitflaps(void){
/* create config instance for splitflap 0 */ /* create config instance for splitflap 0 */
SF_Config_t sf1Config; SF_Config_t sf1Config;
sf1Config.setupIdentifier = 1;
sf1Config.hardwareIdentifier = 35;
sf1Config.magSensorConfig = magSensor1Config; sf1Config.magSensorConfig = magSensor1Config;
sf1Config.motorConfig = motor1Config; sf1Config.motorConfig = motor1Config;
/* initialize splitflap 1 */ /* instanciate splitflap 1 */
splitflap1 = SF_Init(&sf1Config, 1); splitflap1 = SF_Init(&sf1Config);
} }

@ -9,6 +9,7 @@
#include "platform.h" #include "platform.h"
#include <stdbool.h> #include <stdbool.h>
#include "splitflap.h" #include "splitflap.h"
#include "splitflap_positions.h"
#include "McuRTOS.h" #include "McuRTOS.h"
#include "McuULN2003.h" #include "McuULN2003.h"
#include <stdbool.h> #include <stdbool.h>
@ -22,11 +23,6 @@
/* dynamic dictionary for the letters of the dictionary */ /* dynamic dictionary for the letters of the dictionary */
static dict_t **splitFlapDict; static dict_t **splitFlapDict;
/* all letters of the splitflap in the correct order */
static Flap_t 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",
"!", "?", ":"};
/* function declarations */ /* function declarations */
static bool OngoingMoveMutex_Lock(SF_Handle_t instance); static bool OngoingMoveMutex_Lock(SF_Handle_t instance);
@ -50,7 +46,7 @@ void SF_InitConfig(void){
// + 0.5 so the rounding is done correctly // + 0.5 so the rounding is done correctly
int32_t position = (stepsPerSegment * (float)i + 0.5); int32_t position = (stepsPerSegment * (float)i + 0.5);
addItem(splitFlapDict, SF_Letters[i], (int32_t*)position); addItem(splitFlapDict, SF_Letters[i], (int32_t*)position);
McuLog_info("Letter '%s': Position %i\n", SF_Letters[i], (int)position); McuLog_info("Letter '%s': Position %i", SF_Letters[i], (int)position);
} }
} }
@ -58,29 +54,31 @@ void SF_DeInitConfig(void){
dictDealoc(splitFlapDict); dictDealoc(splitFlapDict);
} }
SF_Handle_t SF_Init(SF_Config_t* instance, int id){ SF_Handle_t SF_Init(SF_Config_t* config){
SF_t* splitflap; SF_t* splitflap;
#if SPLITFLAP_CONFIG_USE_FREERTOS_HEAP #if SPLITFLAP_CONFIG_USE_FREERTOS_HEAP
splitflap = (SF_t*)pvPortMalloc(sizeof(SF_t)); /* get a new device descriptor */ splitflap = (SF_t*)pvPortMalloc(sizeof(SF_t)); // get memory for device from FreeRtos heap
#else #else
splitflap = (SF_t*)malloc(sizeof(SF_t)); /* get a new device descriptor */ splitflap = (SF_t*)malloc(sizeof(SF_t)); // get memory for device from MCU heap
#endif #endif
splitflap->magSensor = McuGPIO_InitGPIO(&instance->magSensorConfig); splitflap->magSensor = McuGPIO_InitGPIO(&config->magSensorConfig); // initialize magnet sensor GPIO
splitflap->motor = McuULN2003_InitMotor(&instance->motorConfig); splitflap->motor = McuULN2003_InitMotor(&config->motorConfig); // initialize ULN driver for stepper motor
splitflap->ongoingMoveMutex = xSemaphoreCreateRecursiveMutex(); // create mutex for ongoing move splitflap->ongoingMoveMutex = xSemaphoreCreateRecursiveMutex(); // create mutex for ongoing move
splitflap->flapQueue = xQueueCreate(FLAP_QUEUE_LENGTH, FLAP_QUEUE_SELEM_SIZE); // create queue splitflap->flapQueue = xQueueCreate(FLAP_QUEUE_LENGTH, // create queue
splitflap->id = id; FLAP_QUEUE_SELEM_SIZE);
#ifndef APP_DEBUG // when not debugging => set to false splitflap->id = config->setupIdentifier; // copy setup identifier
splitflap->hwId = config->hardwareIdentifier; // copy hardware identifier
#ifndef APP_DEBUG // when not debugging => set to "not initialized"
splitflap->initialized = false; splitflap->initialized = false;
#else // when debugging => set to true #else // when debugging => set to "initialized"
splitflap->initialized = true; splitflap->initialized = true;
#endif #endif
// check if queue was created // check if queue was created
if(splitflap->flapQueue == NULL){ if(splitflap->flapQueue == NULL){
McuLog_error("Flap queue creation failed for Splitflap <%i>", id); McuLog_error("Flap queue creation failed for Splitflap <%i>", splitflap->id);
for(;;){} for(;;){}
} }
// if yes: add queue to registry // if yes: add queue to registry
@ -88,7 +86,7 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){
// check if queue was created // check if queue was created
if(splitflap->ongoingMoveMutex == NULL){ if(splitflap->ongoingMoveMutex == NULL){
McuLog_error("Ongoing Move mutex creation failed for Splitflap <%i>", id); McuLog_error("Ongoing Move mutex creation failed for Splitflap <%i>", splitflap->id);
for(;;){} for(;;){}
} }
// if yes: add ongoing move mutex to registry // if yes: add ongoing move mutex to registry
@ -103,7 +101,7 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){
NULL); NULL);
if(res != pdPASS) // task creation not successful? if(res != pdPASS) // task creation not successful?
{ {
McuLog_error("Creation of task for Splitflap <%i> failed", id); McuLog_error("Creation of task for Splitflap <%i> failed", splitflap->id);
for(;;) {} // Endless loop for(;;) {} // Endless loop
} }
@ -111,18 +109,34 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){
} }
void SF_Deinit(SF_Handle_t instance){ void SF_Deinit(SF_Handle_t instance){
McuLog_warn("Deinitializing Splitflap <%i>", ((SF_t*)instance)->id);
vSemaphoreDelete(((SF_t*)instance)->ongoingMoveMutex); vSemaphoreDelete(((SF_t*)instance)->ongoingMoveMutex);
vQueueDelete(((SF_t*)instance)->flapQueue);
((SF_t*)instance)->initialized = false; ((SF_t*)instance)->initialized = false;
McuULN2003_DeinitMotor(((SF_t*)instance)->motor); McuULN2003_DeinitMotor(((SF_t*)instance)->motor);
McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor); McuGPIO_DeinitGPIO(((SF_t*)instance)->magSensor);
#if SPLITFLAP_CONFIG_USE_FREERTOS_HEAP
vPortFree((SF_t*)instance); // free FreeRTOS heap
#else
free((SF_t*)instance); // free MCU heap
#endif
} }
/**********************/ /**********************/
/* PUBLIC FUNCTIONS */ /* PUBLIC FUNCTIONS */
/**********************/ /**********************/
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){ bool SF_MoveMotorToZeroPosition(SF_Handle_t instance){
int numStepsMoved = 0; int numStepsMoved = 0;
uint16_t offsetSteps = 0;
// check if the hwId needs an offset init position
for (int i = 0; i < sizeof(offsetStepsPerHw)/sizeof(offsetStepsPerHw[0]); ++i) {
if(offsetStepsPerHw[i][0] == ((SF_t*)instance)->hwId){
offsetSteps = offsetStepsPerHw[i][1];
McuLog_info("SF_MoveMotorToZeroPosition found offsetSteps=%i for Splitflap <%i> with hwId=%i", offsetSteps, ((SF_t*)instance)->id, ((SF_t*)instance)->hwId);
break;
}
}
if(((SF_t*)instance)->initialized){ if(((SF_t*)instance)->initialized){
McuLog_warn("Tried to initialize motor of Splitflap <%i>, but already initialized => skipping", ((SF_t*)instance)->id); McuLog_warn("Tried to initialize motor of Splitflap <%i>, but already initialized => skipping", ((SF_t*)instance)->id);
@ -158,10 +172,16 @@ bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){
// success if less than one rotation // success if less than one rotation
((SF_t*)instance)->initialized = (numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND); ((SF_t*)instance)->initialized = (numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND);
McuLog_info("SF_MoveMotorToZeroPosition finished for Splitflap <%i>, success=%s", ((SF_t*)instance)->id, ((SF_t*)instance)->initialized ? "true" : "false");
return ((SF_t*)instance)->initialized; return ((SF_t*)instance)->initialized;
} }
void SF_MoveMotorToZeroPositionAsync(SF_Handle_t instance){
// TODO TO IMPLEMENT
}
void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){ void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){
if(((SF_t*)instance)->initialized){ if(((SF_t*)instance)->initialized){
if(OngoingMoveMutex_Lock(instance)){ if(OngoingMoveMutex_Lock(instance)){
@ -233,7 +253,7 @@ static void SF_Task(void *pv){
SF_Handle_t instance = (SF_Handle_t)pv; SF_Handle_t instance = (SF_Handle_t)pv;
SF_t* splitflap = (SF_t*)instance; SF_t* splitflap = (SF_t*)instance;
Flap_t nextFlap = " "; Flap_t nextFlap = " ";
McuLog_info("Splitflap: Task for Splitflap <%i> started.\n", splitflap->id); McuLog_info("Splitflap: Task for Splitflap <%i> started.", splitflap->id);
for(;;){ for(;;){
// delay task // delay task
@ -256,7 +276,7 @@ static void SF_Task(void *pv){
if(OngoingMoveMutex_Lock(instance)){ if(OngoingMoveMutex_Lock(instance)){
// now get the queue item // now get the queue item
if(xQueueReceive(splitflap->flapQueue, &nextFlap, 0) != pdPASS){ if(xQueueReceive(splitflap->flapQueue, &nextFlap, 0) != pdPASS){
/* failed to receive => queue empty but previously an iteam was there? error */ /* failed to receive => queue empty but previously an item was there? error */
McuLog_error("Failed to receive Queue item of Splitflap <%i>.", splitflap->id); McuLog_error("Failed to receive Queue item of Splitflap <%i>.", splitflap->id);
OngoingMoveMutex_Unlock(instance); OngoingMoveMutex_Unlock(instance);
continue; // ignore & proceed continue; // ignore & proceed
@ -266,6 +286,7 @@ static void SF_Task(void *pv){
McuLog_info("Splitflap <%i> moving to flap '%c'.", splitflap->id, nextFlap[0]); McuLog_info("Splitflap <%i> moving to flap '%c'.", splitflap->id, nextFlap[0]);
SF_MoveToFlap(instance, nextFlap); SF_MoveToFlap(instance, nextFlap);
McuLog_info("Splitflap <%i> move done to flap '%c'.", splitflap->id, nextFlap[0]); McuLog_info("Splitflap <%i> move done to flap '%c'.", splitflap->id, nextFlap[0]);
vTaskDelay(pdMS_TO_TICKS(100)); // wait for logger to finish
OngoingMoveMutex_Unlock(instance); OngoingMoveMutex_Unlock(instance);
} }

@ -15,7 +15,6 @@
#include "lib/dict.h" #include "lib/dict.h"
#include <string.h> #include <string.h>
/****** SETTINGS ******/ /****** SETTINGS ******/
#define SPLITFLAP_CONFIG_USE_FREERTOS_HEAP 0 #define SPLITFLAP_CONFIG_USE_FREERTOS_HEAP 0
#define SPLITFLAP_STEPS_ONE_ROUND 512 #define SPLITFLAP_STEPS_ONE_ROUND 512
@ -30,8 +29,15 @@ typedef void* SF_Handle_t;
/* descriptor to identify the flap */ /* descriptor to identify the flap */
typedef char* Flap_t; typedef char* Flap_t;
/* descriptor to identify the hardware */
typedef uint8_t HardwareIdentifier_t;
/* descriptor to identify the instance in the setup */
typedef uint8_t SetupIdentifier_t;
typedef struct { typedef struct {
int id; SetupIdentifier_t id;
HardwareIdentifier_t hwId;
McuULN2003_Handle_t motor; McuULN2003_Handle_t motor;
McuGPIO_Handle_t magSensor; McuGPIO_Handle_t magSensor;
SemaphoreHandle_t ongoingMoveMutex; SemaphoreHandle_t ongoingMoveMutex;
@ -42,6 +48,8 @@ typedef struct {
typedef struct { typedef struct {
McuULN2003_Config_t motorConfig; McuULN2003_Config_t motorConfig;
McuGPIO_Config_t magSensorConfig; McuGPIO_Config_t magSensorConfig;
HardwareIdentifier_t hardwareIdentifier;
SetupIdentifier_t setupIdentifier;
} SF_Config_t; } SF_Config_t;
/****** FUNCTIONS ******/ /****** FUNCTIONS ******/
@ -51,17 +59,26 @@ void SF_InitConfig(void);
/* free splitflap flaps dictionary */ /* free splitflap flaps dictionary */
void SF_DeInitConfig(void); void SF_DeInitConfig(void);
/* split flap initialization /* instantiate new split flap
* only pass configured types, they do not need to be initialized. * only pass configured types, they do not need to be initialized.
* Initialization will be made inside the SF_Init method */ * Initialization will be made inside the SF_Init method */
SF_Handle_t SF_Init(SF_Config_t* instance, int id); SF_Handle_t SF_Init(SF_Config_t* instance);
/* kill instance of splitflap,
* free memory etc. */
void SF_Deinit(SF_Handle_t instance);
/* Moves split flap slowly to zero position.
* returns true if the move was successful, false if not
* (depending on if the sensor was hit before one round)
* After moving to zero position, the current position of the motor is set to 0 */
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance);
/* Moves split flap slowly to zero position. /* Moves split flap slowly to zero position.
* returns true if the move was successful, false if not * returns true if the move was successful, false if not
* (depending on if the sensor was hit before one round) * (depending on if the sensor was hit before one round)
* offsetSteps: amount of steps to offset from magnet sensor positive flank position
* After moving to zero position, the current position of the motor is set to 0 */ * After moving to zero position, the current position of the motor is set to 0 */
bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps); void SF_MoveMotorToZeroPositionAsync(SF_Handle_t instance);
/* split flap moves number of steps (uint, since only forward moves are allowed) */ /* split flap moves number of steps (uint, since only forward moves are allowed) */
void SF_MoveSteps(SF_Handle_t instance, uint32_t steps); void SF_MoveSteps(SF_Handle_t instance, uint32_t steps);
@ -78,7 +95,6 @@ void SF_MoveToFlapAsync(SF_Handle_t instance, Flap_t flap);
/* get current position of motor */ /* get current position of motor */
int32_t SF_GetMotorPosition(SF_Handle_t instance); int32_t SF_GetMotorPosition(SF_Handle_t instance);
/* split flap deinitialization */
void SF_Deinit(SF_Handle_t instance);
#endif /* SPLITFLAP_H_ */ #endif /* SPLITFLAP_H_ */

@ -0,0 +1,24 @@
/*
* splitflap_positions.h
*
* Created on: 16.10.2022
* Author: jonas
*/
#ifndef SPLITFLAP_POSITIONS_H_
#define SPLITFLAP_POSITIONS_H_
#include "splitflap.h"
/* all letters of the splitflap in the correct order */
Flap_t 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",
"!", "?", ":"};
/* registers offset steps for initialization per hwId. [0] = hwId. [1] = offsetSteps */
HardwareIdentifier_t offsetStepsPerHw[][2] = { {33, 12}, {35, 20}};
#endif /* SPLITFLAP_POSITIONS_H_ */
Loading…
Cancel
Save