训练营PLSR题目
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

232 lines
5.3 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. uint32_t NowFrequency;
  19. int32_t PulseCount = 0;
  20. /**
  21. * @brief 根据脉冲的频率设置对应的PSC和ARR
  22. * @param[in] SentPost 脉冲通道
  23. * @param[in] Ferquency 脉冲的频率
  24. * @return 无
  25. */
  26. void SetFrequency(uint8_t SentPost, uint32_t Frequency)
  27. {
  28. CalculatePSCARR(Frequency, 72000000, 65535, &psc, &arr);
  29. //printf("Fre=%d,",Frequency);
  30. switch (SentPost)
  31. {
  32. case 0: TIM10->ARR = arr, TIM10->PSC = psc, TIM10->CCR1 = arr / 2; break;
  33. case 1: TIM11->ARR = arr, TIM11->PSC = psc, TIM11->CCR1 = arr / 2; break;
  34. case 2: TIM13->ARR = arr, TIM13->PSC = psc, TIM13->CCR1 = arr / 2; break;
  35. case 3: TIM14->ARR = arr, TIM14->PSC = psc, TIM14->CCR1 = arr / 2; break;
  36. }
  37. }
  38. /**
  39. * @brief 脉冲开始
  40. * @param[in] StartPulse 起始脉冲段
  41. * @return 无
  42. */
  43. void PulseStart(void)
  44. {
  45. SetNextPulse();
  46. switch(Options.SentPost)
  47. {
  48. case 0: HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1); break;
  49. case 1: HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); break;
  50. case 2: HAL_TIM_PWM_Start(&htim13, TIM_CHANNEL_1); break;
  51. case 3: HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1); break;
  52. }
  53. }
  54. /**
  55. * @brief 获取加速度
  56. * @param[in] Start 起始脉冲频率
  57. * @param[in] End 目标脉冲频率
  58. * @param[in] AccCount 需要多少个脉冲完成加速
  59. * @return 加速度
  60. */
  61. float GetAcc(int32_t Start, int32_t End, int32_t AccCount)
  62. {
  63. return ((float)End - (float)Start) / (float)AccCount;
  64. }
  65. /**
  66. * @brief 设置结果脉冲数
  67. * @param[in] PulseCount 要设定的脉冲数
  68. * @return 无
  69. */
  70. void MAXPulseSet(int32_t PulseCount)
  71. {
  72. if (NowPulse == 0)
  73. {
  74. NowMAXPulse = PulseOutput[NowPulse].PulseCount;
  75. }
  76. else
  77. {
  78. NowMAXPulse += PulseOutput[NowPulse].PulseCount;
  79. }
  80. }
  81. /**
  82. * @brief 设置下一次进入中断的脉冲数
  83. * @return 无
  84. */
  85. void SetNextPulse(void)
  86. {
  87. int32_t temp;
  88. if (Options.RunMod == 0) /* 如果是相对模式 */
  89. {
  90. if (PulseOutput[NowPulse].PulseCount > 0)
  91. {
  92. if (Options.AllPulse - PulseNum == 1)
  93. {
  94. TIM2->ARR = PulseOutput[NowPulse].PulseCount - N_Acc;
  95. }
  96. else
  97. {
  98. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  99. }
  100. }
  101. else
  102. {
  103. if (Options.AllPulse - PulseNum == 1)
  104. {
  105. TIM2->ARR = - PulseOutput[NowPulse].PulseCount - N_Acc;
  106. }
  107. else
  108. {
  109. TIM2->ARR = - PulseOutput[NowPulse].PulseCount;
  110. }
  111. }
  112. }
  113. else if (Options.RunMod == 1)/* 如果是绝对模式 */
  114. {
  115. if (PulseOutput[NowPulse].PulseCount * PulseOutput[PrePulse].PulseCount >= 0)
  116. {
  117. temp = PulseOutput[NowPulse].PulseCount - PulseOutput[PrePulse].PulseCount;
  118. }
  119. else
  120. {
  121. temp = PulseOutput[NowPulse].PulseCount + PulseOutput[PrePulse].PulseCount;
  122. }
  123. if(temp >= 0)
  124. {
  125. TIM2->ARR = temp;
  126. }
  127. else
  128. {
  129. TIM2->ARR = - temp;
  130. }
  131. if (Options.StartPulse == NowPulse)
  132. {
  133. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  134. }
  135. }
  136. }
  137. /**
  138. * @brief 从寄存器读取设置参数
  139. * @return 无
  140. */
  141. void PLSROptionLoad(void)
  142. {
  143. /* 数据转换 */
  144. uint32_t InitSpeed;
  145. uint16_t tempH, tempL;
  146. Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
  147. Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
  148. Set16_32(&InitSpeed, &tempL, &tempH);
  149. /* 数据读取 */
  150. Options.SentPost = Register_L[0x1000];
  151. Options.DirPost = Register_L[0x1001];
  152. Options.EXT = Register_L[0x1002];
  153. Options.DirDelay = Register_L[0x1003]; //可能更大。
  154. Options.Dir = Register_L[0x1004];
  155. Options.AccMod = Register_L[0x1005];
  156. Options.RunMod = Register_L[0x1006];
  157. Options.AllPulse = Register_L[0x1007];
  158. Options.StartPulse = Register_L[0x1008] - 1;
  159. Options.InitSpeed = InitSpeed;
  160. Options.AccUpTime = (Register_H[0x100B]<< 8) | Register_L[0x100B];
  161. Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];
  162. }
  163. /**
  164. * @brief 从寄存器读取脉冲参数
  165. * @return 无
  166. */
  167. void PLSRPluseLoad(void)
  168. {
  169. uint32_t temp_Frequency, temp_PulseCount;
  170. uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
  171. for (int i = 0; i < 10; i++)
  172. {
  173. Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
  174. , &Register_L[0x1100 + 0x10 * i]);
  175. Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
  176. , &Register_L[0x1101 + 0x10 * i]);
  177. Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
  178. Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
  179. , &Register_L[0x1102 + 0x10 * i]);
  180. Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
  181. , &Register_L[0x1103 + 0x10 * i]);
  182. Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
  183. PulseOutput[i].Frequency = temp_Frequency;
  184. PulseOutput[i].PulseCount = temp_PulseCount;
  185. PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
  186. PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
  187. }
  188. }
  189. /**
  190. * @brief 保存总的脉冲数至指定的寄存器
  191. * @return 无
  192. */
  193. void CountSave(void)
  194. {
  195. int32_t temp_Count = 0;
  196. uint16_t DatH, DatL;
  197. memcpy(&temp_Count, &AllPulseCNT, 4);
  198. DatH = (temp_Count >> 16);
  199. DatL = (temp_Count & 0x0000ffff);
  200. Register_H[0x2001] = (DatH >> 8);
  201. Register_L[0x2001] = (DatH & 0x00ff);
  202. Register_H[0x2000] = (DatL >> 8);
  203. Register_L[0x2000] = (DatL & 0x00ff);
  204. }