训练营PLSR题目
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

309 satır
7.1 KiB

  1. #include "PLSR.h"
  2. #include "PSCARR.h"
  3. #include "tim.h"
  4. #include "sram.h"
  5. #include <string.h>
  6. #include "modbus.h"
  7. #include "bitset.h"
  8. #include <stdio.h>
  9. Pulse PulseOutput[10]; // 十段脉冲的配置
  10. Option Options;
  11. uint8_t NowPulse; /* 当前处于第几段脉冲 */
  12. uint8_t PrePulse; /* 之前处于第几段脉冲 */
  13. uint8_t PulseNum; /* 一共有多少段脉冲 */
  14. int32_t MAX_Pulse; /* 最多的脉冲数 */
  15. int32_t AccUpCount; /* 加速脉冲数 */
  16. int32_t AccDownCount; /* 减速脉冲数 */
  17. int32_t NowMAXPulse; /* 当前要达到的总脉冲 */
  18. int32_t PulseCount = 0;
  19. /**
  20. * @brief 根据脉冲的频率设置对应的PSC和ARR
  21. * @param[in] SentPost 脉冲通道
  22. * @param[in] Ferquency 脉冲的频率
  23. * @return 无
  24. */
  25. void SetFrequency(uint8_t SentPost, uint32_t Frequency)
  26. {
  27. Frequency_Change_PSC_And_ARR(Frequency, &psc, &arr);
  28. printf("Fre=%d,",Frequency);
  29. switch(SentPost)
  30. {
  31. case 0: TIM10->ARR = arr, TIM10->PSC = psc, TIM10->CCR1 = arr / 2; break;
  32. case 1: TIM11->ARR = arr, TIM11->PSC = psc, TIM11->CCR1 = arr / 2; break;
  33. case 2: TIM13->ARR = arr, TIM13->PSC = psc, TIM13->CCR1 = arr / 2; break;
  34. case 3: TIM14->ARR = arr, TIM14->PSC = psc, TIM14->CCR1 = arr / 2; break;
  35. }
  36. }
  37. /**
  38. * @brief 设置脉冲基本参数
  39. * @param[in] Pulse 第几段脉冲
  40. * @param[in] Ferquency 脉冲的频率
  41. * @param[in] Count 脉冲的数量
  42. * @param[in] EXT EXT信号
  43. * @param[in] NextPulse 下一段脉冲
  44. * @return 无
  45. */
  46. void SetPulse(uint8_t Pulse, uint32_t Ferquency, int32_t Count, uint8_t EXT, int8_t NextPulse)
  47. {
  48. PulseOutput[Pulse].Frequency = Ferquency;
  49. PulseOutput[Pulse].PulseCount = Count;
  50. PulseOutput[Pulse].EXT = EXT;
  51. PulseOutput[Pulse].NextPulse = NextPulse;
  52. }
  53. /**
  54. * @brief 脉冲初始化
  55. * @param[in] StartPulse 起始脉冲段
  56. * @param[in] PulseMod 绝对1/相对0
  57. * @return 无
  58. */
  59. void PulseBaseInit(uint8_t StartPulse, uint8_t PulseMod, int32_t MAX_Pulse)
  60. {
  61. Options.StartPulse = StartPulse;
  62. Options.RunMod = PulseMod;
  63. NowPulse = StartPulse;
  64. MAX_Pulse = MAX_Pulse;
  65. }
  66. /**
  67. * @brief 脉冲开始
  68. * @param[in] StartPulse 起始脉冲段
  69. * @return 无
  70. */
  71. void PulseStart(void)
  72. {
  73. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  74. switch(Options.SentPost)
  75. {
  76. case 0: HAL_TIM_PWM_Start_IT(&htim10, TIM_CHANNEL_1); break;
  77. case 1: HAL_TIM_PWM_Start_IT(&htim11, TIM_CHANNEL_1); break;
  78. case 2: HAL_TIM_PWM_Start_IT(&htim13, TIM_CHANNEL_1); break;
  79. case 3: HAL_TIM_PWM_Start_IT(&htim14, TIM_CHANNEL_1); break;
  80. }
  81. }
  82. /**
  83. * @brief 获取加速度
  84. * @param[in] Start 起始脉冲频率
  85. * @param[in] End 目标脉冲频率
  86. * @param[in] AccCount 需要多少个脉冲完成加速
  87. * @return 加速度
  88. */
  89. float GetAcc(int32_t Start, int32_t End, int32_t AccCount)
  90. {
  91. return ((float)End - (float)Start) / (float)AccCount;
  92. }
  93. /**
  94. * @brief 添加新的脉冲(只允许按顺序添加)
  95. * @param[in] Pulse 第几段脉冲
  96. * @param[in] Ferquency 脉冲的频率
  97. * @param[in] Count 脉冲的数量
  98. * @param[in] NextPulse 下一段脉冲
  99. * @return 无
  100. */
  101. void AddPulse(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
  102. {
  103. if(Pulse == Options.AllPulse)
  104. {
  105. if(Count > 0)
  106. {
  107. SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
  108. }
  109. else
  110. {
  111. SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
  112. }
  113. Options.AllPulse ++;
  114. }
  115. }
  116. /**
  117. * @brief 脉冲初始化
  118. * @param[in] Pulse 第几段脉冲
  119. * @param[in] Ferquency 脉冲的频率
  120. * @param[in] Count 脉冲的数量
  121. * @param[in] Direction 脉冲的方向
  122. * @param[in] NextPulse 下一段脉冲
  123. * @return 无
  124. */
  125. void PulseInit(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
  126. {
  127. if(Count > 0)
  128. {
  129. SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
  130. }
  131. else
  132. {
  133. SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
  134. }
  135. Options.AllPulse = 1;
  136. }
  137. /**
  138. * @brief 设置结果脉冲数
  139. * @param[in] PulseCount 要设定的脉冲数
  140. * @return 无
  141. */
  142. void MAXPulseSet(int32_t PulseCount)
  143. {
  144. if(NowPulse == 0)
  145. {
  146. NowMAXPulse = PulseOutput[NowPulse].PulseCount;
  147. }
  148. else
  149. {
  150. NowMAXPulse += PulseOutput[NowPulse].PulseCount;
  151. }
  152. }
  153. /**
  154. * @brief 设置下一次进入中断的脉冲数
  155. * @return 无
  156. */
  157. void SetNextPulse(void)
  158. {
  159. MAXPulseSet(PulseOutput[NowPulse].PulseCount); /* 更改结果脉冲数 */
  160. if(Options.RunMod == 0) /* 如果是相对模式 */
  161. {
  162. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  163. }
  164. else /* 如果是绝对模式 */
  165. {
  166. if(NowMAXPulse >= MAX_Pulse) /* 如果超出了上限 */
  167. {
  168. TIM2->ARR = - (MAX_Pulse - NowMAXPulse);
  169. }
  170. else
  171. {
  172. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  173. }
  174. }
  175. }
  176. /**
  177. * @brief 获取与CNT相加的基础值
  178. * @param[in] NowPulseNUM 当前的脉冲段数
  179. * @return 基础值
  180. */
  181. uint32_t GetBase(uint8_t NowPulseNUM)
  182. {
  183. uint32_t temp = 0;
  184. if(NowPulseNUM == 0)
  185. {
  186. temp = 0;
  187. }
  188. else
  189. {
  190. for(int i = 0; i < NowPulseNUM; i++)
  191. {
  192. temp += PulseOutput[i].PulseCount;
  193. }
  194. }
  195. return temp;
  196. }
  197. /**
  198. * @brief 从寄存器读取设置参数
  199. * @return 无
  200. */
  201. void PLSROptionLoad(void)
  202. {
  203. /* 数据转换 */
  204. uint32_t InitSpeed;
  205. uint16_t tempH, tempL;
  206. Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
  207. Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
  208. Set16_32(&InitSpeed, &tempL, &tempH);
  209. /* 数据读取 */
  210. Options.SentPost = Register_L[0x1000];
  211. Options.DirPost = Register_L[0x1001];
  212. Options.EXT = Register_L[0x1002];
  213. Options.DirDelay = Register_L[0x1003]; //可能更大。
  214. Options.Dir = Register_L[0x1004];
  215. Options.AccMod = Register_L[0x1005];
  216. Options.RunMod = Register_L[0x1006];
  217. Options.AllPulse = Register_L[0x1007];
  218. Options.StartPulse = Register_L[0x1008] - 1;
  219. Options.InitSpeed = InitSpeed;
  220. Options.AccUpTime = (Register_H[0x100B]<< 8) | Register_L[0x100B];
  221. Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];
  222. }
  223. void PLSRPluseLoad(void)
  224. {
  225. uint32_t temp_Frequency, temp_PulseCount;
  226. uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
  227. for (int i = 0; i < 10; i++)
  228. {
  229. Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
  230. , &Register_L[0x1100 + 0x10 * i]);
  231. Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
  232. , &Register_L[0x1101 + 0x10 * i]);
  233. Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
  234. Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
  235. , &Register_L[0x1102 + 0x10 * i]);
  236. Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
  237. , &Register_L[0x1103 + 0x10 * i]);
  238. Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
  239. PulseOutput[i].Frequency = temp_Frequency;
  240. PulseOutput[i].PulseCount = temp_PulseCount;
  241. PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
  242. PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
  243. }
  244. }
  245. /**
  246. * @brief 保存总的脉冲数至指定的寄存器
  247. * @return 无
  248. */
  249. void CountSave(void)
  250. {
  251. int32_t temp_Count = 0;
  252. uint16_t DatH, DatL;
  253. memcpy(&temp_Count, &AllPulseCNT, 4);
  254. DatH = temp_Count / 0xffff;
  255. DatL = temp_Count % 0xffff;
  256. Register_H[0x2001] = (DatH >> 8);
  257. Register_L[0x2001] = (DatH & 0x00ff);
  258. Register_H[0x2000] = (DatL >> 8);
  259. Register_L[0x2000] = (DatL & 0x00ff);
  260. }
  261. /**
  262. * @brief 获取加减速的脉冲数
  263. * @return 无
  264. */
  265. void GetAddCount(void)
  266. {
  267. if(NowPulse == Options.StartPulse)
  268. {
  269. AccUpCount = Options.AccUpTime * PulseOutput[NowPulse].Frequency / 1000;
  270. }
  271. else
  272. {
  273. AccUpCount = Options.AccUpTime * (PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / 1000;
  274. AccDownCount = Options.AccDownTime * (PulseOutput[PrePulse].Frequency - PulseOutput[NowPulse].Frequency) / 1000;
  275. }
  276. }