phase_current.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include <math.h>
  2. #include "bsp/adc.h"
  3. #include "phase_current.h"
  4. #include "libs/utils.h"
  5. #include "libs/logger.h"
  6. #define NB_OFFSET_SAMPLES 32
  7. #define Rvbus 0.0005f
  8. #define Gvbus (13.1f) //母线电流的运放
  9. #define Rds_Defualt 0.005f//欧
  10. #define Gmos (1.7f)//mos 电流的运放
  11. #define Sample_R Rds_Defualt
  12. #define Lower_Pass_p 0.2f
  13. #define VBUS_VOL(adc) (((float)(adc)) * 3.3f / 4096.0f / Gvbus)
  14. #define MOSds_VOL(adc) (((float)(adc)) * 3.3f / 4096.0f / Gmos)
  15. #define current_i(v, r) ((v)/(r))
  16. void phase_current_init(current_samp_t *cs) {
  17. cs->offset_sample_count = NB_OFFSET_SAMPLES + 1;
  18. cs->Ia = 0.0f;
  19. cs->Ib = 0.0f;
  20. cs->Ic = 0.0f;
  21. }
  22. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  23. void phase_current_offset(current_samp_t *cs) {
  24. s32 phase_current1, phase_current2;
  25. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  26. if (cs->offset_sample_count == (NB_OFFSET_SAMPLES + 1)) {
  27. cs->offset_sample_count --;
  28. return;
  29. }
  30. if (cs->offset_sample_count > 0) {
  31. cs->offset_sample_count--;
  32. if (cs->sector == SECTOR_5 && cs->offset_sample_count >= 0) {
  33. cs->adc_offset_b += phase_current1;
  34. cs->adc_offset_a += phase_current2;
  35. if (cs->offset_sample_count == 0) {
  36. cs->adc_offset_b = cs->adc_offset_b / NB_OFFSET_SAMPLES;
  37. cs->adc_offset_a = cs->adc_offset_a / NB_OFFSET_SAMPLES;
  38. }
  39. }
  40. if (cs->sector == SECTOR_1 && cs->offset_sample_count >= 0) {
  41. cs->adc_offset_c += phase_current2;
  42. if (cs->offset_sample_count == 0) {
  43. cs->adc_offset_c = cs->adc_offset_c / NB_OFFSET_SAMPLES;
  44. }
  45. }
  46. }
  47. }
  48. #define LowPass_filter 1.0f
  49. void phase_current_sample(current_samp_t *cs){
  50. s32 phase_current1, phase_current2;
  51. float Ia, Ib, Ic;
  52. phase_time_t *time = &cs->time;
  53. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  54. if (time->three_shunts_flags == 1) {
  55. time->three_shunts_flags = 0;
  56. return; //use old current;
  57. }
  58. if (cs->sector == SECTOR_4 || cs->sector == SECTOR_5) {
  59. /* Current on Phase C is not accessible */
  60. /* Ia = PhaseAOffset - ADC converted value) */
  61. Ib = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_b), Rds_Defualt);
  62. LowPass_Filter(cs->Ib, Ib, LowPass_filter);
  63. Ia = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_a), Rds_Defualt);
  64. LowPass_Filter(cs->Ia, Ia, LowPass_filter);
  65. cs->Ic = -(cs->Ia + cs->Ib);
  66. }else if (cs->sector == SECTOR_1 || cs->sector == SECTOR_6) {
  67. /* Current on Phase A is not accessible */
  68. /* Ib = PhaseBOffset - ADC converted value) */
  69. Ib = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_b), Rds_Defualt);
  70. LowPass_Filter(cs->Ib, Ib, LowPass_filter);
  71. Ic = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_c), Rds_Defualt);
  72. LowPass_Filter(cs->Ic, Ic, LowPass_filter);
  73. cs->Ia = -(cs->Ib + cs->Ic);
  74. }else if (cs->sector == SECTOR_2 || cs->sector == SECTOR_3) {
  75. /* Current on Phase B is not accessible */
  76. /* Ia = PhaseAOffset - ADC converted value) */
  77. Ia = current_i(MOSds_VOL(phase_current1 - cs->adc_offset_a), Rds_Defualt);
  78. LowPass_Filter(cs->Ia, Ia, LowPass_filter);
  79. Ic = current_i(MOSds_VOL(phase_current2 - cs->adc_offset_c), Rds_Defualt);
  80. LowPass_Filter(cs->Ic, Ic, LowPass_filter);
  81. cs->Ib = -(cs->Ia + cs->Ic);
  82. }
  83. {
  84. static int count = 0;
  85. if (count++ % 2 == 0) {
  86. //log_chan_value(1, (int)(cs->Ia * 1000));
  87. }
  88. }
  89. }
  90. void get_phase_sample_point(current_samp_t *cs, u8 sector){
  91. phase_time_t *time = &cs->time;
  92. u32 low_side_low_duty = FOC_PWM_Half_Period - time->low;
  93. u32 low_side_mid_duty = FOC_PWM_Half_Period - time->midle;
  94. cs->sector = sector;
  95. time->Samp_p1 = FOC_PWM_Half_Period + 1;
  96. time->Samp_p2 = FOC_PWM_Half_Period + 1;
  97. /*底边开mos的时间是2倍的 low_side_low_duty(一个周期)*/
  98. if (low_side_low_duty * 2 >= TSampleMIN) { //可以采样
  99. if (low_side_low_duty >= (TADC + TDead)) {//可以在pwm的中心点采样
  100. time->Samp_p1 = FOC_PWM_Half_Period - 1;
  101. cs->sector = SECTOR_1;
  102. }else {
  103. u32 Samp_p = time->low + TSampleBefore;
  104. if (Samp_p >= FOC_PWM_Half_Period) { //需要在pwm中心点过后采样,需要配置PWM0模式
  105. time->Samp_p2 = ( 2u * FOC_PWM_Half_Period ) - Samp_p - (uint16_t) 1;
  106. //log_chan_value(2, time->Samp_p2);
  107. }else {
  108. time->Samp_p1 = Samp_p;
  109. //log_chan_value(4, time->Samp_p1);
  110. }
  111. }
  112. }else if (low_side_mid_duty * 2 >= TSampleMIN){
  113. if (low_side_mid_duty >= (TADC + TDead)) {//可以在pwm的中心点采样
  114. time->Samp_p1 = FOC_PWM_Half_Period - 1;
  115. }else {
  116. u32 Samp_p = time->midle + TSampleBefore;
  117. if (Samp_p >= FOC_PWM_Half_Period) { //需要在pwm中心点过后采样,需要配置PWM0模式
  118. time->Samp_p2 = ( 2u * FOC_PWM_Half_Period ) - Samp_p - (uint16_t) 1;
  119. //log_chan_value(3, time->Samp_p2);
  120. }else {
  121. time->Samp_p1 = Samp_p;
  122. //log_chan_value(5, time->Samp_p1);
  123. }
  124. }
  125. }else {
  126. time->three_shunts_flags = 1; //means do'nt use the sample current
  127. time->Samp_p1 = FOC_PWM_Half_Period - 1;//dumy trigger
  128. }
  129. }
  130. #else
  131. #define TBEFOR (TDead + MAX(TRise, TNoise))
  132. #define TMIN (TDead + MAX(TRise, TNoise) + TADC)
  133. static __inline u8 _get_sample_boundary(current_samp_t *cs, phase_time_t *time) {
  134. #if 0
  135. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  136. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  137. if (delta_duty0 <= TMIN && delta_duty1 <= TMIN) {
  138. return BOUNDARY_3;
  139. }else if (delta_duty0 <= TMIN && delta_duty1 > TMIN) {
  140. return BOUNDARY_2;
  141. }else if (delta_duty0 > TMIN && delta_duty1 <= TMIN) {
  142. return BOUNDARY_1;
  143. }else {
  144. return REGULAR;
  145. }
  146. #else
  147. return REGULAR;
  148. #endif
  149. }
  150. static __inline void _get_boundary1_samp(current_samp_t *cs, phase_time_t *time) {
  151. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  152. s32 delta_time_inc = (TMIN - delta_duty1);
  153. s32 delta_time_dec = min(delta_time_inc, delta_duty1);
  154. s32 sample_p;
  155. switch(cs->sector) {
  156. case SECTOR_1: //AB big and delta small
  157. time->Samp_p1 = time->B - TADC;
  158. sample_p = time->A;
  159. time->A = sample_p + delta_time_inc;
  160. time->A_next = sample_p - delta_time_dec;
  161. time->Samp_p2 = time->A - TADC;
  162. time->sampe_phase_1 = SAMP_NIC;
  163. time->sampe_phase_2 = SAMP_IA;
  164. break;
  165. case SECTOR_2://BA big and delta small
  166. time->Samp_p1 = time->A - TADC;
  167. sample_p = time->B;
  168. time->B = sample_p + delta_time_inc;
  169. time->B_next = sample_p - delta_time_dec;
  170. time->Samp_p2 = time->B - TADC;
  171. time->sampe_phase_1 = SAMP_NIC;
  172. time->sampe_phase_2 = SAMP_IB;
  173. break;
  174. case SECTOR_3://BC big and delta small
  175. time->Samp_p1 = time->C - TADC;
  176. sample_p = time->B;
  177. time->B = sample_p + delta_time_inc;
  178. time->B_next = sample_p - delta_time_dec;
  179. time->Samp_p2 = time->B - TADC;
  180. time->sampe_phase_1 = SAMP_NIA;
  181. time->sampe_phase_2 = SAMP_IB;
  182. break;
  183. case SECTOR_4://CB big and delta small
  184. time->Samp_p1 = time->B - TADC;
  185. sample_p = time->C;
  186. time->C = sample_p + delta_time_inc;
  187. time->C_next = sample_p - delta_time_dec;
  188. time->Samp_p2 = time->C - TADC;
  189. time->sampe_phase_1 = SAMP_NIA;
  190. time->sampe_phase_2 = SAMP_IC;
  191. break;
  192. case SECTOR_5://CA big and delta small
  193. time->Samp_p1 = time->A - TADC;
  194. sample_p = time->C;
  195. time->C = sample_p + delta_time_inc;
  196. time->C_next = sample_p - delta_time_dec;
  197. time->Samp_p2 = time->C - TADC;
  198. time->sampe_phase_1 = SAMP_NIB;
  199. time->sampe_phase_2 = SAMP_IC;
  200. break;
  201. case SECTOR_6://AC big and delta small
  202. time->Samp_p1 = time->C - TADC;
  203. sample_p = time->A;
  204. time->A = sample_p + delta_time_inc;
  205. time->A_next = sample_p - delta_time_dec;
  206. time->Samp_p2 = time->A - TADC;
  207. time->sampe_phase_1 = SAMP_NIB;
  208. time->sampe_phase_2 = SAMP_IA;
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. static __inline void _get_boundary2_samp(current_samp_t *cs, phase_time_t *time) {
  215. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  216. s32 delta_time_dec = (TMIN - delta_duty0);
  217. s32 delta_time_inc = min(delta_time_dec, delta_duty0);
  218. s32 sample_p;
  219. switch(cs->sector) {
  220. case SECTOR_1: //BC samll and delta small
  221. time->Samp_p2 = time->A - TADC;
  222. sample_p = time->C;
  223. time->C = sample_p - delta_time_dec;
  224. time->C_next = sample_p + delta_time_inc;
  225. time->Samp_p1 = time->B - TADC;
  226. time->sampe_phase_2 = SAMP_IA;
  227. time->sampe_phase_1 = SAMP_NIC;
  228. break;
  229. case SECTOR_2://AC samll and delta small
  230. time->Samp_p2 = time->B - TADC;
  231. sample_p = time->C;
  232. time->C = sample_p - delta_time_dec;
  233. time->C_next = sample_p + delta_time_inc;
  234. time->Samp_p1 = time->A - TADC;
  235. time->sampe_phase_2 = SAMP_IB;
  236. time->sampe_phase_1 = SAMP_NIC;
  237. break;
  238. case SECTOR_3://CA samll and delta small
  239. time->Samp_p2 = time->B - TADC;
  240. sample_p = time->A;
  241. time->A = sample_p - delta_time_dec;
  242. time->A_next = sample_p + delta_time_inc;
  243. time->Samp_p1 = time->C - TADC;
  244. time->sampe_phase_2 = SAMP_IB;
  245. time->sampe_phase_1 = SAMP_NIA;
  246. break;
  247. case SECTOR_4://BA samll and delta small
  248. time->Samp_p2 = time->C - TADC;
  249. sample_p = time->A;
  250. time->A = sample_p - delta_time_dec;
  251. time->A_next = sample_p + delta_time_inc;
  252. time->Samp_p1 = time->B - TADC;
  253. time->sampe_phase_2 = SAMP_IC;
  254. time->sampe_phase_1 = SAMP_NIA;
  255. break;
  256. case SECTOR_5://AB samll and delta small
  257. time->Samp_p2 = time->C - TADC;
  258. sample_p = time->B;
  259. time->B = sample_p - delta_time_dec;
  260. time->B_next = sample_p + delta_time_inc;
  261. time->Samp_p1 = time->A - TADC;
  262. time->sampe_phase_2 = SAMP_IC;
  263. time->sampe_phase_1 = SAMP_NIB;
  264. break;
  265. case SECTOR_6://CB samll and delta small
  266. time->Samp_p2 = time->A - TADC;
  267. sample_p = time->B;
  268. time->B = sample_p - delta_time_dec;
  269. time->B_next = sample_p + delta_time_inc;
  270. time->Samp_p1 = time->C - TADC;
  271. time->sampe_phase_2 = SAMP_IA;
  272. time->sampe_phase_1 = SAMP_NIB;
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. static __inline void _get_boundary3_samp(current_samp_t *cs, phase_time_t *time) {
  279. #if 1
  280. s32 sample_p;
  281. if ((time->boundary3_flags & 1) == 0) {
  282. time->boundary3_flags |= 1;
  283. sample_p = time->A;
  284. time->A = sample_p + TMIN;
  285. time->A_next = sample_p - TMIN;
  286. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  287. time->Samp_p2 = time->A - TADC;
  288. time->sampe_phase_1 = SAMP_OLDB;
  289. time->sampe_phase_2 = SAMP_IA;
  290. }else {
  291. time->boundary3_flags &= ~1;
  292. sample_p = time->B;
  293. time->B = sample_p + TMIN;
  294. time->B_next = sample_p - TMIN;
  295. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  296. time->Samp_p2 = time->B - TADC;
  297. time->sampe_phase_1 = SAMP_OLDA;
  298. time->sampe_phase_2 = SAMP_IB;
  299. }
  300. #else
  301. s32 delta_duty0 = (s32)time->midle - (s32)time->high;
  302. s32 delta_duty1 = (s32)time->low - (s32)time->midle;
  303. s32 delta_time_inc1 = (TMIN - delta_duty1);
  304. s32 delta_time_dec1 = min(delta_time_inc1, delta_duty1);
  305. s32 delta_time_dec2 = (TMIN - delta_duty0);
  306. s32 delta_time_inc2 = min(delta_time_dec2, delta_duty0);
  307. s32 sample_p;
  308. switch(cs->sector) {
  309. case SECTOR_1: //deltaBC > deltaAB
  310. if (delta_duty0 > delta_duty1) {
  311. sample_p = time->C;
  312. time->C = sample_p - delta_time_dec2;
  313. time->C_next = sample_p + delta_time_inc2;
  314. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  315. time->Samp_p2 = time->B - TADC;
  316. time->sampe_phase_1 = SAMP_OLDB;
  317. time->sampe_phase_2 = SAMP_NIC;
  318. }else {
  319. sample_p = time->A;
  320. time->A = sample_p + delta_time_inc1;
  321. time->A_next = sample_p - delta_time_dec1;
  322. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  323. time->Samp_p2 = time->A - TADC;
  324. time->sampe_phase_1 = SAMP_OLDC;
  325. time->sampe_phase_2 = SAMP_IA;
  326. }
  327. break;
  328. case SECTOR_2: //deltaAC > deltaBA
  329. if (delta_duty0 > delta_duty1) {
  330. sample_p = time->C;
  331. time->C = sample_p - delta_time_dec2;
  332. time->C_next = sample_p + delta_time_inc2;
  333. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  334. time->Samp_p2 = time->A - TADC;
  335. time->sampe_phase_1 = SAMP_OLDA;
  336. time->sampe_phase_2 = SAMP_NIC;
  337. }else {
  338. sample_p = time->B;
  339. time->B = sample_p + delta_time_inc1;
  340. time->B_next = sample_p - delta_time_dec1;
  341. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  342. time->Samp_p2 = time->B - TADC;
  343. time->sampe_phase_1 = SAMP_OLDB;
  344. time->sampe_phase_2 = SAMP_IB;
  345. }
  346. break;
  347. case SECTOR_3: //deltaCA > deltaBC
  348. if (delta_duty0 > delta_duty1) {
  349. sample_p = time->A;
  350. time->A = sample_p - delta_time_dec2;
  351. time->A_next = sample_p + delta_time_inc2;
  352. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  353. time->Samp_p2 = time->C - TADC;
  354. time->sampe_phase_1 = SAMP_OLDC;
  355. time->sampe_phase_2 = SAMP_NIA;
  356. }else {
  357. sample_p = time->B;
  358. time->B = sample_p + delta_time_inc1;
  359. time->B_next = sample_p - delta_time_dec1;
  360. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  361. time->Samp_p2 = time->B - TADC;
  362. time->sampe_phase_1 = SAMP_OLDA;
  363. time->sampe_phase_2 = SAMP_IB;
  364. }
  365. break;
  366. case SECTOR_4: //CBA, //deltaBA > deltaCB
  367. if (delta_duty0 > delta_duty1) {
  368. sample_p = time->A;
  369. time->A = sample_p - delta_time_dec2;
  370. time->A_next = sample_p + delta_time_inc2;
  371. time->Samp_p1 = time->B - (2 * TADC + TRise);//dumy trigger
  372. time->Samp_p2 = time->B - TADC;
  373. time->sampe_phase_1 = SAMP_OLDB;
  374. time->sampe_phase_2 = SAMP_NIA;
  375. }else {
  376. sample_p = time->C;
  377. time->C = sample_p + delta_time_inc1;
  378. time->C_next = sample_p - delta_time_dec1;
  379. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  380. time->Samp_p2 = time->C - TADC;
  381. time->sampe_phase_1 = SAMP_OLDC;
  382. time->sampe_phase_2 = SAMP_IC;
  383. }
  384. break;
  385. case SECTOR_5: //CAB, //deltaAB > deltaCA
  386. if (delta_duty0 > delta_duty1) {
  387. sample_p = time->B;
  388. time->B = sample_p - delta_time_dec2;
  389. time->B_next = sample_p + delta_time_inc2;
  390. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  391. time->Samp_p2 = time->A - TADC;
  392. time->sampe_phase_1 = SAMP_OLDA;
  393. time->sampe_phase_2 = SAMP_NIB;
  394. }else {
  395. sample_p = time->C;
  396. time->C = sample_p + delta_time_inc1;
  397. time->C_next = sample_p - delta_time_dec1;
  398. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  399. time->Samp_p2 = time->C - TADC;
  400. time->sampe_phase_1 = SAMP_OLDB;
  401. time->sampe_phase_2 = SAMP_IC;
  402. }
  403. break;
  404. case SECTOR_6: //ACB, //deltaCB > deltaAC
  405. if (delta_duty0 > delta_duty1) {
  406. sample_p = time->B;
  407. time->B = sample_p - delta_time_dec2;
  408. time->B_next = sample_p + delta_time_inc2;
  409. time->Samp_p1 = time->C - (2 * TADC + TRise);//dumy trigger
  410. time->Samp_p2 = time->C - TADC;
  411. time->sampe_phase_1 = SAMP_OLDC;
  412. time->sampe_phase_2 = SAMP_NIB;
  413. }else {
  414. sample_p = time->A;
  415. time->A = sample_p + delta_time_inc1;
  416. time->A_next = sample_p - delta_time_dec1;
  417. time->Samp_p1 = time->A - (2 * TADC + TRise);//dumy trigger
  418. time->Samp_p2 = time->A - TADC;
  419. time->sampe_phase_1 = SAMP_OLDA;
  420. time->sampe_phase_2 = SAMP_IA;
  421. }
  422. break;
  423. default:
  424. break;
  425. }
  426. #endif
  427. }
  428. static __inline void _get_regular_samp(current_samp_t *cs, phase_time_t *time) {
  429. time->Samp_p1 = time->midle - TADC;
  430. time->Samp_p2 = time->low - TADC;
  431. switch(cs->sector) {
  432. case SECTOR_1: //ABC
  433. time->sampe_phase_1 = SAMP_NIC;
  434. time->sampe_phase_2 = SAMP_IA;
  435. break;
  436. case SECTOR_2: //BAC
  437. time->sampe_phase_1 = SAMP_NIC;
  438. time->sampe_phase_2 = SAMP_IB;
  439. break;
  440. case SECTOR_3: //BCA
  441. time->sampe_phase_1 = SAMP_NIA;
  442. time->sampe_phase_2 = SAMP_IB;
  443. break;
  444. case SECTOR_4: //CBA
  445. time->sampe_phase_1 = SAMP_NIA;
  446. time->sampe_phase_2 = SAMP_IC;
  447. break;
  448. case SECTOR_5: //CAB
  449. time->sampe_phase_1 = SAMP_NIB;
  450. time->sampe_phase_2 = SAMP_IC;
  451. break;
  452. case SECTOR_6: //ACB
  453. time->sampe_phase_1 = SAMP_NIB;
  454. time->sampe_phase_2 = SAMP_IA;
  455. break;
  456. default:
  457. break;
  458. }
  459. }
  460. void phase_current_offset(current_samp_t *cs) {
  461. s32 phase_current1, phase_current2;
  462. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  463. if (cs->offset_sample_count > 0) {
  464. cs->offset_sample_count--;
  465. if (cs->offset_sample_count >= 0) {
  466. cs->adc_offset_a += phase_current1;
  467. cs->adc_offset_b += phase_current2;
  468. if (cs->offset_sample_count == 0) {
  469. cs->adc_offset_a = cs->adc_offset_a / NB_OFFSET_SAMPLES;
  470. cs->adc_offset_b = cs->adc_offset_b / NB_OFFSET_SAMPLES;
  471. }
  472. }
  473. }
  474. }
  475. void phase_current_sample(current_samp_t *cs){
  476. s32 phase_current1, phase_current2;
  477. u8 b_curr_a = 0;
  478. u8 b_curr_b = 0;
  479. u8 b_curr_c = 0;
  480. phase_time_t *time = &cs->time;
  481. adc_phase_current_read(cs->sector, &phase_current1, &phase_current2);
  482. phase_current1 -= cs->adc_offset_a;
  483. phase_current2 -= cs->adc_offset_b;
  484. float current = current_i(VBUS_VOL(abs(phase_current1)), Sample_R);
  485. switch (time->sampe_phase_1) {
  486. case SAMP_IA:
  487. cs->Ia = current;
  488. b_curr_a = 1;
  489. break;
  490. case SAMP_IB:
  491. cs->Ib = current;
  492. b_curr_b = 1;
  493. break;
  494. case SAMP_IC:
  495. cs->Ic = current;
  496. b_curr_c = 1;
  497. break;
  498. case SAMP_NIA:
  499. cs->Ia = -current;
  500. b_curr_a = 1;
  501. break;
  502. case SAMP_NIB:
  503. cs->Ib = -current;
  504. b_curr_b = 1;
  505. break;
  506. case SAMP_NIC:
  507. cs->Ic = -current;
  508. b_curr_c = 1;
  509. break;
  510. case SAMP_OLDA:
  511. cs->Ia = cs->old_Ia;
  512. b_curr_a = 1;
  513. break;
  514. case SAMP_OLDB:
  515. cs->Ib = cs->old_Ib;
  516. b_curr_b = 1;
  517. break;
  518. case SAMP_OLDC:
  519. cs->Ic = cs->old_Ic;
  520. b_curr_c = 1;
  521. break;
  522. default:
  523. break;
  524. }
  525. current = current_i(VBUS_VOL(abs(phase_current2)), Sample_R);
  526. switch (time->sampe_phase_2) {
  527. case SAMP_IA:
  528. cs->Ia = current;
  529. b_curr_a = 1;
  530. break;
  531. case SAMP_IB:
  532. cs->Ib = current;
  533. b_curr_b = 1;
  534. break;
  535. case SAMP_IC:
  536. cs->Ic = current;
  537. b_curr_c = 1;
  538. break;
  539. case SAMP_NIA:
  540. cs->Ia = -current;
  541. b_curr_a = 1;
  542. break;
  543. case SAMP_NIB:
  544. cs->Ib = -current;
  545. b_curr_b = 1;
  546. break;
  547. case SAMP_NIC:
  548. cs->Ic = -current;
  549. b_curr_c = 1;
  550. break;
  551. case SAMP_OLDA:
  552. cs->Ia = cs->old_Ia;
  553. b_curr_a = 1;
  554. break;
  555. case SAMP_OLDB:
  556. cs->Ib = cs->old_Ib;
  557. b_curr_b = 1;
  558. break;
  559. case SAMP_OLDC:
  560. cs->Ic = cs->old_Ic;
  561. b_curr_c = 1;
  562. break;
  563. default:
  564. break;
  565. }
  566. if (b_curr_a == 0) {
  567. cs->Ia = -(cs->Ib + cs->Ic);
  568. }
  569. if (b_curr_b == 0) {
  570. cs->Ib = -(cs->Ia + cs->Ic);
  571. }
  572. if (b_curr_c == 0) {
  573. cs->Ic = -(cs->Ia + cs->Ib);
  574. }
  575. cs->old_Ia = cs->Ia;
  576. cs->old_Ib = cs->Ib;
  577. cs->old_Ic = cs->Ic;
  578. {
  579. static int count = 0;
  580. if (count++ % 3 == 0) {
  581. log_chan_value(1, (int)(cs->Ia * 1000));
  582. }
  583. }
  584. }
  585. void get_phase_sample_point(current_samp_t *cs, u8 sector){
  586. phase_time_t *time = &cs->time;
  587. if (cs->is_calibrating_offset) {
  588. time->Samp_p1 = FOC_PWM_Half_Period - 2 * TMIN;
  589. time->Samp_p2 = FOC_PWM_Half_Period - 1;
  590. return;
  591. }
  592. cs->sector = sector;
  593. time->A_next = time->A;
  594. time->B_next = time->B;
  595. time->C_next = time->C;
  596. u8 boundary = _get_sample_boundary(cs, time);
  597. if (boundary == BOUNDARY_1) {
  598. _get_boundary1_samp(cs, time);
  599. }else if (boundary == BOUNDARY_2) {
  600. _get_boundary2_samp(cs, time);
  601. }else if (boundary == BOUNDARY_3) {
  602. _get_boundary3_samp(cs, time);
  603. }else { //REGULAR, 直接可以采样
  604. _get_regular_samp(cs, time);
  605. }
  606. }
  607. #endif
  608. void phase_current_adc_triger(current_samp_t *cs){
  609. adc_enable_ext_trigger();
  610. }