samples.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "foc/samples.h"
  2. #include "bsp/bsp_driver.h"
  3. #include "math/fast_math.h"
  4. #include "math/fix_math.h"
  5. #include "os/os_task.h"
  6. #include "foc/foc_config.h"
  7. #include "foc/ntc.h"
  8. #include "libs/logger.h"
  9. typedef struct {
  10. float value;
  11. float filted_value;
  12. int filted_int;
  13. u16 adc_offset;
  14. float lowpass;
  15. }samples_t;
  16. static u32 sample_task(void *);
  17. static samples_t _vbus;
  18. static samples_t _vref;
  19. #ifdef THROTTLE_CHAN
  20. static samples_t _throttle[2];
  21. #endif
  22. #ifdef U_VOL_ADC_CHAN
  23. static samples_t _uvw_phase[3];
  24. #endif
  25. #ifdef MOTOR_TEMP_ADC_CHAN
  26. static samples_t motor_temp;
  27. #endif
  28. #ifdef MOS_TEMP_ADC_CHAN
  29. static samples_t mos_temp;
  30. #endif
  31. #ifdef ACC_V_CHAN
  32. static samples_t acc_vol;
  33. #endif
  34. #ifdef VBUS_I_CHAN
  35. static samples_t _ibus;
  36. #endif
  37. void samples_init(void){
  38. _vref.filted_value = 0;
  39. _vref.value = 0;
  40. _vref.lowpass = 0.01f;
  41. sample_vref();
  42. _vbus.value = ((float)adc_get_vbus() * VBUS_VOL_CEOF);
  43. _vbus.filted_value = _vbus.value;
  44. _vbus.lowpass = (0.01f);
  45. #ifdef ACC_V_CHAN
  46. acc_vol.value = ((float)adc_get_acc() * VBUS_VOL_CEOF);
  47. acc_vol.filted_value = (CONFIG_RATED_DC_VOL);
  48. acc_vol.lowpass = (0.01f);
  49. #endif
  50. sample_vbus();
  51. #ifdef THROTTLE_CHAN
  52. _throttle[0].value = (float)adc_get_throttle() * THROTTLE_VOL_CEOF;
  53. _throttle[0].filted_value = _throttle[0].value;
  54. _throttle[0].lowpass = (0.01f);
  55. #ifdef THROTTLE2_CHAN
  56. _throttle[1].value = (float)adc_get_throttle2() * THROTTLE_VOL_CEOF;
  57. _throttle[1].filted_value = _throttle[1].value;
  58. _throttle[1].lowpass = (0.01f);
  59. #endif
  60. sample_throttle();
  61. #endif
  62. #ifdef U_VOL_ADC_CHAN
  63. _uvw_phase[0].value = _uvw_phase[0].filted_value = 0;
  64. _uvw_phase[0].lowpass = 0.01f;
  65. _uvw_phase[1].value = _uvw_phase[1].filted_value = 0;
  66. _uvw_phase[1].lowpass = 0.01f;
  67. _uvw_phase[2].value = _uvw_phase[2].filted_value = 0;
  68. _uvw_phase[2].lowpass = 0.01f;
  69. sample_uvw_phase();
  70. #endif
  71. #ifdef MOTOR_TEMP_ADC_CHAN
  72. motor_temp.value = motor_temp.filted_value = 0;
  73. motor_temp.lowpass = 0.1f;
  74. sample_motor_temp();
  75. #endif
  76. #ifdef MOS_TEMP_ADC_CHAN
  77. mos_temp.value = mos_temp.filted_value = 0;
  78. mos_temp.lowpass = 0.1f;
  79. sample_mos_temp();
  80. #endif
  81. #ifdef VBUS_I_CHAN
  82. _ibus.value = _ibus.filted_value = 0;
  83. _ibus.lowpass = 0.01f;
  84. sample_ibus();
  85. #endif
  86. shark_task_create(sample_task, NULL);
  87. }
  88. void get_phase_vols(float *uvw) {
  89. uvw[0] = _uvw_phase[0].filted_value;
  90. uvw[1] = _uvw_phase[1].filted_value;
  91. uvw[2] = _uvw_phase[2].filted_value;
  92. }
  93. float get_vbus_float(void) {
  94. return (_vbus.filted_value);
  95. }
  96. int get_vbus_int(void){
  97. return _vbus.filted_int;
  98. }
  99. int get_acc_vol(void) {
  100. #ifdef ACC_V_CHAN
  101. return acc_vol.filted_int;
  102. #else
  103. return get_vbus_int();
  104. #endif
  105. }
  106. float get_vbus_current(void) {
  107. #ifdef VBUS_I_CHAN
  108. s16 ibus = _ibus.filted_value * 10.0f;
  109. return (float)ibus/10.0f;
  110. #else
  111. return 0;
  112. #endif
  113. }
  114. s16 get_motor_temp(void) {
  115. return motor_temp.filted_value;
  116. }
  117. s16 get_mos_temp(void) {
  118. return mos_temp.filted_value;
  119. }
  120. float get_throttle_float(void) {
  121. #ifdef THROTTLE_CHAN
  122. return _throttle[0].filted_value;
  123. #else
  124. return 0.0f;
  125. #endif
  126. }
  127. float get_throttle2_float(void) {
  128. #ifdef THROTTLE2_CHAN
  129. return _throttle[1].filted_value;
  130. #else
  131. return get_throttle_float();
  132. #endif
  133. }
  134. float get_adc_vref(void) {
  135. return _vref.filted_value;
  136. }
  137. float get_thro_5v_float(void) {
  138. u16 adc = adc_get_thro_5v();
  139. if (adc == 0) {
  140. return 5.0f;
  141. }
  142. return (float)adc * (ADC_REFERENCE_VOLTAGE*(2.0f)/ADC_FULL_MAX);
  143. }
  144. float get_thro2_5v_float(void) {
  145. u16 adc = adc_get_thro2_5v();
  146. if (adc == 0) {
  147. return 5.0f;
  148. }
  149. return (float)adc * (ADC_REFERENCE_VOLTAGE*(2.0f)/ADC_FULL_MAX);
  150. }
  151. static u32 sample_task(void *param) {
  152. sample_vbus();
  153. sample_ibus();
  154. sample_throttle();
  155. sample_uvw_phase();
  156. sample_motor_temp();
  157. sample_mos_temp();
  158. sample_vref();
  159. return 0;
  160. }
  161. void sample_vref(void) {
  162. float vadc = adc_get_vref();
  163. _vref.value = ADC_REFERENCE_VOLTAGE * vadc / ADC_FULL_MAX;
  164. LowPass_Filter(_vref.filted_value, _vref.value, _vref.lowpass);
  165. }
  166. float sample_vbus_raw(void) {
  167. s16 vadc = adc_get_vbus();
  168. return (float)vadc * VBUS_VOL_CEOF;
  169. }
  170. float sample_acc_vol_raw(void) {
  171. s16 vadc = adc_get_acc();
  172. return (float)vadc * VBUS_VOL_CEOF;
  173. }
  174. void sample_vbus(void){
  175. u32 ticks = task_ticks_abs();
  176. _vbus.value = sample_vbus_raw();
  177. LowPass_Filter(_vbus.filted_value, _vbus.value, _vbus.lowpass);
  178. _vbus.filted_int = (int)_vbus.filted_value;
  179. #ifdef ACC_V_CHAN
  180. acc_vol.value = sample_acc_vol_raw();
  181. LowPass_Filter(acc_vol.filted_value, acc_vol.value, acc_vol.lowpass);
  182. acc_vol.filted_int = (int)acc_vol.filted_value;
  183. #endif
  184. }
  185. float sample_ibus_raw(void) {
  186. #ifdef VBUS_I_POSITIVE
  187. s16 vadc = adc_get_ibus() - _ibus.adc_offset;
  188. #else
  189. s16 vadc = _ibus.adc_offset - adc_get_ibus();
  190. #endif
  191. return (float)vadc * VBUS_I_CEOF;
  192. }
  193. void sample_ibus(void) {
  194. #ifdef VBUS_I_CHAN
  195. _ibus.value = sample_ibus_raw();
  196. LowPass_Filter(_ibus.filted_value, _ibus.value, _ibus.lowpass);
  197. #endif
  198. }
  199. void sample_ibus_offset(u16 offset) {
  200. #ifdef VBUS_I_CHAN
  201. _ibus.adc_offset = offset;
  202. #endif
  203. }
  204. void sample_throttle(void){
  205. #ifdef THROTTLE_CHAN
  206. s16 vadc = adc_get_throttle();
  207. _throttle[0].value = (float)vadc * THROTTLE_VOL_CEOF;
  208. LowPass_Filter(_throttle[0].filted_value, _throttle[0].value, _throttle[0].lowpass);
  209. #endif
  210. #ifdef THROTTLE2_CHAN
  211. s16 vadc2 = adc_get_throttle2();
  212. _throttle[1].value = (float)vadc2 * THROTTLE_VOL_CEOF;
  213. LowPass_Filter(_throttle[1].filted_value, _throttle[1].value, _throttle[1].lowpass);
  214. #endif
  215. }
  216. void sample_uvw_phases_raw(s16 *uvw_raw) {
  217. u16 uvw[3];
  218. adc_get_uvw_phaseV(uvw);
  219. uvw_raw[0] = (s16)((float)uvw[0] * UVW_VOL_CEOF * 100.0f);
  220. uvw_raw[1] = (s16)((float)uvw[1] * UVW_VOL_CEOF * 100.0f);
  221. uvw_raw[2] = (s16)((float)uvw[2] * UVW_VOL_CEOF * 100.0f);
  222. }
  223. void sample_uvw_phase(void) {
  224. #ifdef U_VOL_ADC_CHAN
  225. u16 uvw[3];
  226. adc_get_uvw_phaseV(uvw);
  227. _uvw_phase[0].value = (float)uvw[0] * UVW_VOL_CEOF;
  228. LowPass_Filter(_uvw_phase[0].filted_value, _uvw_phase[0].value, _uvw_phase[0].lowpass);
  229. _uvw_phase[1].value = (float)uvw[1] * UVW_VOL_CEOF;
  230. LowPass_Filter(_uvw_phase[1].filted_value, _uvw_phase[1].value, _uvw_phase[1].lowpass);
  231. _uvw_phase[2].value = (float)uvw[2] * UVW_VOL_CEOF;
  232. LowPass_Filter(_uvw_phase[2].filted_value, _uvw_phase[2].value, _uvw_phase[2].lowpass);
  233. #endif
  234. }
  235. void sample_motor_temp(void) {
  236. #ifdef CONFIG_BOARD_MCXXX
  237. u16 adc = adc_get_motor_temp();
  238. u32 r = (u32)MOTOR_TEMP_R(adc);
  239. if (r > 65535) {
  240. r = 65535;
  241. }
  242. motor_temp.value = ntc_get_motor_temp(r);
  243. LowPass_Filter(motor_temp.filted_value, motor_temp.value, motor_temp.lowpass);
  244. #endif
  245. }
  246. void sample_mos_temp(void) {
  247. #ifdef CONFIG_BOARD_MCXXX
  248. u16 adc = adc_get_mos_temp();
  249. u32 r = (u32)MOS_TEMP_R(adc);
  250. if (r > 65535) {
  251. r = 65535;
  252. }
  253. mos_temp.value = ntc_get_mos_temp(r);
  254. LowPass_Filter(mos_temp.filted_value, mos_temp.value, mos_temp.lowpass);
  255. #endif
  256. }
  257. void sample_log(void) {
  258. sys_debug("Thro Vol %f-%f, %f-%f\n", get_thro_5v_float(), get_throttle_float(), get_thro2_5v_float(), get_throttle2_float());
  259. }