diff --git a/circuit/README.md b/circuit/README.md index ec9b2e88050e0edecacdfdc0b74cc96942f0baee..2bce403760b568cb716bc56579de3195b2f0c591 100644 --- a/circuit/README.md +++ b/circuit/README.md @@ -22,6 +22,7 @@ This perhaps means I need to run that SPI cable with a differential driver, to b - wants one lo-side debug pin! - for resets etc, pull en to gnd on other side of switch! - wants spi debug pins + - wants more gnd pins, for scope, gnd pins to be not beside 3v3 - might have to go to DRV8320 - newer, available - CSD88548 is CSD88599 but more amps less volts, use these diff --git a/embedded/atkbldcdriver/atkbldcdriver/atkbldcdriver.cproj b/embedded/atkbldcdriver/atkbldcdriver/atkbldcdriver.cproj index 7327bcc799a1d688ea4a1133726dd91983f30ae0..ad4566f3e87d57bd0d89280031fcf69197b5bd3b 100644 --- a/embedded/atkbldcdriver/atkbldcdriver/atkbldcdriver.cproj +++ b/embedded/atkbldcdriver/atkbldcdriver/atkbldcdriver.cproj @@ -178,6 +178,9 @@ <Compile Include="ringbuffer.h"> <SubType>compile</SubType> </Compile> + <Compile Include="sinelut.h"> + <SubType>compile</SubType> + </Compile> <Compile Include="spiport.c"> <SubType>compile</SubType> </Compile> diff --git a/embedded/atkbldcdriver/atkbldcdriver/main.c b/embedded/atkbldcdriver/atkbldcdriver/main.c index cf24a05e5315f322786a934b94b4006f35e55ad7..7612e062aafc4d301d26442a482c4e58adc9aeca 100644 --- a/embedded/atkbldcdriver/atkbldcdriver/main.c +++ b/embedded/atkbldcdriver/atkbldcdriver/main.c @@ -9,6 +9,7 @@ #include <avr/interrupt.h> #include "hardware.h" #include "fastmath.h" +#include "sinelut.h" // first setup all the pins // want six step commutation, or sinpwm on encoder reading? @@ -51,6 +52,20 @@ void encoder_init(void){ ams5047_init(&ams5047, &spiEncoder); } +#define ENC_RESOLUTION 16835 + +// a handful of constants, probably need many less +// divisor is # of poles on motor / 2 (for each n/s pair, one set of 3 coils per full rotation) +const static uint16_t enc_resolution = ENC_RESOLUTION; +const static uint8_t enc_reverse = 1; // 1 or 0, for reverse op +const static uint16_t twoPi_enc = 2340; //ENC_RESOLUTION / 7; // divisor is # poles / 2 +const static uint16_t pi_enc = 1170; +const static int16_t enc_offset = -600; // start at midpoint - adjust -ve or +ve from + +static uint16_t enc_reading = 0; +static uint16_t phase_target = 0; +// 2^14 = 16,384 + void pwm_periods(uint16_t peru, uint16_t perv, uint16_t perw){ // check overrun (peru > 1024) ? peru = 1024 : (0); @@ -78,6 +93,23 @@ void pwm_by_offset(int16_t ofu, int16_t ofv, int16_t ofw){ pwm_periods(peru, perv, perw); } +void pwm_by_sin_duty(uint16_t phase, float duty){ + // phases respective of home + int16_t pu = phase; + (pu >= twoPi_enc) ? (pu -= twoPi_enc) : (0); + int16_t pv = phase + 780; + (pv >= twoPi_enc) ? (pv -= twoPi_enc) : (0); + int16_t pw = phase + 1560; + (pw >= twoPi_enc) ? (pw -= twoPi_enc) : (0); + + // zero-set offset + int16_t peru = ((sinelut[pu] - 512) * duty) + 512; + int16_t perv = ((sinelut[pv] - 512) * duty) + 512; + int16_t perw = ((sinelut[pw] - 512) * duty) + 512; + + pwm_periods(peru, perv, perw); +} + void pwm_init(void){ // setup awex etc @@ -121,7 +153,7 @@ void drv_init(void){ // setup drv8302 mode pin_clear(&drvModePwm); // low for 6-channel pwm, hi and lo sides from uc - pin_clear(&drvModeGain); // low for 10v/v, hi for 40v/v current sense gains + pin_set(&drvModeGain); // low for 10v/v, hi for 40v/v current sense gains pin_clear(&drvDcCal); // turn DC cal off, we turn this high to set midpoint on amps pin_clear(&drvEnPin); // disable the gate driver, to start. also broken by no/go hardware switch } @@ -193,7 +225,7 @@ int main(void) bldc_init(&bldc); // on startup, speed (if clcomm) and duty //bldc_setTargetSpeed(&bldc, 1000); - bldc_setDuty(&bldc, 30); + // bldc_setDuty(&bldc, 10); // startup the driver drv_init(); // and enable the gate @@ -215,87 +247,20 @@ int main(void) } } -int8_t comTable[6][3] = { - {1,-1,0}, - {1,0,-1}, - {0,1,-1}, - {-1,1,0}, - {-1,0,1}, - {0,-1,1} -}; - -#define ENC_RESOLUTION 16834 - -// a handful of constants, probably need many less -// divisor is # of poles on motor / 2 (for each n/s pair, one set of 3 coils per full rotation) -const static uint16_t enc_resolution = ENC_RESOLUTION; -const static uint8_t enc_reverse = 0; // 1 or 0, for reverse op -const static uint16_t enc_modulo = ENC_RESOLUTION / 7; // divisor is # poles / 2 -const static uint16_t enc_offset = 0; // start at midpoint - adjust -ve or +ve from -const static uint16_t enc_steplength = ENC_RESOLUTION / 7 / 6; -static uint16_t enc_reading = 0; -static uint16_t enc_adjusted = 0; -static uint16_t enc_phase = 0; -static uint16_t enc_target = 0; -// 2^14 = 16,384 - // commutation timer ISR(TCD0_OVF_vect){ // CL 6-step Commutate // get encoder reading ams5047_read(&ams5047, &enc_reading); - // offset and modulo to bring to in-phase relative location - if(enc_reverse){ - enc_adjusted = enc_resolution - enc_reading; - } else { - enc_adjusted = enc_reading; - } - enc_adjusted += enc_offset; // add offset - enc_adjusted %= enc_resolution; // wrap to resolution - enc_adjusted %= enc_modulo; // break into phase relative - // so this should represent the phase (0-5) that we're currently in - enc_phase = enc_adjusted / enc_steplength; - // if dir, phase + 2 to go forwards, else + 4, effectively - 2, and wrap past circle - (bldc.comDir) ? (enc_target = (enc_phase + 2) % 6) : (enc_target = (enc_phase + 4) % 6); - //uint8_t dout[6] = {6,0,5,254,255,enc_target}; - //uart_sendchars_buffered(&up0, dout, 6); - // now we can set PWMs accordingly - pwm_by_offset( comTable[enc_target][0] * bldc.comDuty, - comTable[enc_target][1] * bldc.comDuty, - comTable[enc_target][2] * bldc.comDuty - ); - - // OL Commutate - /* - (bldc.comDir) ? (bldc.comState ++) : (bldc.comState --); - if(bldc.comState > 5){ - bldc.comState = 0; - } + // reversal if + enc_reverse ? enc_reading = enc_resolution - enc_reading : (0); + // target phase expressed in 0 - 2PI, where 2PI in terms of an encoder counts' worth of ticks + // we add the encoder's resolution here so that we can safely have negative encoder offsets + phase_target = (enc_resolution + enc_reading + enc_offset + pi_enc) % twoPi_enc; - pwm_by_offset( comTable[bldc.comState][0] * bldc.comDuty, - comTable[bldc.comState][1] * bldc.comDuty, - comTable[bldc.comState][2] * bldc.comDuty - ); - */ -} - -// acceleration timer -ISR(TCD1_OVF_vect){ - // CL Commutate: PID Speed Loop - - // OL Commutate: Acceleration to Speed - /* - if(bldc.currentSpeed != bldc.targetSpeed){ - if(bldc.currentSpeed < bldc.targetSpeed){ - bldc.currentSpeed ++; - bldc_setSpeed(&bldc, bldc.currentSpeed); - } else { - bldc.currentSpeed --; - bldc_setSpeed(&bldc, bldc.currentSpeed); - } - } - */ + // we need to compute the respective pwm positions given the phase target + pwm_by_sin_duty(phase_target, 0.04); } ISR(USARTE1_RXC_vect){ diff --git a/embedded/atkbldcdriver/atkbldcdriver/sinelut.h b/embedded/atkbldcdriver/atkbldcdriver/sinelut.h new file mode 100644 index 0000000000000000000000000000000000000000..cb5f273c98a3d8f867fb368f1a6addb131e9d928 --- /dev/null +++ b/embedded/atkbldcdriver/atkbldcdriver/sinelut.h @@ -0,0 +1,172 @@ +/* + * sinelut.h + * + * Created: 7/30/2018 5:47:35 PM + * Author: Jake + */ + + +#ifndef SINELUT_H_ +#define SINELUT_H_ + +const static int16_t sinelut[2340] = { + 512,513,515,516,517,519,520,522,523,524,526,527,528,530,531,533, + 534,535,537,538,539,541,542,544,545,546,548,549,550,552,553,555, + 556,557,559,560,561,563,564,566,567,568,570,571,572,574,575,576, + 578,579,581,582,583,585,586,587,589,590,591,593,594,595,597,598, + 600,601,602,604,605,606,608,609,610,612,613,614,616,617,618,620, + 621,622,624,625,627,628,629,631,632,633,635,636,637,639,640,641, + 643,644,645,647,648,649,650,652,653,654,656,657,658,660,661,662, + 664,665,666,668,669,670,672,673,674,675,677,678,679,681,682,683, + 685,686,687,688,690,691,692,694,695,696,697,699,700,701,703,704, + 705,706,708,709,710,711,713,714,715,716,718,719,720,722,723,724, + 725,727,728,729,730,731,733,734,735,736,738,739,740,741,743,744, + 745,746,748,749,750,751,752,754,755,756,757,758,760,761,762,763, + 764,766,767,768,769,770,772,773,774,775,776,777,779,780,781,782, + 783,784,786,787,788,789,790,791,793,794,795,796,797,798,799,801, + 802,803,804,805,806,807,808,810,811,812,813,814,815,816,817,818, + 820,821,822,823,824,825,826,827,828,829,830,832,833,834,835,836, + 837,838,839,840,841,842,843,844,845,846,847,848,849,850,852,853, + 854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869, + 870,871,872,873,874,875,875,876,877,878,879,880,881,882,883,884, + 885,886,887,888,889,890,891,892,892,893,894,895,896,897,898,899, + 900,901,902,902,903,904,905,906,907,908,909,909,910,911,912,913, + 914,915,915,916,917,918,919,920,920,921,922,923,924,925,925,926, + 927,928,929,929,930,931,932,933,933,934,935,936,936,937,938,939, + 940,940,941,942,943,943,944,945,945,946,947,948,948,949,950,951, + 951,952,953,953,954,955,955,956,957,957,958,959,959,960,961,961, + 962,963,963,964,965,965,966,967,967,968,969,969,970,970,971,972, + 972,973,973,974,975,975,976,976,977,977,978,979,979,980,980,981, + 981,982,982,983,984,984,985,985,986,986,987,987,988,988,989,989, + 990,990,991,991,992,992,993,993,994,994,995,995,995,996,996,997, + 997,998,998,999,999,999,1000,1000,1001,1001,1001,1002,1002,1003,1003,1003, + 1004,1004,1005,1005,1005,1006,1006,1006,1007,1007,1007,1008,1008,1008,1009,1009, + 1009,1010,1010,1010,1011,1011,1011,1012,1012,1012,1013,1013,1013,1013,1014,1014, + 1014,1014,1015,1015,1015,1015,1016,1016,1016,1016,1017,1017,1017,1017,1018,1018, + 1018,1018,1018,1019,1019,1019,1019,1019,1020,1020,1020,1020,1020,1020,1021,1021, + 1021,1021,1021,1021,1021,1022,1022,1022,1022,1022,1022,1022,1022,1023,1023,1023, + 1023,1023,1023,1023,1023,1023,1023,1023,1023,1024,1024,1024,1024,1024,1024,1024, + 1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1024, + 1024,1024,1024,1024,1024,1024,1024,1024,1024,1024,1023,1023,1023,1023,1023,1023, + 1023,1023,1023,1023,1023,1023,1022,1022,1022,1022,1022,1022,1022,1022,1021,1021, + 1021,1021,1021,1021,1021,1020,1020,1020,1020,1020,1020,1019,1019,1019,1019,1019, + 1018,1018,1018,1018,1018,1017,1017,1017,1017,1016,1016,1016,1016,1015,1015,1015, + 1015,1014,1014,1014,1014,1013,1013,1013,1013,1012,1012,1012,1011,1011,1011,1010, + 1010,1010,1009,1009,1009,1008,1008,1008,1007,1007,1007,1006,1006,1006,1005,1005, + 1005,1004,1004,1003,1003,1003,1002,1002,1001,1001,1001,1000,1000,999,999,999, + 998,998,997,997,996,996,995,995,995,994,994,993,993,992,992,991, + 991,990,990,989,989,988,988,987,987,986,986,985,985,984,984,983, + 982,982,981,981,980,980,979,979,978,977,977,976,976,975,975,974, + 973,973,972,972,971,970,970,969,969,968,967,967,966,965,965,964, + 963,963,962,961,961,960,959,959,958,957,957,956,955,955,954,953, + 953,952,951,951,950,949,948,948,947,946,945,945,944,943,943,942, + 941,940,940,939,938,937,936,936,935,934,933,933,932,931,930,929, + 929,928,927,926,925,925,924,923,922,921,920,920,919,918,917,916, + 915,915,914,913,912,911,910,909,909,908,907,906,905,904,903,902, + 902,901,900,899,898,897,896,895,894,893,892,892,891,890,889,888, + 887,886,885,884,883,882,881,880,879,878,877,876,875,875,874,873, + 872,871,870,869,868,867,866,865,864,863,862,861,860,859,858,857, + 856,855,854,853,852,850,849,848,847,846,845,844,843,842,841,840, + 839,838,837,836,835,834,833,832,830,829,828,827,826,825,824,823, + 822,821,820,818,817,816,815,814,813,812,811,810,808,807,806,805, + 804,803,802,801,799,798,797,796,795,794,793,791,790,789,788,787, + 786,784,783,782,781,780,779,777,776,775,774,773,772,770,769,768, + 767,766,764,763,762,761,760,758,757,756,755,754,752,751,750,749, + 748,746,745,744,743,741,740,739,738,736,735,734,733,731,730,729, + 728,727,725,724,723,722,720,719,718,716,715,714,713,711,710,709, + 708,706,705,704,703,701,700,699,697,696,695,694,692,691,690,688, + 687,686,685,683,682,681,679,678,677,675,674,673,672,670,669,668, + 666,665,664,662,661,660,658,657,656,654,653,652,650,649,648,647, + 645,644,643,641,640,639,637,636,635,633,632,631,629,628,627,625, + 624,622,621,620,618,617,616,614,613,612,610,609,608,606,605,604, + 602,601,600,598,597,595,594,593,591,590,589,587,586,585,583,582, + 581,579,578,576,575,574,572,571,570,568,567,566,564,563,561,560, + 559,557,556,555,553,552,550,549,548,546,545,544,542,541,539,538, + 537,535,534,533,531,530,528,527,526,524,523,522,520,519,517,516, + 515,513,512,511,509,508,507,505,504,502,501,500,498,497,496,494, + 493,491,490,489,487,486,485,483,482,480,479,478,476,475,474,472, + 471,469,468,467,465,464,463,461,460,458,457,456,454,453,452,450, + 449,448,446,445,443,442,441,439,438,437,435,434,433,431,430,429, + 427,426,424,423,422,420,419,418,416,415,414,412,411,410,408,407, + 406,404,403,402,400,399,397,396,395,393,392,391,389,388,387,385, + 384,383,381,380,379,377,376,375,374,372,371,370,368,367,366,364, + 363,362,360,359,358,356,355,354,352,351,350,349,347,346,345,343, + 342,341,339,338,337,336,334,333,332,330,329,328,327,325,324,323, + 321,320,319,318,316,315,314,313,311,310,309,308,306,305,304,302, + 301,300,299,297,296,295,294,293,291,290,289,288,286,285,284,283, + 281,280,279,278,276,275,274,273,272,270,269,268,267,266,264,263, + 262,261,260,258,257,256,255,254,252,251,250,249,248,247,245,244, + 243,242,241,240,238,237,236,235,234,233,231,230,229,228,227,226, + 225,223,222,221,220,219,218,217,216,214,213,212,211,210,209,208, + 207,206,204,203,202,201,200,199,198,197,196,195,194,192,191,190, + 189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174, + 172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157, + 156,155,154,153,152,151,150,149,149,148,147,146,145,144,143,142, + 141,140,139,138,137,136,135,134,133,132,132,131,130,129,128,127, + 126,125,124,123,122,122,121,120,119,118,117,116,115,115,114,113, + 112,111,110,109,109,108,107,106,105,104,104,103,102,101,100,99, + 99,98,97,96,95,95,94,93,92,91,91,90,89,88,88,87, + 86,85,84,84,83,82,81,81,80,79,79,78,77,76,76,75, + 74,73,73,72,71,71,70,69,69,68,67,67,66,65,65,64, + 63,63,62,61,61,60,59,59,58,57,57,56,55,55,54,54, + 53,52,52,51,51,50,49,49,48,48,47,47,46,45,45,44, + 44,43,43,42,42,41,40,40,39,39,38,38,37,37,36,36, + 35,35,34,34,33,33,32,32,31,31,30,30,29,29,29,28, + 28,27,27,26,26,25,25,25,24,24,23,23,23,22,22,21, + 21,21,20,20,19,19,19,18,18,18,17,17,17,16,16,16, + 15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,11, + 10,10,10,10,9,9,9,9,8,8,8,8,7,7,7,7, + 6,6,6,6,6,5,5,5,5,5,4,4,4,4,4,4, + 3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,1, + 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1, + 1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2, + 3,3,3,3,3,3,3,4,4,4,4,4,4,5,5,5, + 5,5,6,6,6,6,6,7,7,7,7,8,8,8,8,9, + 9,9,9,10,10,10,10,11,11,11,11,12,12,12,13,13, + 13,14,14,14,15,15,15,16,16,16,17,17,17,18,18,18, + 19,19,19,20,20,21,21,21,22,22,23,23,23,24,24,25, + 25,25,26,26,27,27,28,28,29,29,29,30,30,31,31,32, + 32,33,33,34,34,35,35,36,36,37,37,38,38,39,39,40, + 40,41,42,42,43,43,44,44,45,45,46,47,47,48,48,49, + 49,50,51,51,52,52,53,54,54,55,55,56,57,57,58,59, + 59,60,61,61,62,63,63,64,65,65,66,67,67,68,69,69, + 70,71,71,72,73,73,74,75,76,76,77,78,79,79,80,81, + 81,82,83,84,84,85,86,87,88,88,89,90,91,91,92,93, + 94,95,95,96,97,98,99,99,100,101,102,103,104,104,105,106, + 107,108,109,109,110,111,112,113,114,115,115,116,117,118,119,120, + 121,122,122,123,124,125,126,127,128,129,130,131,132,132,133,134, + 135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,149, + 150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165, + 166,167,168,169,170,171,172,174,175,176,177,178,179,180,181,182, + 183,184,185,186,187,188,189,190,191,192,194,195,196,197,198,199, + 200,201,202,203,204,206,207,208,209,210,211,212,213,214,216,217, + 218,219,220,221,222,223,225,226,227,228,229,230,231,233,234,235, + 236,237,238,240,241,242,243,244,245,247,248,249,250,251,252,254, + 255,256,257,258,260,261,262,263,264,266,267,268,269,270,272,273, + 274,275,276,278,279,280,281,283,284,285,286,288,289,290,291,293, + 294,295,296,297,299,300,301,302,304,305,306,308,309,310,311,313, + 314,315,316,318,319,320,321,323,324,325,327,328,329,330,332,333, + 334,336,337,338,339,341,342,343,345,346,347,349,350,351,352,354, + 355,356,358,359,360,362,363,364,366,367,368,370,371,372,374,375, + 376,377,379,380,381,383,384,385,387,388,389,391,392,393,395,396, + 397,399,400,402,403,404,406,407,408,410,411,412,414,415,416,418, + 419,420,422,423,424,426,427,429,430,431,433,434,435,437,438,439, + 441,442,443,445,446,448,449,450,452,453,454,456,457,458,460,461, + 463,464,465,467,468,469,471,472,474,475,476,478,479,480,482,483, + 485,486,487,489,490,491,493,494,496,497,498,500,501,502,504,505, + 507,508,509,511 +}; + + +const static int8_t comTable[6][3] = { + {1,-1,0}, + {1,0,-1}, + {0,1,-1}, + {-1,1,0}, + {-1,0,1}, + {0,-1,1} +}; + +#endif /* SINELUT_H_ */ \ No newline at end of file