samples.c 6.2 KB

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