motors.c
/*
* motors.c
*
* Created on: 21 Apr 2019
* Author: Windows 7
*/
#include "main.h"
#include "adc.h"
#include "tim.h"
#include "gpio.h"
#include "motors.h"
#include "core_cm3.h"
#include "system_stm32f1xx.h"
#define MOTORDELAY 5
#define TURN_TRIGGER 20
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;
uint32_t adcValueArray[3];
//void turn(motors_t motor, step_t step)
//{
// switch (step)
// {
// case one:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case two:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case three:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,SET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,RESET);
// HAL_Delay(MOTORDELAY);
// break;
// case four:
// HAL_GPIO_WritePin(motor.pins.port_pin_one, motor.pins.pin_one,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_two, motor.pins.pin_two,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_three, motor.pins.pin_three,RESET);
// HAL_GPIO_WritePin(motor.pins.port_pin_four, motor.pins.pin_four,SET);
// HAL_Delay(MOTORDELAY);
// break;
// }
//
//}
void StartADCs()
{
HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adcValueArray, 3);
HAL_TIM_Base_Start(&htim3);
}
//
//void channel2_setup(motors_t pMotor)
//{
// pMotor.pins.port_pin_one = m2_s1_GPIO_Port;
// pMotor.pins.pin_one = m2_s1_Pin;
//
// pMotor.pins.port_pin_two = m2_s2_GPIO_Port;
// pMotor.pins.pin_two = m2_s2_Pin;
//
// pMotor.pins.port_pin_three = m2_s3_GPIO_Port;
// pMotor.pins.pin_three = m2_s3_Pin;
//
// pMotor.pins.port_pin_four = m2_s4_GPIO_Port;
// pMotor.pins.pin_four = m2_s4_Pin;
//
// pMotor.pins.port_end_one = m2_end1_GPIO_Port;
// pMotor.pins.end_one = m2_end1_Pin;
//
// pMotor.pins.port_end_two = m2_end2_GPIO_Port;
// pMotor.pins.end_two = m2_end2_Pin;
//
// pMotor.analogs.Instance = &hadc1;
// pMotor.analogs.channel = ADC_CHANNEL_1;
//
// pMotor.analogs.result = adcValueArray[1];
// pMotor.first_on = 1;
//
// pMotor.lastmovedvalue = 0;
//}
//
//void channel3_setup(motors_t pMotor)
//{
// pMotor.pins.port_pin_one = *m3_s1_GPIO_Port;
// pMotor.pins.pin_one = m3_s1_Pin;
//
// pMotor.pins.port_pin_two = m3_s2_GPIO_Port;
// pMotor.pins.pin_two = m3_s2_Pin;
//
// pMotor.pins.port_pin_three = m3_s3_GPIO_Port;
// pMotor.pins.pin_three = m3_s3_Pin;
//
// pMotor.pins.port_pin_four = m3_s4_GPIO_Port;
// pMotor.pins.pin_four = m3_s4_Pin;
//
// pMotor.pins.port_end_one = m3_end1_GPIO_Port;
// pMotor.pins.end_one = m3_end1_Pin;
// pMotor.pins.port_end_two = m3_end2_GPIO_Port;
// pMotor.pins.end_two = m3_end2_Pin;
//
// pMotor.analogs.Instance = &hadc1;
// pMotor.analogs.channel = ADC_CHANNEL_2;
//
// pMotor.analogs.result = adcValueArray[2];
// pMotor.first_on = 1;
// pMotor.lastmovedvalue = 0;
//}
//
//
//void collect_position(motors_t * pMotor)
//{
// static uint8_t i = 0;
// uint32_t total;
// //pMotor->analogs.result = adcValueArray[1]; //temporray
// pMotor->analogs.position.previousResults[i] = pMotor->analogs.result;//adcValueArray[1];//
//
// i++;
//
// if (pMotor->first_on > 0)
// {
// for (int j = 0; j <12; j++)
// {
// pMotor->analogs.position.previousResults[j] = pMotor->analogs.result;
//
// }
// pMotor->first_on = 0;
// }
//
// if (i >= 12)
// {
//
// i = 0;
// }
//
// for (int k = 0; k < 12; k++)
// {
// total = pMotor->analogs.position.previousResults[k]; //pMotor->analogs.position.previousResults[k];
// }
//
// pMotor->analogs.averaged = total/11;
//}
//
//
//
//void has_turn(motors_t * motor)
//{
//
// int32_t test = motor->lastmovedvalue - (motor->analogs.averaged);
//
// if (test >= TURN_TRIGGER )
// {
//
// for (int z = test; z < 0; z--)
// {
//
// turn(motor,motor->step);
//
// motor->step--;
//
// if (motor->step > 3)
// {
// motor->step = 3;
// }
//
// }
// }
//
// if (test <= TURN_TRIGGER)
// {
// for (int z = 0; z > test; z++)
// {
// turn(motor, motor->step);
//
// motor->step++;
//
// if (motor->step > 3)
// {
// motor->step = 0;
// }
// }
// }
//
// motor->lastmovedvalue = motor->analogs.averaged;
//
//}
//