samples.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_filtered(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. s16 get_motor_temp_raw(void) {
  121. return motor_temp.value;
  122. }
  123. s16 get_mos_temp_raw(void) {
  124. return mos_temp.value;
  125. }
  126. float get_throttle_float(void) {
  127. #ifdef THROTTLE_CHAN
  128. return _throttle[0].filted_value;
  129. #else
  130. return 0.0f;
  131. #endif
  132. }
  133. float get_throttle2_float(void) {
  134. #ifdef THROTTLE2_CHAN
  135. return _throttle[1].filted_value;
  136. #else
  137. return get_throttle_float();
  138. #endif
  139. }
  140. float get_adc_vref(void) {
  141. return _vref.filted_value;
  142. }
  143. float get_thro_5v_float(void) {
  144. u16 adc = adc_get_thro_5v();
  145. if (adc == 0xFFFF) {
  146. return 5.0f;
  147. }
  148. return (float)adc * (ADC_REFERENCE_VOLTAGE*(2.0f)/ADC_FULL_MAX);
  149. }
  150. float get_thro2_5v_float(void) {
  151. u16 adc = adc_get_thro2_5v();
  152. if (adc == 0xFFFF) {
  153. return 5.0f;
  154. }
  155. return (float)adc * (ADC_REFERENCE_VOLTAGE*(2.0f)/ADC_FULL_MAX);
  156. }
  157. static u32 sample_task(void *param) {
  158. sample_vbus();
  159. sample_ibus();
  160. sample_throttle();
  161. //sample_uvw_phase();
  162. sample_motor_temp();
  163. sample_mos_temp();
  164. sample_vref();
  165. return 0;
  166. }
  167. void sample_vref(void) {
  168. float vadc = adc_get_vref();
  169. _vref.value = ADC_REFERENCE_VOLTAGE * vadc / ADC_FULL_MAX;
  170. LowPass_Filter(_vref.filted_value, _vref.value, _vref.lowpass);
  171. }
  172. float sample_vbus_raw(void) {
  173. s16 vadc = adc_get_vbus();
  174. return (float)vadc * VBUS_VOL_CEOF;
  175. }
  176. float sample_acc_vol_raw(void) {
  177. s16 vadc = adc_get_acc();
  178. return (float)vadc * VBUS_VOL_CEOF;
  179. }
  180. void sample_vbus(void){
  181. u32 ticks = task_ticks_abs();
  182. _vbus.value = sample_vbus_raw();
  183. LowPass_Filter(_vbus.filted_value, _vbus.value, _vbus.lowpass);
  184. _vbus.filted_int = (int)_vbus.filted_value;
  185. #ifdef ACC_V_CHAN
  186. acc_vol.value = sample_acc_vol_raw();
  187. LowPass_Filter(acc_vol.filted_value, acc_vol.value, acc_vol.lowpass);
  188. acc_vol.filted_int = (int)acc_vol.filted_value;
  189. #endif
  190. }
  191. float sample_ibus_raw(void) {
  192. #ifdef VBUS_I_POSITIVE
  193. s16 vadc = adc_get_ibus() - _ibus.adc_offset;
  194. #else
  195. s16 vadc = _ibus.adc_offset - adc_get_ibus();
  196. #endif
  197. return (float)vadc * VBUS_I_CEOF;
  198. }
  199. void sample_ibus(void) {
  200. #ifdef VBUS_I_CHAN
  201. _ibus.value = sample_ibus_raw();
  202. LowPass_Filter(_ibus.filted_value, _ibus.value, _ibus.lowpass);
  203. #endif
  204. }
  205. void sample_ibus_offset(u16 offset) {
  206. #ifdef VBUS_I_CHAN
  207. _ibus.adc_offset = offset;
  208. #endif
  209. }
  210. void sample_throttle(void){
  211. #ifdef THROTTLE_CHAN
  212. s16 vadc = adc_get_throttle();
  213. _throttle[0].value = (float)vadc * THROTTLE_VOL_CEOF;
  214. LowPass_Filter(_throttle[0].filted_value, _throttle[0].value, _throttle[0].lowpass);
  215. #endif
  216. #ifdef THROTTLE2_CHAN
  217. s16 vadc2 = adc_get_throttle2();
  218. _throttle[1].value = (float)vadc2 * THROTTLE_VOL_CEOF;
  219. LowPass_Filter(_throttle[1].filted_value, _throttle[1].value, _throttle[1].lowpass);
  220. #endif
  221. }
  222. void get_uvw_phases_raw(float *uvw_raw) {
  223. u16 uvw[3];
  224. adc_get_uvw_phaseV(uvw);
  225. uvw_raw[0] = ((float)uvw[0] * UVW_VOL_CEOF);
  226. uvw_raw[1] = ((float)uvw[1] * UVW_VOL_CEOF);
  227. uvw_raw[2] = ((float)uvw[2] * UVW_VOL_CEOF);
  228. }
  229. void sample_uvw_phase(void) {
  230. #ifdef U_VOL_ADC_CHAN
  231. float uvw[3];
  232. get_uvw_phases_raw(uvw);
  233. _uvw_phase[0].value = uvw[0];
  234. LowPass_Filter(_uvw_phase[0].filted_value, _uvw_phase[0].value, _uvw_phase[0].lowpass);
  235. _uvw_phase[1].value = uvw[1];
  236. LowPass_Filter(_uvw_phase[1].filted_value, _uvw_phase[1].value, _uvw_phase[1].lowpass);
  237. _uvw_phase[2].value = uvw[2];
  238. LowPass_Filter(_uvw_phase[2].filted_value, _uvw_phase[2].value, _uvw_phase[2].lowpass);
  239. #endif
  240. }
  241. void sample_motor_temp(void) {
  242. #ifdef CONFIG_BOARD_MCXXX
  243. u16 adc = adc_get_motor_temp();
  244. u32 r = (u32)MOTOR_TEMP_R(adc);
  245. if (r > 65535) {
  246. r = 65535;
  247. }
  248. motor_temp.value = ntc_get_motor_temp(r);
  249. LowPass_Filter(motor_temp.filted_value, motor_temp.value, motor_temp.lowpass);
  250. #endif
  251. }
  252. void sample_mos_temp(void) {
  253. #ifdef CONFIG_BOARD_MCXXX
  254. u16 adc = adc_get_mos_temp();
  255. u32 r = (u32)MOS_TEMP_R(adc);
  256. if (r > 65535) {
  257. r = 65535;
  258. }
  259. mos_temp.value = ntc_get_mos_temp(r);
  260. LowPass_Filter(mos_temp.filted_value, mos_temp.value, mos_temp.lowpass);
  261. #endif
  262. }
  263. void sample_log(void) {
  264. sys_debug("Thro Vol %f-%f, %f-%f\n", get_thro_5v_float(), get_throttle_float(), get_thro2_5v_float(), get_throttle2_float());
  265. }