找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2602|回复: 0
打印 上一主题 下一主题
收起左侧

STM32平衡小车代码 可以直接用

[复制链接]
跳转到指定楼层
楼主
ID:290582 发表于 2018-3-11 23:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在其他网站找到的一份可以直接用的平衡车代码,大家可以参考一下

单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2017 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32f1xx_hal.h"
  36. #include "i2c.h"
  37. #include "spi.h"
  38. #include "tim.h"
  39. #include "usart.h"
  40. #include "gpio.h"

  41. /* USER CODE BEGIN Includes */
  42. #include "math.h"
  43. #include "attitude_pid.h"
  44. #include "MPU9250.h"
  45. #include "showwave.h"
  46. #include "dot_matrix.h"
  47. #include "attitude_pid.h"
  48. #include "moto_ctrl.h"
  49. /* USER CODE END Includes */

  50. /* Private variables ---------------------------------------------------------*/

  51. /* USER CODE BEGIN PV */
  52. /* Private variables ---------------------------------------------------------*/
  53. uint8_t g_count = 0;
  54. extern uint8_t g_sys_mode  ;
  55. extern mpu9250_t g_mpu9250 ;
  56. extern TIM_HandleTypeDef htim3;
  57. extern TIM_HandleTypeDef htim8;
  58. extern float Left_Motor_Control_K;
  59. extern float Right_Motor_Control_K;
  60. //static float Kp = 0.95;static float Ki = 0.04;static float Kd = 0.08;static float SetOff = 9 ;
  61. //static float Kp = 0.95;  static float Ki = 0.04;static float Kd = 0.05;static float SetOff = 3;
  62. static float Kp,Kd,Ki,SetOff;
  63. /* USER CODE END PV */

  64. /* Private function prototypes -----------------------------------------------*/
  65. void SystemClock_Config(void);
  66. void Error_Handler(void);

  67. /* USER CODE BEGIN PFP */
  68. /* Private function prototypes -----------------------------------------------*/

  69. /* USER CODE END PFP */

  70. /* USER CODE BEGIN 0 */

  71. /* USER CODE END 0 */

  72. int main(void)
  73. {

  74.   /* USER CODE BEGIN 1 */
  75.   uint16_t i = 0;

  76.   /* USER CODE END 1 */

  77.   /* MCU Configuration----------------------------------------------------------*/

  78.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  79.   HAL_Init();

  80.   /* Configure the system clock */
  81.   SystemClock_Config();

  82.   /* Initialize all configured peripherals */
  83.   MX_GPIO_Init();
  84.   MX_USART1_UART_Init();
  85.   MX_I2C1_Init();
  86.   MX_TIM8_Init();
  87.   MX_TIM3_Init();
  88.   MX_SPI3_Init();
  89.   MX_USART2_UART_Init();
  90.   MX_USART3_UART_Init();

  91.   /* USER CODE BEGIN 2 */
  92.         Motor_Stop();
  93.         dot_matrix_init() ;
  94.         InitSensor();
  95.         HAL_TIM_Base_Start_IT(&htim3);
  96. //        HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1);
  97. //        HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2);

  98.   /* USER CODE END 2 */

  99.   /* Infinite loop */
  100.   /* USER CODE BEGIN WHILE */
  101.   while (1)
  102.   {
  103.   /* USER CODE END WHILE */

  104.   /* USER CODE BEGIN 3 */
  105.                
  106.                 ReadUserButton();
  107.                 if ( g_sys_mode == 0 ) {        //SetOff = 9 ;       
  108.                 }
  109.                 else if ( g_sys_mode == 1 ) {  //SetOff = 0;
  110.                 }
  111.                 else if ( g_sys_mode == 2 ) {
  112.     }

  113.                 if(i++>200)
  114.                 {                       
  115.                         i = 0;
  116.                         HAL_GPIO_TogglePin(LED_Y_GPIO_Port, LED_Y_Pin);
  117.                         HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin);
  118.                 }
  119.   }
  120.   /* USER CODE END 3 */

  121. }

  122. /** System Clock Configuration
  123. */
  124. void SystemClock_Config(void)
  125. {

  126.   RCC_OscInitTypeDef RCC_OscInitStruct;
  127.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  128.     /**Initializes the CPU, AHB and APB busses clocks
  129.     */
  130.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  131.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  132.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  133.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  134.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  136.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  137.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138.   {
  139.     Error_Handler();
  140.   }

  141.     /**Initializes the CPU, AHB and APB busses clocks
  142.     */
  143.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  146.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  148.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  149.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  150.   {
  151.     Error_Handler();
  152.   }

  153.     /**Configure the Systick interrupt time
  154.     */
  155.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  156.     /**Configure the Systick
  157.     */
  158.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  159.   /* SysTick_IRQn interrupt configuration */
  160.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  161. }

  162. /* USER CODE BEGIN 4 */

  163. /* USER CODE END 4 */

  164. /**
  165.   * @brief  Period elapsed callback in non blocking mode
  166.   * @note   This function is called  when TIM6 interrupt took place, inside
  167.   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  168.   * a global variable "uwTick" used as application time base.
  169.   * @param  htim : TIM handle
  170.   * @retval None
  171.   */
  172. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  173. {
  174. /* USER CODE BEGIN Callback 0 */
  175.         static int count = 0 ;
  176.         if (htim->Instance == htim3.Instance)
  177.         {
  178.                 count ++ ;
  179.                 if (count < 1000 )
  180.                     {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.05 ; SetOff = 3 ; Left_Motor_Control_K = 1;
  181.          Right_Motor_Control_K = 1;}
  182.                 else if ( count < 2000 )
  183.                             {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.08 ; SetOff = 9 ;Left_Motor_Control_K = 1;
  184.             Right_Motor_Control_K = 1;}
  185.                                          else if (count < 2500 )
  186.                                                  {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.05 ; SetOff = 3 ;Left_Motor_Control_K = 1;
  187.               Right_Motor_Control_K = 1;}
  188.                                                  else if (count < 3000 )
  189.                                                          {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.08 ; SetOff = 9 ;Left_Motor_Control_K = 0.5;
  190.                  Right_Motor_Control_K = 1.2;}
  191.                                                          else if (count < 3500 )
  192.                                                                  {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.05 ; SetOff = 3 ; Left_Motor_Control_K = 1;
  193.                   Right_Motor_Control_K = 1 ;}
  194.                                                                          else if (count < 4000 )
  195.                                                                                  {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.08 ; SetOff = 9 ;Left_Motor_Control_K = 1.2;
  196.                         Right_Motor_Control_K = 0.5;}
  197.                                                                                  else if ( count < 4500 )
  198.                                                                                          {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.05 ; SetOff = 3 ; Left_Motor_Control_K = 1;
  199.                          Right_Motor_Control_K = 1 ;}
  200.                                                                                          else if ( count < 5000 )
  201.                                                                                                  {Kp = 0.95 ; Ki = 0.04 ; Kd = 0.08 ; SetOff = 9 ;Left_Motor_Control_K = 0;
  202.                             Right_Motor_Control_K = 1;}
  203.                                                                                                  else Motor_Stop();                                                                                                                                                                                                    
  204.     Get_Accel_Gyro() ;
  205. //                Kalman_Filter( g_mpu9250.accel_y , g_mpu9250.gyro_x );
  206.     calculate_angle() ;
  207.                 PID(Kp,Kd,Ki,SetOff);
  208.                 Set_Motor_Pwm() ;
  209.                
  210. /*
  211.                 Angle_Calculate();
  212.                 Angle_Control();
  213.                 Set_Motor_Pwm();       
  214. */
  215.         }
  216. /* USER CODE END Callback 0 */
  217.   if (htim->Instance == TIM6) {
  218.     HAL_IncTick();
  219.   }
  220. /* USER CODE BEGIN Callback 1 */

  221. /* USER CODE END Callback 1 */
  222. }

  223. /**
  224.   * @brief  This function is executed in case of error occurrence.
  225.   * @param  None
  226.   * @retval None
  227.   */
  228. void Error_Handler(void)
  229. {
  230.   /* USER CODE BEGIN Error_Handler */
  231.   /* User can add his own implementation to report the HAL error return state */
  232.   while(1)
  233.   {
  234.   }
  235.   /* USER CODE END Error_Handler */
  236. }

  237. #ifdef USE_FULL_ASSERT

  238. /**
  239.    * @brief Reports the name of the source file and the source line number
  240.    * where the assert_param error has occurred.
  241.    * @param file: pointer to the source file name
  242.    * @param line: assert_param error line source number
  243.    * @retval None
  244.    */
  245. void assert_failed(uint8_t* file, uint32_t line)
  246. {
  247.   /* USER CODE BEGIN 6 */
  248.   /* User can add his own implementation to report the file name and line number,
  249.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  250.   /* USER CODE END 6 */

  251. }

  252. #endif

  253. /**
  254.   * @}
  255.   */

  256. /**
  257.   * @}
  258. */

  259. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
陈老板终极版平衡车代码.rar (3.49 MB, 下载次数: 46)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表