soft_test.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "soft_test.h"
  2. #include "main.h"
  3. #include "tim.h"
  4. #include "soft_can.h"
  5. #include "soft_uart.h"
  6. #include "can.h"
  7. #include "usart.h"
  8. #include "soft_terrain.h"
  9. #include "soft_obstacle.h"
  10. #include "soft_p_2_c.h"
  11. #include "soft_flash.h"
  12. #include "soft_obstacle.h"
  13. #include "string.h"
  14. #include "can_debug.h"
  15. #include "soft_version.h"
  16. #include "config.h"
  17. #include "dronecan.h"
  18. /******************************************************************
  19. * ****************测试程序*****************************************
  20. * ****************************************************************/
  21. uint8_t UART3_send_buf[8] = {0, 1, 2, 3, 4, 5, 6, 7}; //DMA发送 不能用局部变量 可以使用static局部变量
  22. void uart_send_msg(void)
  23. {
  24. static uint32_t uart_time = 0;
  25. if (HAL_GetTick() - uart_time < 100)
  26. {
  27. return;
  28. }
  29. else
  30. {
  31. uart_time = HAL_GetTick();
  32. }
  33. uint32_t cur_time = HAL_GetTick();
  34. memcpy(&UART3_send_buf[0],&cur_time,4);
  35. //HAL_UART_Transmit(&huart3,&UART3_send_buf[i],1,0xFFFF);
  36. //HAL_UART_Transmit_DMA(&huart2, UART2_send_buf, 8); //DMA发送数据
  37. //HAL_UART_Transmit(&huart2,&UART2_send_buf[i],1,0xFFFF);
  38. /*
  39. while (huart3.gState == HAL_UART_STATE_READY)
  40. {
  41. assd++;
  42. }
  43. */
  44. HAL_UART_Transmit_DMA(&huart3, UART3_send_buf, 4);
  45. }
  46. void ttest_func(void)
  47. {
  48. /*
  49. if (HAL_GetTick() - led_time > 1000 && HAL_GetTick() > 10000)
  50. {
  51. HAL_GPIO_TogglePin(GPIOA,LED2_Pin);
  52. led_time = HAL_GetTick();
  53. }
  54. if ((HAL_GetTick() > 5000) && (a == 1))
  55. {
  56. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1300);
  57. __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 1700);
  58. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 1700);
  59. __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 1700);
  60. a = 0;
  61. }
  62. */
  63. //5HZ 发送CAN
  64. //uart_send_msg();
  65. //HAL_GPIO_WritePin(GPIOA, LED1_Pin, SET); //蓝色
  66. //HAL_GPIO_WritePin(GPIOA, LED2_Pin, SET); //红色
  67. //HAL_GPIO_WritePin(GPIOA, LED3_Pin, SET); //绿色
  68. }
  69. uint32_t led_time_green = 0;
  70. uint32_t led_time_purple = 0;
  71. uint32_t led_time_blue = 0;
  72. uint32_t led_time_yellow = 0;
  73. bool led_green = false;
  74. bool led_purple = false;
  75. bool led_blue = false;
  76. bool led_yellow = false;
  77. bool only_star_one_green = false;
  78. bool only_star_one_purple = false;
  79. bool only_star_one_blue = false;
  80. bool only_star_one_yellow = false;
  81. bool no_led_star = false;
  82. /**
  83. * @file pmu_inside_led
  84. * @brief 板载LED灯
  85. * @param None
  86. * @details 绿:正常 --紫色:仿地 --蓝:前避 -- 黄:后避
  87. * @author Zhang Sir
  88. **/
  89. void pmu_inside_led()
  90. {
  91. if (led_green == false && led_purple == false && led_blue == false && led_green == false && led_yellow == false)
  92. {
  93. no_led_star = true;
  94. }
  95. else
  96. {
  97. no_led_star = false;
  98. }
  99. //正常闪绿灯 设备连接后依次闪其他颜色
  100. if ((HAL_GetTick() - led_time_green > 2500) && (no_led_star == true) &&
  101. (only_star_one_green == false))
  102. {
  103. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  104. led_time_green = HAL_GetTick();
  105. led_green = true;
  106. only_star_one_green = true;
  107. only_star_one_purple = true;
  108. only_star_one_blue = true;
  109. only_star_one_yellow = true;
  110. }
  111. else if (terrain_is_link == true && no_led_star == true && only_star_one_purple == true)
  112. {
  113. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_RESET);
  114. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  115. led_time_purple = HAL_GetTick();
  116. led_purple = true;
  117. }
  118. else if (obs_f_is_link == true && no_led_star == true && only_star_one_blue == true)
  119. {
  120. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_RESET);
  121. led_time_blue = HAL_GetTick();
  122. led_blue = true;
  123. }
  124. else if (obs_b_is_link == true && no_led_star == true && only_star_one_yellow == true)
  125. {
  126. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  127. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_RESET);
  128. led_time_yellow = HAL_GetTick();
  129. led_yellow = true;
  130. }
  131. //灭灯
  132. if ((HAL_GetTick() - led_time_green >= 500) && led_green == true)
  133. {
  134. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_SET);
  135. led_green = false;
  136. only_star_one_green = false;
  137. }
  138. if ((HAL_GetTick() - led_time_purple >= 500) && led_purple == true)
  139. {
  140. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_SET);
  141. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);
  142. led_purple = false;
  143. only_star_one_purple = false;
  144. }
  145. if ((HAL_GetTick() - led_time_blue >= 500) && led_blue == true)
  146. {
  147. HAL_GPIO_WritePin(GPIOA, LED1_Pin, GPIO_PIN_SET);
  148. led_blue = false;
  149. only_star_one_blue = false;
  150. }
  151. if ((HAL_GetTick() - led_time_yellow >= 500) && led_yellow == true)
  152. {
  153. HAL_GPIO_WritePin(GPIOA, LED2_Pin, GPIO_PIN_SET);
  154. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_SET);
  155. led_yellow = false;
  156. only_star_one_yellow = false;
  157. }
  158. //ttest_func();
  159. }
  160. /**
  161. * @file user_init
  162. * @brief 初始化参数
  163. * @param None
  164. * @details 串口 can口 pwm rkfifo flash读取
  165. * @author Zhang Sir
  166. **/
  167. uint8_t recv_vk_protocol[MAX_UART_BUF * 4] = {0};
  168. uint8_t candebug_protocol[MAX_UART_BUF * 2] = {0};
  169. uint8_t send_uart_buffer[ MAX_UART_BUF * 8 ] = {0};
  170. void user_init(void)
  171. {
  172. //can pwm uart 初始化
  173. can_start_init();
  174. UART_Init();
  175. pwm_init();
  176. //PVD_Config();
  177. //dronecan_init(); //test
  178. //上电读内存参数
  179. flash_read_funcktion();
  180. //上电亮绿灯
  181. HAL_GPIO_WritePin(GPIOA, LED3_Pin, GPIO_PIN_RESET);
  182. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); //使能PWM
  183. //初始化缓存区
  184. rkfifo_init(&recv_rkfifo, recv_vk_protocol, sizeof(recv_vk_protocol), 1);
  185. rkfifo_init(&candebug_rkfifo, candebug_protocol, sizeof(candebug_protocol), 1);
  186. rkfifo_init(&send_uart_rkfifo, send_uart_buffer, sizeof(send_uart_buffer), 1);
  187. //注册PMU
  188. Int2String(current_pmu_par.SN,dev_pmu.sn,9);
  189. Int2String(APP_VERSION,dev_pmu.soft_serial,6);
  190. regist_dev_info(&dev_pmu,DEVICE_PMU,false,dev_pmu.sn,9,dev_pmu.soft_serial,6,NULL,0,"vk",3);
  191. }
  192. uint8_t isLeapYear(uint16_t year)
  193. {
  194. uint8_t res=0;
  195. if(year%4 == 0) // 能够被4整除
  196. {
  197. if(year%100 != 0 || year%400 == 0) //能被4整除不能被100整除或者能够被400整除
  198. {
  199. res = 1;
  200. }
  201. else
  202. {
  203. res =0;
  204. }
  205. }
  206. return res;
  207. }
  208. // 将Unix时间戳转换为北京时间
  209. // unixTime: 需要判断的Unix时间戳
  210. // *tempBeijing:返回的北京时间
  211. // return:none
  212. // note:没对输入参数正确性做判断
  213. rtc_time_t beijing_time;
  214. void covUnixTimeStp2Beijing( uint32_t unixTime, rtc_time_t *tempBeijing )
  215. {
  216. uint32_t totleDayNum = 0, totleSecNum = 0;
  217. uint16_t remainDayofYear = 0, tempYear = 0;
  218. uint8_t *pr = NULL;
  219. static uint8_t month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
  220. static uint8_t Leap_month_day[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年
  221. totleDayNum = unixTime / ( 24 * 60 * 60 ); //总天数(注意加括号)
  222. totleSecNum = unixTime % ( 24 * 60 * 60 ); //当天剩余的秒速
  223. memset( tempBeijing, 0x00, sizeof( rtc_time_t ) );
  224. // 1.先计算时间 HH:MM:SS
  225. tempBeijing->ui8Hour = totleSecNum / 3600;
  226. tempBeijing->ui8Minute = ( totleSecNum % 3600 ) / 60; //error:变量搞错
  227. tempBeijing->ui8Second = ( totleSecNum % 3600 ) % 60;
  228. // 2.对时间进行时区调整(注意:这里可能造成日期 +1)
  229. tempBeijing->ui8Hour += TIMEZONE;
  230. if ( tempBeijing->ui8Hour > 23 )
  231. {
  232. //printf("modify day..\n");
  233. tempBeijing->ui8Hour -= 24;
  234. remainDayofYear++; // 日期+1
  235. }
  236. // 3.计算哪一年
  237. tempBeijing->ui8Year = 1970 + ( totleDayNum / FOURYEARDAY ) * 4; // 4年为一个周期
  238. remainDayofYear += totleDayNum % FOURYEARDAY;
  239. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  240. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  241. while ( remainDayofYear >= tempYear ) // 计算4年整数倍外的年。
  242. {
  243. tempBeijing->ui8Year++;
  244. remainDayofYear -= tempYear;
  245. tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
  246. }
  247. // 4.计算哪一月的哪一天
  248. pr = isLeapYear( tempBeijing->ui8Year ) ? Leap_month_day : month_day;
  249. remainDayofYear++; // 这里开始计算具体日期。remainDayofYear为 0 时其实是 1 号,所以这里要 +1
  250. while ( remainDayofYear > *( pr + tempBeijing->ui8Month ) )
  251. {
  252. remainDayofYear -= *( pr + tempBeijing->ui8Month );
  253. tempBeijing->ui8Month++;
  254. }
  255. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
  256. tempBeijing->ui8Month++; //month
  257. tempBeijing->ui8DayOfMonth = remainDayofYear; //day
  258. //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, tempBeijing->ui8DayOfMonth);
  259. }
  260. void PVD_IRQHandler(void)
  261. {
  262. HAL_PWR_PVD_IRQHandler();
  263. }
  264. void PVD_Config(void)
  265. {
  266. /*##-1- Enable Power Clock #################################################*/
  267. __HAL_RCC_PWR_CLK_ENABLE();
  268. /*##-2- Configure the NVIC for PVD #########################################*/
  269. HAL_NVIC_SetPriority(PVD_IRQn, 0, 0);
  270. HAL_NVIC_EnableIRQ(PVD_IRQn);
  271. PWR_PVDTypeDef sConfigPVD;
  272. sConfigPVD.PVDLevel = PWR_PVDLEVEL_3;
  273. sConfigPVD.Mode = PWR_PVD_MODE_IT_RISING;
  274. HAL_PWR_ConfigPVD(&sConfigPVD);
  275. /* Enable the PVD Output */
  276. HAL_PWR_EnablePVD();
  277. }