You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /**
  2. * @file dj.c
  3. * @author 1722451300@qq.com
  4. * @brief
  5. * @version 0.1
  6. * @date 2021-12-15
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. #include "motor.h"
  12. volatile int32_t recv_motor_data_state=0;//1就判断是不是接收完成,0不判断
  13. volatile int32_t motor_jerk_state = 0;
  14. extern volatile int32_t lcd_issue_motor_speed;
  15. volatile int16_t motor_off_state=0;
  16. extern int32_t recv_motor_data_time;
  17. extern int32_t inquire_error_time=0;
  18. void motor_uart_config(void)
  19. {
  20. /**
  21. * @brief UART0 CH340电机端
  22. *
  23. */
  24. uart0_config();
  25. rs485_re_pin = 0;
  26. rs485_re_port &= ~(0x01 << 3); //初始化为低电平默认接收模式
  27. }
  28. void send_cmd_to_motor(volatile char buff[20], unsigned int num)
  29. {
  30. /**
  31. * @brief
  32. * @param buff
  33. */
  34. rs485_re_port |= (0x01 << 3);
  35. while (num--) {
  36. while (!TRMT0)
  37. ;
  38. TX0B = *buff;
  39. buff++;
  40. }
  41. while (!TRMT0)
  42. ; //等待向电机发送数据完成
  43. rs485_re_port &= ~(0x01 << 3);
  44. }
  45. void inquire_motor_error_state(void)
  46. {
  47. /**
  48. * @brief 3
  49. * 3E 9C 01 00 DB
  50. */
  51. unsigned char inquire_motor_error_cmd[20] = {0};
  52. unsigned char str[2]={0};
  53. if(inquire_error_time>=2000)//2s查询一次
  54. {
  55. inquire_error_time=0;
  56. //读取错误信息
  57. inquire_motor_error_cmd[0] = 0x3E;
  58. inquire_motor_error_cmd[1] = 0x9A;
  59. inquire_motor_error_cmd[2] = 0X01;
  60. inquire_motor_error_cmd[3] = 0x00;
  61. inquire_motor_error_cmd[4] = 0xD9;
  62. //send_cmd_to_motor(inquire_motor_error_cmd, 5);
  63. sleep_ms(10);
  64. //读取速度的值
  65. inquire_motor_error_cmd[0] = 0x3E;
  66. inquire_motor_error_cmd[1] = 0x9C;
  67. inquire_motor_error_cmd[2] = 0X01;
  68. inquire_motor_error_cmd[3] = 0x00;
  69. inquire_motor_error_cmd[4] = 0xDB;
  70. //send_cmd_to_motor(inquire_motor_error_cmd, 5);
  71. sleep_ms(10);
  72. }
  73. }
  74. //----------------------------------------------------
  75. //----------------------------------------------------
  76. //----------------------------------------------------
  77. uint8_t g_motor_rxbuf_isready = 0;
  78. volatile char motor_rxbuff[50] = {0};
  79. volatile uint32_t motor_count = 0;
  80. uint32_t g_process_recv_motor_data_time = 0;
  81. void recv_from_motor_data(unsigned char data)
  82. {
  83. /**
  84. * @brief
  85. *
  86. */
  87. g_motor_rxbuf_isready=true;//进入中断就让他为真
  88. motor_rxbuff[motor_count++] = data;
  89. g_process_recv_motor_data_time= hal_get_irqticket();//记录最后的时间
  90. }
  91. uint32_t judge_recv_motor_data_finish(uint32_t time)
  92. {
  93. /**
  94. * @brief motor的数据完成
  95. */
  96. static uint32_t recv_motor_data_time = 0;
  97. recv_motor_data_time = time;
  98. if(recv_motor_data_time=0)
  99. {
  100. recv_motor_data_time = hal_get_ticket();
  101. return recv_motor_data_time;
  102. }
  103. else if (hal_has_passedms(recv_motor_data_time) > 10) {
  104. g_motor_rxbuf_isready=false;
  105. recv_motor_data_time = hal_get_ticket();
  106. send_cmd_to_motor(motor_rxbuff,motor_count);//接收完成将数据发给电脑
  107. motor_count=0;
  108. return 0;
  109. }
  110. }
  111. void motor_data_analyse(volatile char buff[20], unsigned int num)
  112. {
  113. /**
  114. * @brief
  115. * @param
  116. * @param
  117. * buff[8],buff[9]
  118. */
  119. if (buff[0] == 0x3E && buff[1] == 0x9A)//查询电机错误状态命令下发后,电机的回复
  120. {
  121. if((buff[11]>>0&0x01)||(buff[11]>>3&0x01))
  122. {
  123. lcd_switch_anomaly_page();
  124. }
  125. }
  126. if (buff[0] == 0x3E && buff[1] == 0x9C)//读取点击速度数值
  127. {
  128. }
  129. }
  130. volatile int32_t speed_dsp_now = 0;//记录现在的速度
  131. volatile int32_t first_set_motor_speed_dsp = 0;
  132. int32_t motor_jerk_status(void)
  133. {
  134. /**
  135. * @brief
  136. *
  137. */
  138. if(!((PA>>0)&1)&&motor_jerk_state==0)
  139. {
  140. sleep_ms(10);
  141. if(!((PA>>0)&1))
  142. {
  143. motor_jerk_state = 1;
  144. speed_dsp_now=0;
  145. first_set_motor_speed_dsp=0;
  146. lcd_switch_anomaly_page();//急停状态的时候显示异常界面
  147. clear_zero();
  148. sleep_ms(3000);
  149. lcd_switch_anomaly_page_key();
  150. }
  151. }
  152. else if((PA >> 0) & 1) //不是急停状态
  153. {
  154. if((PA >> 0) & 1)
  155. {
  156. motor_jerk_state = 0;
  157. beep_off;
  158. }
  159. }
  160. }
  161. int32_t judge_foreward_or_reversal(int32_t speed,int32_t turn_state) {
  162. /**
  163. * @brief
  164. *
  165. */
  166. if(turn_state==0) //正转
  167. {
  168. if(first_set_motor_speed_dsp<0)
  169. {
  170. speed =~speed+1;
  171. }
  172. return speed;
  173. }
  174. else if (turn_state == 1) //反转
  175. {
  176. if(first_set_motor_speed_dsp>0)
  177. {
  178. speed = ~(speed - 1);
  179. }
  180. return speed;
  181. }
  182. }
  183. void set_motor_off(void)
  184. {
  185. /**
  186. * @brief 0
  187. * 3E 80 01 00 BF
  188. */
  189. motor_off_state=1;
  190. unsigned char motor_off_cmd[20] = {0};
  191. motor_off_cmd[0] = 0x3E;
  192. motor_off_cmd[1] = 0x80;
  193. motor_off_cmd[2] = 0X01;
  194. motor_off_cmd[3] = 0x00;
  195. motor_off_cmd[4] = 0xBF;
  196. send_cmd_to_motor(motor_off_cmd, 5);
  197. }
  198. //===========================================================================
  199. //===========================================================================
  200. //===========================================================================
  201. //===========================================================================
  202. int32_t set_moter_speed_internal(int32_t speed_dsp)
  203. {
  204. /**
  205. * @brief
  206. */
  207. unsigned char motor_speed_cmd[20] = {0};
  208. speed_dsp_now = speed_dsp;
  209. first_set_motor_speed_dsp = speed_dsp;
  210. speed_dsp = speed_dsp * 100; //电机下发速度要乘以100
  211. motor_speed_cmd[0] = 0x3E;
  212. motor_speed_cmd[1] = 0xA2;
  213. motor_speed_cmd[2] = 0X01;
  214. motor_speed_cmd[3] = 0x04;
  215. motor_speed_cmd[4] = motor_speed_cmd[0] + motor_speed_cmd[1] + motor_speed_cmd[2] + motor_speed_cmd[3];
  216. motor_speed_cmd[5] = speed_dsp & 0xFF;
  217. motor_speed_cmd[6] = (speed_dsp >> 8) & 0xFF;
  218. motor_speed_cmd[7] = (speed_dsp >> 16) & 0xFF;
  219. motor_speed_cmd[8] = (speed_dsp >> 24) & 0xFF;
  220. motor_speed_cmd[9] = motor_speed_cmd[5] + motor_speed_cmd[6] + motor_speed_cmd[7] + motor_speed_cmd[8];
  221. send_cmd_to_motor(motor_speed_cmd, 10);
  222. if(speed_dsp_now==0)
  223. {
  224. sleep_ms(10);
  225. set_motor_off();
  226. }
  227. }
  228. /*
  229. void set_motor_speed(int32_t speed_dsp_target)
  230. {
  231. /**
  232. * @brief
  233. * @param
  234. */
  235. /*
  236. while(1)
  237. {
  238. if(speed_dsp_target==speed_dsp_now)//目标速度与现在的速度相等
  239. {
  240. break;
  241. }
  242. else if(speed_dsp_target < speed_dsp_now)//说明用户向让速度减小
  243. {
  244. int32_t setspeedthistime = speed_dsp_now - 10;
  245. if (setspeedthistime < speed_dsp_target) {
  246. setspeedthistime = speed_dsp_target;
  247. }
  248. set_moter_speed_internal(setspeedthistime);
  249. }
  250. else if (speed_dsp_target > speed_dsp_now) //说明用户想让速度进行增加 要设置的speed_dsp_target是150,现在的speed_dsp_now 0
  251. {
  252. int32_t nowexpectspeed = speed_dsp_now + 10;
  253. if (nowexpectspeed > speed_dsp_target) {
  254. nowexpectspeed = speed_dsp_target;
  255. }
  256. set_moter_speed_internal(nowexpectspeed);// 去设置的是10
  257. }
  258. sleep_ms(10);
  259. }
  260. }
  261. */