Nawor3565

New motor func

Aug 14th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. // Motor "0" means magnitude is applied to all motors
  2. void setMotorPWM(uint8_t motor_number, uint32_t motor_magnitude){
  3.     uint8_t motor_gpio_pins = 0;
  4.    
  5.     if (motor_magnitude > 100) {
  6.         motor_gpio_pins = 1;
  7.     }
  8.     if (motor_magnitude != 0) {
  9.         motor_magnitude = motor_magnitude + 50;
  10.     }
  11.    
  12.  
  13.     switch (motor_number) {
  14.         case 0:
  15.             TIM1->CCR1 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  16.             TIM2->CCR2 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  17.             TIM2->CCR3 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  18.             HAL_GPIO_WritePin(MOTOR_4_GPIO_Port, MOTOR_4_Pin, motor_gpio_pins);
  19.             HAL_GPIO_WritePin(MOTOR_5_GPIO_Port, MOTOR_5_Pin, motor_gpio_pins);
  20.             break;
  21.         case 1:
  22.             TIM1->CCR1 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  23.             break;
  24.         case 2:
  25.             TIM2->CCR2 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  26.             break;
  27.         case 3:
  28.             TIM2->CCR3 = (uint16_t)(MAX_PWM_PULSE_COUNT * ((float)motor_magnitude / 255.0));
  29.             break;
  30.         case 4:
  31.             HAL_GPIO_WritePin(MOTOR_4_GPIO_Port, MOTOR_4_Pin, motor_gpio_pins);
  32.             break;
  33.         case 5:
  34.             HAL_GPIO_WritePin(MOTOR_5_GPIO_Port, MOTOR_5_Pin, motor_gpio_pins);
  35.             break;
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment