训练营PLSR题目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

211 行
5.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. #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. MAXPulseSet(PulseOutput[NowPulse].PulseCount); /* 更改结果脉冲数 */
  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 /* 如果是绝对模式 */
  106. {
  107. if(NowMAXPulse >= MAX_Pulse) /* 如果超出了上限 */
  108. {
  109. TIM2->ARR = - (MAX_Pulse - NowMAXPulse);
  110. }
  111. else
  112. {
  113. TIM2->ARR = PulseOutput[NowPulse].PulseCount;
  114. }
  115. }
  116. }
  117. /**
  118. * @brief 从寄存器读取设置参数
  119. * @return 无
  120. */
  121. void PLSROptionLoad(void)
  122. {
  123. /* 数据转换 */
  124. uint32_t InitSpeed;
  125. uint16_t tempH, tempL;
  126. Set8_16(&tempH, &Register_H[0x1009], &Register_L[0x1009]);
  127. Set8_16(&tempL, &Register_H[0x100A], &Register_L[0x100A]);
  128. Set16_32(&InitSpeed, &tempL, &tempH);
  129. /* 数据读取 */
  130. Options.SentPost = Register_L[0x1000];
  131. Options.DirPost = Register_L[0x1001];
  132. Options.EXT = Register_L[0x1002];
  133. Options.DirDelay = Register_L[0x1003]; //可能更大。
  134. Options.Dir = Register_L[0x1004];
  135. Options.AccMod = Register_L[0x1005];
  136. Options.RunMod = Register_L[0x1006];
  137. Options.AllPulse = Register_L[0x1007];
  138. Options.StartPulse = Register_L[0x1008] - 1;
  139. Options.InitSpeed = InitSpeed;
  140. Options.AccUpTime = (Register_H[0x100B]<< 8) | Register_L[0x100B];
  141. Options.AccDownTime = (Register_H[0x100C]<< 8) | Register_L[0x100C];
  142. }
  143. /**
  144. * @brief 从寄存器读取脉冲参数
  145. * @return 无
  146. */
  147. void PLSRPluseLoad(void)
  148. {
  149. uint32_t temp_Frequency, temp_PulseCount;
  150. uint16_t FrequencyDatH, FrequencyDatL, PulseCountDatH, PulseCountDatL;
  151. for (int i = 0; i < 10; i++)
  152. {
  153. Set8_16(&FrequencyDatH, &Register_H[0x1100 + 0x10 * i]
  154. , &Register_L[0x1100 + 0x10 * i]);
  155. Set8_16(&FrequencyDatL, &Register_H[0x1101 + 0x10 * i]
  156. , &Register_L[0x1101 + 0x10 * i]);
  157. Set16_32(&temp_Frequency, &FrequencyDatL, &FrequencyDatH);
  158. Set8_16(&PulseCountDatH, &Register_H[0x1102 + 0x10 * i]
  159. , &Register_L[0x1102 + 0x10 * i]);
  160. Set8_16(&PulseCountDatL, &Register_H[0x1103 + 0x10 * i]
  161. , &Register_L[0x1103 + 0x10 * i]);
  162. Set16_32(&temp_PulseCount, &PulseCountDatL, &PulseCountDatH);
  163. PulseOutput[i].Frequency = temp_Frequency;
  164. PulseOutput[i].PulseCount = temp_PulseCount;
  165. PulseOutput[i].EXT = Register_L[0x1104 + 0x10 * i];
  166. PulseOutput[i].NextPulse = Register_L[0x1105 + 0x10 * i];
  167. }
  168. }
  169. /**
  170. * @brief 保存总的脉冲数至指定的寄存器
  171. * @return 无
  172. */
  173. void CountSave(void)
  174. {
  175. int32_t temp_Count = 0;
  176. uint16_t DatH, DatL;
  177. memcpy(&temp_Count, &AllPulseCNT, 4);
  178. DatH = (temp_Count >> 16);
  179. DatL = (temp_Count & 0x0000ffff);
  180. Register_H[0x2001] = (DatH >> 8);
  181. Register_L[0x2001] = (DatH & 0x00ff);
  182. Register_H[0x2000] = (DatL >> 8);
  183. Register_L[0x2000] = (DatL & 0x00ff);
  184. }