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

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