예제 #1
0
파일: motor.c 프로젝트: loc12345pro/Robot
void Motor_Init(motor_t* p_motor)
{
    if (p_motor != NULL)
    {
        // Preparations
        GPIO_InitTypeDef gpio_init;
        GPIO_StructInit(&gpio_init);
        
        // Enable clock 
        GPIO_EnableClock(p_motor->gpio_port);
        
        // Initialize GPIO
        gpio_init.GPIO_Mode = GPIO_Mode_OUT;
        gpio_init.GPIO_OType = GPIO_OType_PP;
        gpio_init.GPIO_Pin = (p_motor->gpio_direction_pin_1) | (p_motor->gpio_direction_pin_2);
        gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
        gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
        
        GPIO_Init(p_motor->gpio_port, &gpio_init);
        
        // Go into brake state
        TM_PWM_SetChannelPercent(&(p_motor->tim_data), p_motor->pwm_channel, p_motor->speed);
        Motor_Brake(p_motor);
    }
}
예제 #2
0
파일: motor.c 프로젝트: loc12345pro/Robot
/*
 * Name: Motor_SetSpeedLevel
 * Module: Motor
 * Parameters:  speedPercent: 0-100%
 * Description: Set motor's speed level
 * Return: void
 */
void Motor_SetSpeed(motor_t* p_motor, float speed)
{
    if ((p_motor != NULL) && (speed >= MOTOR_MIN_SPEED) && (speed <= MOTOR_MAX_SPEED))
    {
        p_motor->speed = speed;
        TM_PWM_SetChannelPercent(&(p_motor->tim_data), p_motor->pwm_channel, p_motor->speed);
    }
}
예제 #3
0
파일: main.c 프로젝트: Joonaskaru/stm32f429
int main(void) {
	TM_PWM_TIM_t TIM4_Data, TIM2_Data;
	
	/* Initialize system */
	SystemInit();

/* STM32F4-Discovery LEDS start */
/* Leds on PD12, PD13, PD14, PD15 */
	
	/* Set PWM to 1kHz frequency on timer TIM4 */
	/* 1 kHz = 1ms = 1000us */
	TM_PWM_InitTimer(TIM4, &TIM4_Data, 1000);
	
	/* Initialize PWM on TIM4, Channel 1 and PinsPack 2 = PD12 */
	TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_1, TM_PWM_PinsPack_2);
	/* Initialize PWM on TIM4, Channel 2 and PinsPack 2 = PD13 */
	TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_2, TM_PWM_PinsPack_2);
	/* Initialize PWM on TIM4, Channel 3 and PinsPack 2 = PD14 */
	TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_3, TM_PWM_PinsPack_2);
	/* Initialize PWM on TIM4, Channel 4 and PinsPack 2 = PD15 */
	TM_PWM_InitChannel(&TIM4_Data, TM_PWM_Channel_4, TM_PWM_PinsPack_2);
	
	/* Set channel 1 value, 50% duty cycle */
	TM_PWM_SetChannel(&TIM4_Data, TM_PWM_Channel_1, TIM4_Data.Period / 2);
	/* Set channel 2 value, 33% duty cycle */
	TM_PWM_SetChannel(&TIM4_Data, TM_PWM_Channel_2, TIM4_Data.Period / 3);
	/* Set channel 3 value, 25% duty cycle */
	TM_PWM_SetChannel(&TIM4_Data, TM_PWM_Channel_3, TIM4_Data.Period / 4);
	/* Set channel 4 value, 5% duty cycle*/
	TM_PWM_SetChannelPercent(&TIM4_Data, TM_PWM_Channel_4, 5);
/* STM32F4-Discovery LEDS stop */

/* Nucleo F4(0/1)1-RE LED start */
/* Led connected to PA5 */

	/* Set PWM to 1kHz frequency on timer TIM2 */
	/* 1 kHz = 1ms = 1000us */
	TM_PWM_InitTimer(TIM2, &TIM2_Data, 1000);
	
	/* Initialize PWM on TIM2, Channel 1 and PinsPack 2 = PA5 */
	TM_PWM_InitChannel(&TIM2_Data, TM_PWM_Channel_1, TM_PWM_PinsPack_2);
	
	/* Set channel 1 value, 500us pulse high = 500 / 1000 = 0.5 = 50% duty cycle */
	TM_PWM_SetChannelMicros(&TIM2_Data, TM_PWM_Channel_1, 10);
	
/* Nucleo F4(0/1)1-RE LED stop */

	while (1) {
		
	}
}
예제 #4
0
파일: motor.c 프로젝트: loc12345pro/Robot
/*
 * Name: Motor_ChangeSpeed
 * Module: Motor
 * Parameters:  increase: > 0 to increase speed, <= 0 to decrease speed
 * Description: Change motor's speed up or down
 * Return: void
 */
void Motor_ChangeSpeed(motor_t* p_motor, uint8_t increase)
{
    if ((p_motor != NULL))
    {
        if (increase > 0)
        {
            p_motor->speed += MOTOR_SPEED_CHANGE_INTERVAL;
        }
        else
        {
            p_motor->speed -= MOTOR_SPEED_CHANGE_INTERVAL;            
        }
        
        TM_PWM_SetChannelPercent(&(p_motor->tim_data), p_motor->pwm_channel, p_motor->speed);
    }
}
예제 #5
0
int main(void) {

    int accelData[3];
    int analogData[BUFFER];
    int i=0;
    for(i=0;i<BUFFER;i++){ analogData[i]=0;	}
    int a = 0;
    int analogIn = 0;
    int analogMin, analogMax;

    /* Initialize system */
    SystemInit();

    /* Initialize delay */
    //TM_DELAY_Init();

    /* Initialize PG13 (GREEN LED) and PG14 (RED LED) */
    TM_GPIO_Init(GPIOG, GPIO_PIN_13 | GPIO_PIN_14, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Fast);
    TM_GPIO_SetPinValue(GPIOG, GPIO_PIN_14, 1); // Red: ON

#ifdef ENABLE_USART
    /* Initialize USART1 at 115200 baud, TX: PA10, RX: PA9 */
    TM_USART_Init(USART1, TM_USART_PinsPack_1, 115200);
#endif

#ifdef ENABLE_VCP
    /* Initialize USB Virtual Comm Port */

    TM_USB_VCP_Result status = TM_USB_VCP_NOT_CONNECTED;
    while (TM_USB_VCP_GetStatus() != TM_USB_VCP_CONNECTED) {
    	TM_USB_VCP_Init();
    	TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_14);
    	Delay(500000);
    }
    SendString("USB VCP initialized and connected\n");
    TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_14 | GPIO_PIN_13); // Red: OFF, Gr: ON

#endif

#ifdef ENABLE_MMA

    /* Initialize MMA845X */
    uint8_t mma_status = MMA845X_Initialize(MMA_RANGE_4G);
    if (mma_status == MMA_OK) {
    	SendString("MMA initialized\n");
    } else {
    	SendString("MMA initialization failed, error code: ");
    	// Add 48 to the byte value to have character representation, (48 = '0')
    	SendChar('0'+mma_status);
    	SendChar('\n');
    }

#endif

    /* Initialize Display */
	TM_ILI9341_Init();
	TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_1);
	TM_ILI9341_SetLayer1();
	TM_ILI9341_Fill(ILI9341_COLOR_BLACK); /* Fill data on layer 1 */

	/* Initialize ADC1 */
	TM_ADC_Init(CURRENT_ADC, CURRENT_CH);

	/* Initialize PE2 and PE3 for digital output (Motor direction) */
    TM_GPIO_Init(GPIOE, GPIO_PIN_2 | GPIO_PIN_3, TM_GPIO_Mode_OUT, TM_GPIO_OType_PP, TM_GPIO_PuPd_NOPULL, TM_GPIO_Speed_Fast);
    // Set them to HIGH/LOW
    TM_GPIO_SetPinHigh(GPIOE, GPIO_PIN_3);
    TM_GPIO_SetPinLow(GPIOE, GPIO_PIN_2);

#ifdef ENABLE_PWM
    /* Set up PE5 (in front of PE4) for PWM (TIM9 CH1 PP2) (Motor speed control) */
    TM_PWM_TIM_t TIM9_Data;
    // Set PWM to 1kHz frequency on timer TIM4, 1 kHz = 1ms = 1000us
	TM_PWM_InitTimer(TIM9, &TIM9_Data, 1000);
	// Initialize PWM on TIM9, Channel 1 and PinsPack 2 = PE5
	TM_PWM_InitChannel(&TIM9_Data, TM_PWM_Channel_1, TM_PWM_PinsPack_2);
	// Set channel 1 value, 50% duty cycle
	TM_PWM_SetChannelPercent(&TIM9_Data, TM_PWM_Channel_1, 50);
#endif

	/* Initialize DAC channel 2, pin PA5 (Shaker control) */
	//TM_DAC_Init(TM_DAC2);
	/* Set 12bit analog value of 2047/4096 * 3.3V */
	//TM_DAC_SetValue(TM_DAC2, 4096);

#ifdef ENABLE_DAC
	// DAC PIN PA5
	/* Initialize DAC1, use TIM4 for signal generation */
	TM_DAC_SIGNAL_Init(TM_DAC2, TIM4);
	/* Output predefined triangle signal with frequency of 5kHz */
	TM_DAC_SIGNAL_SetSignal(TM_DAC2, TM_DAC_SIGNAL_Signal_Sinus, 50);
#endif

	/* MAIN LOOP */
    while (1) {

    	// Read acceleration data
#ifdef ENABLE_MMA
		MMA845X_ReadAcceleration(accelData);
#endif

		// Read analog input
		analogData[a] = TM_ADC_Read(CURRENT_ADC, CURRENT_CH);
		a++;
		if(a==BUFFER) {a=0;}

		// Analog average
		analogIn=0;
		analogMax=0;
		analogMin=4096;
		for(i=0;i<BUFFER;i++){
			if(analogData[i] > analogMax) { analogMax = analogData[i]; }
			if(analogData[i] < analogMin) { analogMin = analogData[i]; }
			analogIn+=analogData[i];
		}
		analogIn/=BUFFER;

		// Print graphs
		printGraphsLCD(accelData, analogData[a], analogIn, analogMin, analogMax);

		// Toggle Green led
		TM_GPIO_TogglePinValue(GPIOG, GPIO_PIN_13);

    }
}