训练营PLSR题目
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

215 řádky
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. 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. * @return 无
  68. */
  69. void SetNextPulse(void)
  70. {
  71. int32_t temp;
  72. if (Options.RunMod == 0) /* 如果是相对模式 */
  73. {
  74. if (PulseOutput[NowPulse].PulseCount > 0)
  75. {
  76. if (Options.AllPulse - PulseNum == 1)
  77. {
  78. TIM2->ARR = PulseOutput[NowPulse].PulseCount - N_Acc;
  79. }
  80. else
  81. {
  82. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  83. }
  84. }
  85. else
  86. {
  87. if (Options.AllPulse - PulseNum == 1)
  88. {
  89. TIM2->ARR = - PulseOutput[NowPulse].PulseCount - N_Acc;
  90. }
  91. else
  92. {
  93. TIM2->ARR = - PulseOutput[NowPulse].PulseCount;
  94. }
  95. }
  96. }
  97. else if (Options.RunMod == 1)/* 如果是绝对模式 */
  98. {
  99. if (PulseOutput[NowPulse].PulseCount * PulseOutput[PrePulse].PulseCount >= 0)
  100. {
  101. temp = PulseOutput[NowPulse].PulseCount - PulseOutput[PrePulse].PulseCount;
  102. }
  103. else
  104. {
  105. temp = PulseOutput[NowPulse].PulseCount + PulseOutput[PrePulse].PulseCount;
  106. }
  107. if(temp >= 0)
  108. {
  109. TIM2->ARR = temp;
  110. }
  111. else
  112. {
  113. TIM2->ARR = - temp;
  114. }
  115. if (Options.StartPulse == NowPulse)
  116. {
  117. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  118. }
  119. }
  120. }
  121. /**
  122. * @brief 从寄存器读取设置参数
  123. * @return 无
  124. */
  125. void PLSROptionLoad(void)
  126. {
  127. /* 数据转换 */
  128. uint32_t InitSpeed;
  129. uint16_t tempH, tempL;
  130. Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
  131. Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
  132. Set16_32(&InitSpeed, &tempL, &tempH);
  133. /* 数据读取 */
  134. Options.SentPost = Register_L[0x1000];
  135. Options.DirPost = Register_L[0x1001];
  136. Options.EXT = Register_L[0x1002];
  137. Options.DirDelay = Register_L[0x1003]; //可能更大。
  138. Options.Dir = Register_L[0x1004];
  139. Options.AccMod = Register_L[0x1005];
  140. Options.RunMod = Register_L[0x1006];
  141. Options.AllPulse = Register_L[0x1007];
  142. Options.StartPulse = Register_L[0x1008] - 1;
  143. Options.InitSpeed = InitSpeed;
  144. Options.AccUpTime = (Register_H[0x100B]<< 8) | Register_L[0x100B];
  145. Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];
  146. }
  147. /**
  148. * @brief 从寄存器读取脉冲参数
  149. * @return 无
  150. */
  151. void PLSRPluseLoad(void)
  152. {
  153. uint32_t temp_Frequency, temp_PulseCount;
  154. uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
  155. for (int i = 0; i < 10; i++)
  156. {
  157. Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
  158. , &Register_L[0x1100 + 0x10 * i]);
  159. Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
  160. , &Register_L[0x1101 + 0x10 * i]);
  161. Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
  162. Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
  163. , &Register_L[0x1102 + 0x10 * i]);
  164. Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
  165. , &Register_L[0x1103 + 0x10 * i]);
  166. Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
  167. PulseOutput[i].Frequency = temp_Frequency;
  168. PulseOutput[i].PulseCount = temp_PulseCount;
  169. PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
  170. PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
  171. }
  172. }
  173. /**
  174. * @brief 保存总的脉冲数至指定的寄存器
  175. * @return 无
  176. */
  177. void CountSave(void)
  178. {
  179. int32_t temp_Count = 0;
  180. uint16_t DatH, DatL;
  181. memcpy(&temp_Count, &AllPulseCNT, 4);
  182. DatH = (temp_Count >> 16);
  183. DatL = (temp_Count & 0x0000ffff);
  184. Register_H[0x2001] = (DatH >> 8);
  185. Register_L[0x2001] = (DatH & 0x00ff);
  186. Register_H[0x2000] = (DatL >> 8);
  187. Register_L[0x2000] = (DatL & 0x00ff);
  188. }