cleaned overall code
remaining warning is pin redefinition, can be ignored added working motor init parameters added function descriptions added sd card functionality (commented out because of hardware problems) added adc functionality (implemented but not used because of hardware problems)
This commit is contained in:
+156
-135
@@ -49,6 +49,7 @@
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
SPI_HandleTypeDef hspi1;
|
||||
ADC_HandleTypeDef hadc1;
|
||||
|
||||
RTC_HandleTypeDef hrtc;
|
||||
|
||||
UART_HandleTypeDef huart2;
|
||||
@@ -72,12 +73,29 @@ bool winterTime = true;
|
||||
int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
int DaysInMonthLeapYear[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||||
bool leapYear = false;
|
||||
int singleStepsFor180Deg = 100; // The stepper motor needs 200 single steps for 360 deg, equals 100 steps for 180 deg
|
||||
int stepsFor180Deg = 1600; // The stepper motor needs 200 single steps for 360 deg, equals 100 steps for 180 deg, 180 Deg in 1/16 steps equals 1600 1/16 steps
|
||||
int leapsFor180Deg = 5; // Determines how big the amount of single steps is to complete 180 degrees of rotation
|
||||
bool alarmSunriseFlag = false;
|
||||
bool alarmSunsetFlag = false;
|
||||
bool makeStepFlag = false;
|
||||
|
||||
/* Initialization parameters. */
|
||||
l6208_Init_t initDeviceParameters =
|
||||
{
|
||||
1500, //Acceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
|
||||
20, //Acceleration current torque in % (from 0 to 100)
|
||||
1500, //Deceleration rate in step/s^2 or (1/16)th step/s^2 for microstep modes
|
||||
20, //Deceleration current torque in % (from 0 to 100)
|
||||
1500, //Running speed in step/s or (1/16)th step/s for microstep modes
|
||||
10, //Running current torque in % (from 0 to 100)
|
||||
5, //Holding current torque in % (from 0 to 100)
|
||||
STEP_MODE_1_16, //Step mode via enum motorStepMode_t
|
||||
FAST_DECAY, //Decay mode via enum motorDecayMode_t
|
||||
0, //Dwelling time in ms
|
||||
FALSE, //Automatic HIZ STOP
|
||||
100000 //VREFA and VREFB PWM frequency (Hz)
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int hours;
|
||||
int minutes;
|
||||
@@ -88,15 +106,6 @@ typedef struct {
|
||||
int year;
|
||||
} timeAndDate;
|
||||
|
||||
typedef struct {
|
||||
char hours[10];
|
||||
char minutes[10];
|
||||
char seconds[10];
|
||||
char *fullTimeStamp;
|
||||
} timeStamp;
|
||||
|
||||
timeStamp time;
|
||||
|
||||
// SD CARD Variables
|
||||
FATFS fs;
|
||||
FATFS *pfs;
|
||||
@@ -119,6 +128,7 @@ static void MX_RTC_Init(void);
|
||||
static void MX_SPI1_Init(void);
|
||||
static void MX_ADC1_Init(void);
|
||||
static void MyFlagInterruptHandler(void);
|
||||
void MyErrorHandler(uint16_t error);
|
||||
void ButtonHandler(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
@@ -381,8 +391,8 @@ void calc_tomorrows_date(timeAndDate* initialDate, timeAndDate* tomorrowsDate)
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : set_Alarm
|
||||
* Description : sets the wake up Alarm
|
||||
* Function Name : set_alarm
|
||||
* Description : sets alarm A or B
|
||||
*******************************************************************************/
|
||||
void set_alarm(int h, int min, int weekDay, char* alarm, RTC_AlarmTypeDef* alarmInstance)
|
||||
{
|
||||
@@ -413,37 +423,19 @@ void set_alarm(int h, int min, int weekDay, char* alarm, RTC_AlarmTypeDef* alarm
|
||||
|
||||
}
|
||||
|
||||
void get_time(timeStamp *time){
|
||||
|
||||
char str[200];
|
||||
|
||||
if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK)
|
||||
{
|
||||
sprintf(time->hours, "%d", sTime.Hours);
|
||||
sprintf(time->minutes, "%d", sTime.Minutes);
|
||||
sprintf(time->seconds, "%d", sTime.Seconds);
|
||||
}
|
||||
|
||||
strcpy(str, time->hours);
|
||||
strcat(str, ":");
|
||||
strcat(str, time->minutes);
|
||||
strcat(str, ":");
|
||||
strcat(str, time->seconds);
|
||||
strcat(str, " | ");
|
||||
|
||||
time->fullTimeStamp = str;
|
||||
}
|
||||
|
||||
// sending to UART
|
||||
/*******************************************************************************
|
||||
* Function Name : transmit_uart
|
||||
* Description : Transmit a string over uart
|
||||
*******************************************************************************/
|
||||
void transmit_uart(char *string){
|
||||
//char str[200];
|
||||
//get_time(&time);
|
||||
//strcat(time.fullTimeStamp, string);
|
||||
//strcpy(str, time.fullTimeStamp);
|
||||
uint8_t len = strlen(string);
|
||||
HAL_UART_Transmit(&huart2, (uint8_t*) string, len, 200);
|
||||
}
|
||||
|
||||
/***************************************************************************************************
|
||||
* Function Name : set_time_and_date
|
||||
* Description : Overwriting the date given in timeanddate with the current time and date from rtc
|
||||
****************************************************************************************************/
|
||||
void set_time_and_date(timeAndDate *timeanddate){
|
||||
|
||||
if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK)
|
||||
@@ -462,6 +454,11 @@ void set_time_and_date(timeAndDate *timeanddate){
|
||||
}
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : calc_interval_duration
|
||||
* Description : Calculate the duration between the two given time/dates
|
||||
* Return : The duration as an integer
|
||||
*******************************************************************************/
|
||||
int calc_interval_duration(timeAndDate *sunrise, timeAndDate *sunset){
|
||||
int duration_h=0;
|
||||
int duration_m=0;
|
||||
@@ -477,6 +474,10 @@ int calc_interval_duration(timeAndDate *sunrise, timeAndDate *sunset){
|
||||
return (duration_h * 60 + duration_m) / leapsFor180Deg;
|
||||
}
|
||||
|
||||
/*******************************************************************************
|
||||
* Function Name : generate_filename
|
||||
* Description : Short function to generate a filename with the current date
|
||||
*******************************************************************************/
|
||||
void generate_filename(timeAndDate *date){
|
||||
sprintf(filename, "%02d%02d%02d.txt", date->year, date->month, date->day);
|
||||
}
|
||||
@@ -489,69 +490,93 @@ void generate_filename(timeAndDate *date){
|
||||
*/
|
||||
int main(void)
|
||||
{
|
||||
/* USER CODE BEGIN 1 */
|
||||
/* USER CODE BEGIN 1 */
|
||||
|
||||
/* USER CODE END 1 */
|
||||
/* USER CODE END 1 */
|
||||
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
/* MCU Configuration--------------------------------------------------------*/
|
||||
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
||||
HAL_Init();
|
||||
|
||||
/* USER CODE BEGIN Init */
|
||||
/* USER CODE BEGIN Init */
|
||||
|
||||
/* USER CODE END Init */
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
|
||||
/* USER CODE BEGIN SysInit */
|
||||
/* USER CODE BEGIN SysInit */
|
||||
|
||||
/* USER CODE END SysInit */
|
||||
/* USER CODE END SysInit */
|
||||
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_RTC_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_FATFS_Init();
|
||||
MX_ADC1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
/* Initialize all configured peripherals */
|
||||
MX_GPIO_Init();
|
||||
MX_USART2_UART_Init();
|
||||
MX_RTC_Init();
|
||||
MX_SPI1_Init();
|
||||
MX_FATFS_Init();
|
||||
MX_ADC1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
//######### Inits of the Motor control library #########
|
||||
//######### Inits of the Motor control library #########
|
||||
|
||||
/* Set the L6208 library to use 1 device */
|
||||
BSP_MotorControl_SetNbDevices(BSP_MOTOR_CONTROL_BOARD_ID_L6208, 1);
|
||||
BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_L6208, NULL);
|
||||
/* Set the L6208 library to use 1 device */
|
||||
BSP_MotorControl_SetNbDevices(BSP_MOTOR_CONTROL_BOARD_ID_L6208, 1);
|
||||
|
||||
/* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */
|
||||
BSP_MotorControl_AttachFlagInterrupt(MyFlagInterruptHandler);
|
||||
BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_L6208, &initDeviceParameters);
|
||||
|
||||
/* Attach the function MyErrorHandler (defined below) to the error Handler*/
|
||||
BSP_MotorControl_AttachErrorHandler(Error_Handler);
|
||||
//BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_L6208, NULL); //Default params
|
||||
|
||||
/* Set Systick Interrupt priority highest to ensure no lock by using HAL_Delay */
|
||||
HAL_NVIC_SetPriority(SysTick_IRQn, 0x0, 0x0);
|
||||
/* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */
|
||||
BSP_MotorControl_AttachFlagInterrupt(MyFlagInterruptHandler);
|
||||
|
||||
/* Configure KEY Button */
|
||||
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
|
||||
/* Attach the function MyErrorHandler (defined below) to the error Handler*/
|
||||
BSP_MotorControl_AttachErrorHandler(MyErrorHandler);
|
||||
|
||||
/* Disable the power bridges after initialization */
|
||||
BSP_MotorControl_CmdDisable(0);
|
||||
/* Set Systick Interrupt priority highest to ensure no lock by using HAL_Delay */
|
||||
HAL_NVIC_SetPriority(SysTick_IRQn, 0x0, 0x0);
|
||||
|
||||
//######### Mount SD-Card #########
|
||||
/* Configure KEY Button */
|
||||
//BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
|
||||
|
||||
fres = f_mount(&fs, "", 0);
|
||||
if (fres == FR_OK) {
|
||||
transmit_uart("SD card is mounted successfully!\r\n");
|
||||
} else if (fres != FR_OK) {
|
||||
transmit_uart("SD card is not mounted!\r\n");
|
||||
}
|
||||
/* Disable the power bridges after initialization */
|
||||
BSP_MotorControl_CmdDisable(0);
|
||||
|
||||
//######### Variable inits #########
|
||||
int32_t pos=0;
|
||||
uint32_t freqPwm=0;
|
||||
|
||||
timeAndDate sunrise, sunset, wakeUpTimeForStep, tomorrowsDate, initialDate;
|
||||
sunrise = sunset = wakeUpTimeForStep = tomorrowsDate = initialDate = (timeAndDate) {\
|
||||
freqPwm = BSP_MotorControl_GetBridgeInputPwmFreq(0);
|
||||
|
||||
BSP_MotorControl_SetBridgeInputPwmFreq(0, freqPwm>>1);
|
||||
|
||||
// current position
|
||||
pos = BSP_MotorControl_GetPosition(0);
|
||||
|
||||
// set current position to home
|
||||
BSP_MotorControl_SetHome(0, pos);
|
||||
|
||||
//######### Mount SD-Card #########
|
||||
/* The SD Card is not working at the moment due to getting FR_NOT_READY
|
||||
* when connecting the SD Card Pins with the SPI Pins on the mounted X-NUCLEO Motor driver.
|
||||
* When dismounting the motor driver and connecting it via cables, there is unusual behaviour of the motor mnovement.
|
||||
* The SD Card though, works when the SD Card Breakout is connected directly to the NUCLEO board.
|
||||
*
|
||||
fres = f_mount(&fs, "", 0);
|
||||
if (fres == FR_OK) {
|
||||
transmit_uart("SD card is mounted successfully!\r\n");
|
||||
} else if (fres != FR_OK) {
|
||||
transmit_uart("SD card is not mounted!\r\n");
|
||||
}
|
||||
*/
|
||||
|
||||
//######### Start ADC Conversion #########
|
||||
HAL_ADC_Start(&hadc1);
|
||||
|
||||
//######### Variable inits #########
|
||||
|
||||
timeAndDate sunrise, sunset, wakeUpTimeForStep, tomorrowsDate, initialDate;
|
||||
sunrise = sunset = wakeUpTimeForStep = tomorrowsDate = initialDate = (timeAndDate) {\
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
@@ -559,57 +584,31 @@ int main(void)
|
||||
0,
|
||||
0,
|
||||
0
|
||||
};
|
||||
};
|
||||
|
||||
int32_t pos=0;
|
||||
uint32_t freqPwm=0;
|
||||
uint32_t timeToNextStep_m=0;
|
||||
uint32_t alarmB_h = 0;
|
||||
uint32_t alarmB_m = 0;
|
||||
uint32_t alarmB_wd = 0;
|
||||
uint32_t stepsToMake = singleStepsFor180Deg / leapsFor180Deg; // The amount of single steps to make to complete 180/5 degrees
|
||||
uint32_t timeToNextStep_m=0;
|
||||
uint32_t alarmB_h = 0;
|
||||
uint32_t alarmB_m = 0;
|
||||
uint32_t alarmB_wd = 0;
|
||||
uint32_t stepsToMake = stepsFor180Deg / leapsFor180Deg; // The amount of single steps to make to complete 180/x degrees ^= 1600/x
|
||||
|
||||
/* USER CODE END 2 */
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
|
||||
freqPwm = BSP_MotorControl_GetBridgeInputPwmFreq(0);
|
||||
|
||||
BSP_MotorControl_SetBridgeInputPwmFreq(0, freqPwm>>1);
|
||||
|
||||
pos = BSP_MotorControl_GetPosition(0);
|
||||
|
||||
BSP_MotorControl_SetHome(0, pos);
|
||||
|
||||
BSP_MotorControl_SelectStepMode(0, STEP_MODE_FULL);
|
||||
|
||||
while (1)
|
||||
{
|
||||
//######### Start ADC #########
|
||||
|
||||
// Start ADC Conversion
|
||||
HAL_ADC_Start(&hadc1);
|
||||
|
||||
//######### Motor settings and motor test #########
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
|
||||
while (1)
|
||||
{
|
||||
HAL_Delay(2000);
|
||||
|
||||
transmit_uart("Resetting motor position and calculating new dates and times.\r\n");
|
||||
|
||||
BSP_MotorControl_Move(0, FORWARD, 300);
|
||||
BSP_MotorControl_WaitWhileActive(0);
|
||||
BSP_MotorControl_Move(0, FORWARD, 300);
|
||||
BSP_MotorControl_WaitWhileActive(0);
|
||||
BSP_MotorControl_Move(0, FORWARD, 300);
|
||||
BSP_MotorControl_WaitWhileActive(0);
|
||||
|
||||
BSP_MotorControl_GoHome(0);
|
||||
BSP_MotorControl_WaitWhileActive(0);
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
/* USER CODE BEGIN 3 */
|
||||
set_time_and_date(&initialDate);
|
||||
|
||||
leap_year_check(initialDate.year);
|
||||
@@ -618,6 +617,14 @@ int main(void)
|
||||
|
||||
generate_filename(&tomorrowsDate);
|
||||
|
||||
//######### SD Card Write #########
|
||||
|
||||
/* The SD Card is not working at the moment due to getting FR_NOT_READY
|
||||
* when connecting the SD Card Pins with the SPI Pins on the mounted X-NUCLEO Motor driver.
|
||||
* When dismounting the motor driver and connecting it via cables, there is unusual behaviour of the motor mnovement.
|
||||
* The SD Card though, works when the SD Card Breakout is connected directly to the NUCLEO board.
|
||||
*
|
||||
|
||||
// Open file with tomorrows date as file name
|
||||
fres = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE | FA_READ);
|
||||
if (fres == FR_OK) {
|
||||
@@ -626,15 +633,16 @@ int main(void)
|
||||
transmit_uart("File was not opened!\r\n");
|
||||
}
|
||||
|
||||
f_puts("Cancer", &fil);
|
||||
f_puts("Data", &fil);
|
||||
|
||||
/* Close file */
|
||||
//Close file
|
||||
fres = f_close(&fil);
|
||||
if (fres == FR_OK) {
|
||||
transmit_uart("File is closed.\r\n");
|
||||
} else if (fres != FR_OK) {
|
||||
transmit_uart("File was not closed.\r\n");
|
||||
}
|
||||
*/
|
||||
|
||||
//Calculate sunrise and sunset time for tomorrow
|
||||
calc_sunrise_sunset(&initialDate, &sunrise, &sunset, &tomorrowsDate);
|
||||
@@ -644,8 +652,8 @@ int main(void)
|
||||
sunrise.minutes = 0;
|
||||
sunrise.weekDay = 7;
|
||||
|
||||
sunset.hours = 14;
|
||||
sunset.minutes = 30;
|
||||
sunset.hours = 16;
|
||||
sunset.minutes = 00;
|
||||
sunset.weekDay = 7;
|
||||
|
||||
//Calculate the time for next motor step in minutes
|
||||
@@ -727,8 +735,8 @@ int main(void)
|
||||
|
||||
BSP_MotorControl_GoHome(0);
|
||||
BSP_MotorControl_WaitWhileActive(0);
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -785,6 +793,7 @@ void SystemClock_Config(void)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
* @brief RTC Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
@@ -1070,15 +1079,6 @@ void MyFlagInterruptHandler(void)
|
||||
BSP_MotorControl_CmdDisable(0);
|
||||
}
|
||||
|
||||
void ButtonHandler(void)
|
||||
{
|
||||
gButtonPressed = TRUE;
|
||||
/* Let 200 ms before clearing the IT for key debouncing */
|
||||
HAL_Delay(200);
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(KEY_BUTTON_PIN);
|
||||
HAL_NVIC_ClearPendingIRQ(KEY_BUTTON_EXTI_IRQn);
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
@@ -1096,6 +1096,27 @@ void Error_Handler(void)
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
|
||||
void MyErrorHandler(uint16_t error)
|
||||
{
|
||||
// Motor error handler
|
||||
/* Backup error number */
|
||||
gLastError = error;
|
||||
|
||||
/* Infinite loop */
|
||||
while(1)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonHandler(void)
|
||||
{
|
||||
gButtonPressed = TRUE;
|
||||
/* Let 200 ms before clearing the IT for key debouncing */
|
||||
HAL_Delay(200);
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(KEY_BUTTON_PIN);
|
||||
HAL_NVIC_ClearPendingIRQ(KEY_BUTTON_EXTI_IRQn);
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
|
||||
@@ -82,6 +82,7 @@ void HAL_MspInit(void)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
* @brief RTC MSP Initialization
|
||||
* This function configures the hardware resources used in this example
|
||||
* @param hrtc: RTC handle pointer
|
||||
|
||||
Reference in New Issue
Block a user