训练营PLSR题目
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.
 
 
 
 
 
 

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