训练营PLSR题目
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

324 wiersze
7.4 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. CalculatePSCARR(Frequency, 72000000, 65535, &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. if(PulseOutput[NowPulse].PulseCount >= 0)
  74. {
  75. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  76. }
  77. else
  78. {
  79. TIM2->ARR = - PulseOutput[NowPulse].PulseCount;
  80. }
  81. switch(Options.SentPost)
  82. {
  83. case 0: HAL_TIM_PWM_Start_IT(&htim10, TIM_CHANNEL_1); break;
  84. case 1: HAL_TIM_PWM_Start_IT(&htim11, TIM_CHANNEL_1); break;
  85. case 2: HAL_TIM_PWM_Start_IT(&htim13, TIM_CHANNEL_1); break;
  86. case 3: HAL_TIM_PWM_Start_IT(&htim14, TIM_CHANNEL_1); break;
  87. }
  88. }
  89. /**
  90. * @brief 获取加速度
  91. * @param[in] Start 起始脉冲频率
  92. * @param[in] End 目标脉冲频率
  93. * @param[in] AccCount 需要多少个脉冲完成加速
  94. * @return 加速度
  95. */
  96. float GetAcc(int32_t Start, int32_t End, int32_t AccCount)
  97. {
  98. return ((float)End - (float)Start) / (float)AccCount;
  99. }
  100. /**
  101. * @brief 添加新的脉冲(只允许按顺序添加)
  102. * @param[in] Pulse 第几段脉冲
  103. * @param[in] Ferquency 脉冲的频率
  104. * @param[in] Count 脉冲的数量
  105. * @param[in] NextPulse 下一段脉冲
  106. * @return 无
  107. */
  108. void AddPulse(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
  109. {
  110. if(Pulse == Options.AllPulse)
  111. {
  112. if(Count > 0)
  113. {
  114. SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
  115. }
  116. else
  117. {
  118. SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
  119. }
  120. Options.AllPulse ++;
  121. }
  122. }
  123. /**
  124. * @brief 脉冲初始化
  125. * @param[in] Pulse 第几段脉冲
  126. * @param[in] Ferquency 脉冲的频率
  127. * @param[in] Count 脉冲的数量
  128. * @param[in] Direction 脉冲的方向
  129. * @param[in] NextPulse 下一段脉冲
  130. * @return 无
  131. */
  132. void PulseInit(uint8_t Pulse, uint32_t Ferquency, int32_t Count, int8_t NextPulse)
  133. {
  134. if(Count > 0)
  135. {
  136. SetPulse(Pulse, Ferquency, Count, 0, NextPulse);
  137. }
  138. else
  139. {
  140. SetPulse(Pulse, Ferquency, -Count, 1, NextPulse);
  141. }
  142. Options.AllPulse = 1;
  143. }
  144. /**
  145. * @brief 设置结果脉冲数
  146. * @param[in] PulseCount 要设定的脉冲数
  147. * @return 无
  148. */
  149. void MAXPulseSet(int32_t PulseCount)
  150. {
  151. if(NowPulse == 0)
  152. {
  153. NowMAXPulse = PulseOutput[NowPulse].PulseCount;
  154. }
  155. else
  156. {
  157. NowMAXPulse += PulseOutput[NowPulse].PulseCount;
  158. }
  159. }
  160. /**
  161. * @brief 设置下一次进入中断的脉冲数
  162. * @return 无
  163. */
  164. void SetNextPulse(void)
  165. {
  166. MAXPulseSet(PulseOutput[NowPulse].PulseCount); /* 更改结果脉冲数 */
  167. if(Options.RunMod == 0) /* 如果是相对模式 */
  168. {
  169. if(PulseOutput[NowPulse].PulseCount > 0)
  170. {
  171. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  172. }
  173. else
  174. {
  175. TIM2->ARR = - PulseOutput[NowPulse].PulseCount;
  176. }
  177. }
  178. else /* 如果是绝对模式 */
  179. {
  180. if(NowMAXPulse >= MAX_Pulse) /* 如果超出了上限 */
  181. {
  182. TIM2->ARR = - (MAX_Pulse - NowMAXPulse);
  183. }
  184. else
  185. {
  186. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  187. }
  188. }
  189. }
  190. /**
  191. * @brief 获取与CNT相加的基础值
  192. * @param[in] NowPulseNUM 当前的脉冲段数
  193. * @return 基础值
  194. */
  195. uint32_t GetBase(uint8_t NowPulseNUM)
  196. {
  197. uint32_t temp = 0;
  198. if(NowPulseNUM == 0)
  199. {
  200. temp = 0;
  201. }
  202. else
  203. {
  204. for(int i = 0; i < NowPulseNUM; i++)
  205. {
  206. temp += PulseOutput[i].PulseCount;
  207. }
  208. }
  209. return temp;
  210. }
  211. /**
  212. * @brief 从寄存器读取设置参数
  213. * @return 无
  214. */
  215. void PLSROptionLoad(void)
  216. {
  217. /* 数据转换 */
  218. uint32_t InitSpeed;
  219. uint16_t tempH, tempL;
  220. Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
  221. Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
  222. Set16_32(&InitSpeed, &tempL, &tempH);
  223. /* 数据读取 */
  224. Options.SentPost = Register_L[0x1000];
  225. Options.DirPost = Register_L[0x1001];
  226. Options.EXT = Register_L[0x1002];
  227. Options.DirDelay = Register_L[0x1003]; //可能更大。
  228. Options.Dir = Register_L[0x1004];
  229. Options.AccMod = Register_L[0x1005];
  230. Options.RunMod = Register_L[0x1006];
  231. Options.AllPulse = Register_L[0x1007];
  232. Options.StartPulse = Register_L[0x1008] - 1;
  233. Options.InitSpeed = InitSpeed;
  234. Options.AccUpTime = (Register_H[0x100B]<< 8) | Register_L[0x100B];
  235. Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];
  236. }
  237. void PLSRPluseLoad(void)
  238. {
  239. uint32_t temp_Frequency, temp_PulseCount;
  240. uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
  241. for (int i = 0; i < 10; i++)
  242. {
  243. Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
  244. , &Register_L[0x1100 + 0x10 * i]);
  245. Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
  246. , &Register_L[0x1101 + 0x10 * i]);
  247. Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
  248. Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
  249. , &Register_L[0x1102 + 0x10 * i]);
  250. Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
  251. , &Register_L[0x1103 + 0x10 * i]);
  252. Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
  253. PulseOutput[i].Frequency = temp_Frequency;
  254. PulseOutput[i].PulseCount = temp_PulseCount;
  255. PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
  256. PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
  257. }
  258. }
  259. /**
  260. * @brief 保存总的脉冲数至指定的寄存器
  261. * @return 无
  262. */
  263. void CountSave(void)
  264. {
  265. int32_t temp_Count = 0;
  266. uint16_t DatH, DatL;
  267. memcpy(&temp_Count, &AllPulseCNT, 4);
  268. DatH = (temp_Count >> 16);
  269. DatL = (temp_Count & 0x0000ffff);
  270. Register_H[0x2001] = (DatH >> 8);
  271. Register_L[0x2001] = (DatH & 0x00ff);
  272. Register_H[0x2000] = (DatL >> 8);
  273. Register_L[0x2000] = (DatL & 0x00ff);
  274. }
  275. /**
  276. * @brief 获取加减速的脉冲数
  277. * @return 无
  278. */
  279. void GetAddCount(void)
  280. {
  281. if(NowPulse == Options.StartPulse)
  282. {
  283. AccUpCount = Options.AccUpTime * PulseOutput[NowPulse].Frequency / 1000;
  284. }
  285. else
  286. {
  287. AccUpCount = Options.AccUpTime * (PulseOutput[NowPulse].Frequency - PulseOutput[PrePulse].Frequency) / 1000;
  288. AccDownCount = Options.AccDownTime * (PulseOutput[PrePulse].Frequency - PulseOutput[NowPulse].Frequency) / 1000;
  289. }
  290. }