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

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