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.

271 lines
7.3 KiB

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