Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing continuous sinusoidal velocity profile #2

Open
tohox opened this issue Apr 30, 2021 · 21 comments
Open

Implementing continuous sinusoidal velocity profile #2

tohox opened this issue Apr 30, 2021 · 21 comments

Comments

@tohox
Copy link

tohox commented Apr 30, 2021

Hello Misan,

Thanks for sharing this bit of code. I have been having lots of fun with it!

I've adapted it a bit in order to mimic the behavior of a simple pendulum. So I've gotten rid of the cruise portion and am flipping the direction pin on every iteration of the loop. Now I would like to get rid of the last part of the "S" in your brilliant "S" curve... If you look at this scope capture I think you'll understand what I mean.

image

So I'd like for the velocity command curve to be as close as possible to a true sinusoid but I'm not sure where in your Solve subroutine the returned value gets flipped around to generate the end of the S curve. Or perhaps this S shape is directly mapped in your lookup table?

Again many thanks and warm regards,

@misan
Copy link
Owner

misan commented Apr 30, 2021

Hi Tobie,

If you want a sinusoidal speed pattern, I guess you'd need to change the lookup table on line7:

const double espTime[] = {0,0.8339606223319053,0.9946382076625534,1.102915144710818,1.187087301995585,1.256985726887124,1.317307868080828,1.370698791711928,1.418808837054632,1.462743256670634,1.503281772696069,1.540996854329344,1.576322314637914,1.609595454999827,1.641084186801404,1.671005164425508,1.699536302697544,1.726825656288495,1.752997867901108,1.778158947543807,1.802399879006118,1.825799384967213,1.848426077328789,1.870340150906883,1.891594732895806,1.912236969354632,1.932308908333848,1.95184822399071,1.970888815099815,1.989461303414408,2.00759345148227,2.025310515162264,2.042635542806899,2.059589630581153,2.076192141472401,2.092460894062744,2.108412325976637,2.124061636005198,2.139422908185944,2.154509220540232,2.169332740707453,2.183904810340888,2.198236019825962,2.212336274633107,2.226214854413378,2.239880465776415,2.253341289550687,2.266605023209554,2.27967891904939,2.292569818624179,2.305284183872161,2.317828125311753,2.330207427634383,2.342427572979778,2.354493762143082,2.366410933932288,2.378183782867831,2.38981677539324,2.401314164745938,2.412680004619967,2.42391816173752,2.435032327433079,2.446026028342547,2.456902636279785,2.46766537737418,2.478317340535173,2.488861485302881,2.499300649137883,2.50963755419804,2.519874813645361,2.530014937521857,2.540060338229548,2.550013335646438,2.559876161907387,2.569650965876065,2.579339817331856,2.588944710893423,2.598467569698725,2.607910248859573,2.617274538707227,2.626562167844158,2.635774806015822,2.644914066815134,2.653981510231324,2.662978645053863,2.67190693114134,2.680767781564383,2.689562564630954,2.698292605801794,2.706959189503118,2.715563560843195,2.724106927238868,2.732590459957745,2.741015295581206,2.749382537393208,2.757693256699295,2.765948494080075,2.77414926058307,2.782296538856524,2.790391284228613,2.798434425735177,2.806426867098918,2.814369487662829,2.822263143280413,2.830108667165079,2.837906870700988,2.845658544217408,2.853364457728596,2.861025361641016,2.868641987429614,2.876215048284824,2.883745239731774,2.891233240223137,2.898679711707013,2.906085300171047,2.913450636164008,2.920776335295964,2.928062998718062,2.935311213582958,2.942521553486817,2.949694578893753,2.956830837543572,2.963930864843601,2.970995184245328,2.978024307606593,2.985018735539985,2.991978957748032,2.998905453345893,3.005798691171968,3.012659130087113,3.019487219262841,3.026283398459094,3.033048098291963,3.03978174049184,3.046484738152402,3.053157495970818,3.059800410479529,3.066413870270005,3.072998256208758,3.079553941645978,3.086081292617061,3.092580668037357,3.099052419890367,3.105496893409685,3.111914427254947,3.118305353681976,3.124669998707404,3.131008682267955,3.13732171837461,3.143609415261857,3.149872075532194,3.15610999629609,3.162323469307559,3.168512781095532,3.174678213091139,3.180820041751135,3.186938538677517,3.193033970733538,3.199106600156233,3.205156684665558,3.211184477570318,3.217190227870932,3.223174180359202,3.229136575715162,3.235077650601133,3.240997637753043,3.246896766069155,3.252775260696249,3.258633343113388,3.264471231213308,3.270289139381524,3.276087278573278,3.281865856388305,3.287625077143592,3.293365141944116,3.299086248751678,3.304788592451873,3.310472364919263,3.3161377550808,3.32178494897759,3.327414129824983,3.333025478071137,3.338619171454006,3.344195385056884,3.349754291362483,3.355296060305648,3.360820859324712,3.36632885341155,3.371820205160353,3.377295074815208,3.382753620316452,3.388195997345893,3.393622359370918,3.399032857687488,3.404427641462107,3.409806857772754,3.415170651648831,3.420519166110141,3.425852542204926,3.431170919047012,3.43647443385206,3.441763221972961,3.447037416934406,3.452297150466636,3.457542552538411,3.462773751389224,3.467990873560752,3.473194043927605,3.47838338572736,3.483559020589921,3.488721068566209,3.493869648156197,3.499004876336353,3.504126868586416,3.50923573891563,3.514331599888362,3.519414562649181,3.52448473694736,3.529542231160885,3.534587152319898,3.539619606129671,3.544639696993061,3.549647528032501,3.554643201111511,3.559626816855761,3.564598474673688,3.569558272776673,3.574506308198803,3.579442676816222,3.584367473366081,3.58928079146508,3.594182723627656,3.599073361283782,3.603952794796403,3.608821113478527,3.613678405609958,3.618524758453708,3.623360258272071,3.628184990342361,3.632999038972383,3.637802487515539,3.642595418385683,3.647377913071667,3.65215005215159,3.656911915306794,3.661663581335582,3.666405128166646,3.671136632872278,3.675858171681307,3.680569819991768,3.685271652383384,3.689963742629748,3.69464616371034,3.699318987822251,3.703982286391761,3.708636130085629,3.713280588822229,3.717915731782454,3.722541627420422,3.727158343473997,3.731765946975091,3.736364504259824,3.740954080978441,3.745534742105114,3.750106551947509,3.754669574156225,3.75922387173404,3.763769507045001,3.768306541823357,3.772835037182325,3.777355053622703,3.781866651041351,3.786369888739483,3.790864825430871,3.795351519249846,3.799830027759214,3.804300407958008,3.808762716289098,3.813217008646704,3.81766334038375,3.822101766319115,3.826532340744743,3.830955117432648,3.835370149641792,3.839777490124866,3.844177191134913,3.848569304431905,3.852953881289167,3.857330972499697,3.861700628382403,3.866062898788224,3.870417833106151,3.874765480269151,3.879105888760007,3.883439106617042,3.887765181439764,3.892084160394431,3.896396090219502,3.900701017231012,3.904998987327876,3.909290045997091,3.913574238318856,3.917851608971633,3.922122202237089,3.926386062005006,3.930643231778085,3.934893754676688,3.939137673443493,3.943375030448093,3.947605867691526,3.951830226810713,3.956048149082851,3.960259675429732,3.964464846421989,3.968663702283297,3.972856282894483,3.977042627797598,3.981222776199917,3.985396766977893,3.989564638681014,3.993726429535656,3.997882177448842,4.002031920011953,4.006175694504393,4.010313537897196,4.014445486856572,4.018571577747411,4.022691846636747,4.026806329297125,4.030915061210006,4.035018077569011,4.039115413283217,4.043207102980354,4.047293181009963,4.051373681446535,4.055448638092552,4.05951808448155,4.063582053881091,4.067640579295711,4.071693693469828,4.0757414288906,4.079783817790759,4.083820892151397,4.087852683704689,4.091879223936644,4.095900544089731,4.099916675165548,4.103927647927401,4.107933492902879,4.111934240386377,4.115929920441581,4.119920562903953,4.123906197383132,4.127886853265342,4.131862559715754,4.135833345680804,4.139799239890512,4.143760270860746,4.147716466895441,4.151667856088835,4.155614466327628,4.159556325293149,4.163493460463477,4.167425899115519,4.171353668327095,4.175276794978983,4.179195305756916,4.183109227153575,4.187018585470571,4.190923406820351,4.194823717128134,4.198719542133793,4.202610907393712,4.206497838282641,4.210380359995488,4.214258497549138,4.21813227578421,4.222001719366817,4.225866852790269,4.229727700376802,4.233584286279259,4.237436634482732,4.241284768806224,4.245128712904274,4.24896849026854,4.252804124229388,4.256635637957459,4.260463054465203,4.264286396608424,4.268105687087742,4.271920948450131,4.275732203090358,4.279539473252434,4.283342781031061,4.287142148373024,4.290937597078629,4.294729148803041,4.29851682505769,4.302300647211584,4.306080636492672,4.309856813989134,4.313629200650713,4.317397817289967,4.321162684583575,4.324923823073562,4.328681253168565,4.332434995145034,4.336185069148473,4.33993149519461,4.343674293170606,4.347413482836207,4.351149083824905,4.354881115645093,4.358609597681173,4.362334549194698,4.366055989325457,4.369773937092569,4.373488411395581,4.377199431015505,4.380907014615897,4.384611180743887,4.388311947831204,4.392009334195231,4.395703358039956,4.399394037457014,4.403081390426649,4.406765434818698,4.410446188393555,4.414123668803116,4.417797893591716,4.421468880197078,4.425136645951232,4.428801208081393,4.432462583710907,4.436120789860114,4.439775843447242,4.443427761289262,4.44707656010277,4.450722256504818,4.454364867013781,4.45800440805017,4.461640895937467,4.465274346902937,4.46890477707844,4.47253220250122,4.476156639114709,4.479778102769297,4.483396609223099,4.487012174142737,4.490624813104086,4.494234541593017,4.497841375006156,4.501445328651596,4.505046417749629,4.508644657433468,4.512240062749949,4.515832648660244,4.519422430040545,4.523009421682741,4.526593638295134,4.530175094503071,4.533753804849638,4.537329783796309,4.540903045723598,4.544473604931697,4.548041475641137,4.551606671993396,4.555169208051534,4.558729097800826,4.562286355149337,4.565840993928581,4.569393027894074,4.572942470725946,4.576489336029549,4.580033637336004,4.583575388102797,4.587114601714346,4.590651291482575,4.594185470647454,4.597717152377558,4.601246349770627,4.6047730758541,4.608297343585642,4.611819165853698,4.615338555477987,4.61885552521006,4.622370087733781,4.625882255665863,4.629392041556359,4.632899457889171,4.63640451708254,4.639907231489524,4.643407613398514,4.646905675033696,4.65040142855552,4.653894886061187,4.657386059585106,4.660874961099358,4.664361602514155,4.6678459956783,4.671328152379623,4.674808084345431,4.678285803242943,4.681761320679755,4.685234648204212,4.688705797305895,4.692174779416021,4.695641605907849,4.699106288097124,4.702568837242452,4.706029264545744,4.709487581152594,4.71294379815269,4.716397926580208,4.7198499774142,4.723299961578989,4.726747889944537,4.730193773326861,4.733637622488363,4.737079448138237,4.740519260932831,4.743957071476018,4.747392890319539,4.750826727963386,4.754258594856146,4.757688501395367,4.761116457927876,4.76454247475017,4.767966562108723,4.771388730200339,4.774808989172499,4.778227349123667,4.781643820103658,4.785058412113919,4.788471135107907,4.79188199899136,4.79529101362265,4.798698188813083,4.802103534327213,4.805507059883149,4.808908775152874,4.812308689762542,4.815706813292757,4.819103155278916,4.822497725211463,4.825890532536215,4.829281586654615,4.832670896924065,4.836058472658157,4.839444323127016,4.842828457557516,4.84621088513362,4.849591614996596,4.852970656245332,4.856348017936585,4.859723709085256,4.863097738664648,4.866470115606738,4.869840848802417,4.873209947101777,4.876577419314341,4.879943274209329,4.883307520515897,4.886670166923406,4.890031222081642,4.89339069460108,4.896748593053113,4.90010492597031,4.90345970184662,4.906812929137633,4.910164616260823,4.913514771595737,4.916863403484273,4.920210520230873,4.923556130102753,4.92690024133014,4.930242862106478,4.933584000588656,4.936923664897217,4.940261863116583,4.943598603295248,4.946933893446026,4.950267741546208,4.953600155537822,4.956931143327798,4.960260712788205,4.963588871756419,4.966915628035337,4.970240989393601,4.973564963565743,4.976887558252425,4.980208781120613,4.983528639803757,4.986847141902008,4.990164294982379,4.993480106578946,4.996794584193037,5.00010773529339,5.003419567316366,5.006730087666106,5.010039303714718,5.01334722280245,5.016653852237862,5.019959199298014,5.023263271228622,5.026566075244228,5.029867618528371,5.033167908233774,5.03646695148248,5.03976475536602,5.043061326945603,5.046356673252244,5.049650801286943,5.052943718020836,5.056235430395357,5.059525945322383,5.062815269684409,5.066103410334675,5.069390374097337,5.072676167767605,5.075960798111893,5.079244271867983,5.082526595745149,5.0858077764243,5.089087820558159,5.092366734771356,5.095644525660605,5.098921199794825,5.102196763715286,5.105471223935754,5.1087445869426,5.112016859194959,5.115288047124867,5.118558157137368,5.121827195610663,5.125095168896245,5.128362083319011,5.131627945177386,5.13489276074349,5.138156536263199,5.14141927795633,5.144680992016725,5.1479416846124,5.151201361885616,5.154460029953079,5.157717694905981,5.160974362810162,5.164230039706205,5.167484731609568,5.170738444510683,5.173991184375093,5.177242957143517,5.180493768732015,5.183743625032068,5.186992531910693,5.190240495210545,5.193487520750029,5.19673361432343,5.199978781700966,5.203223028628933,5.206466360829798,5.209708784002292,5.212950303821525,5.216190925939071,5.219430655983096,5.222669499558402,5.225907462246581,5.229144549606089,5.232380767172339,5.235616120457784,5.23885061495204,5.242084256121954,5.245317049411713,5.24854900024292,5.251780114014692,5.255010396103752,5.258239851864509,5.261468486629159,5.264696305707748,5.267923314388284,5.271149517936817,5.274374921597493,5.27759953059269,5.280823350123055,5.284046385367607,5.287268641483811,5.290490123607669,5.293710836853782,5.29693078631544,5.3001499770647,5.30336841415246,5.306586102608527,5.30980304744172,5.313019253639903,5.316234726170098,5.319449469978529,5.322663489990721,5.325876791111547,5.329089378225316,5.332301256195817,5.335512429866447,5.338722904060208,5.341932683579818,5.345141773207764,5.348350177706382,5.351557901817907,5.354764950264546,5.35797132774854,5.361177038952228,5.364382088538117,5.367586481148934,5.370790221407691,5.373993313917738,5.377195763262865,5.380397574007294,5.383598750695791,5.386799297853695,5.389999219987004,5.393198521582406,5.396397207107347,5.399595281010076,5.402792747719722,5.405989611646338,5.40918587718094,5.412381548695581,5.415576630543391,5.418771127058636,5.42196504255677,5.425158381334483,5.428351147669741,5.43154334582186,5.434734980031538,5.437926054520904,5.441116573493561,5.444306541134649,5.447495961610898,5.450684839070627,5.453873177643843,5.457060981442274,5.460248254559376,5.463435001070436,5.466621225032559,5.469806930484763,5.472992121447966,5.47617680192508,5.479360975901016,5.482544647342746,5.485727820199318,5.488910498401932,5.492092685863943,5.495274386480919,5.498455604130686,5.501636342673329,5.504816605951286,5.507996397789333,5.511175721994636,5.514354582356798,5.517532982647882,5.520710926622451,5.523888418017602,5.527065460552986,5.530242057930857,5.533418213836107,5.53659393193627,5.539769215881599,5.542944069305047,5.546118495822328,5.549292499031956,5.552466082515227,5.555639249836302,5.558812004542212,5.561984350162869,5.565156290211136,5.568327828182815,5.571498967556677,5.574669711794522,5.577840064341158,5.581010028624473,5.584179608055418,5.587348806028049,5.590517625919568,5.593686071090314,5.596854144883817,5.600021850626784,5.603189191629161,5.606356171184124,5.609522792568122,5.612689059040893,5.61585497384545,5.619020540208163,5.622185761338738,5.625350640430224,5.628515180659088,5.631679385185163,5.63484325715172,5.638006799685456,5.641170015896522,5.644332908878546,5.647495481708633,5.65065773744738,5.653819679138938,5.656981309810938,5.660142632474596,5.663303650124678,5.666464365739516,5.66962478228104,5.672784902694775,5.675944729909862,5.679104266839077,5.682263516378817,5.68542248140915,5.688581164793775,5.691739569380097,5.694897697999185,5.69805555346579,5.701213138578387,5.704370456119156,5.707527508853991,5.710684299532525,5.713840830888117,5.71699710563788,5.720153126482673,5.723308896107131,5.726464417179638,5.729619692352356,5.732774724261235,5.735929515526013,5.739084068750199,5.742238386521128,5.745392471409911,5.748546325971471,5.751699952744555,5.754853354251707,5.758006532999302,5.761159491477528,5.764312232160394,5.767464757505751,5.770617069955255,5.773769171934411,5.776921065852552,5.780072754102829,5.783224239062254,5.786375523091658,5.789526608535697,5.792677497722893,5.795828192965576,5.798978696559917,5.802129010785928,5.805279137907451,5.80842908017214,5.81157883981152,5.814728419040902,5.817877820059442,5.821027045050112,5.824176096179714,5.827324975598857,5.830473685441956,5.833622227827246,5.836770604856765,5.83991881861636,5.843066871175646,5.846214764588072,5.849362500890836,5.852510082104922,5.855657510235111,5.858804787269936,5.861951915181693,5.865098895926441,5.868245731443982,5.87139242365787,5.874538974475388,5.877685385787548,5.88083165946907,5.883977797378417,5.887123801357728,5.89026967323285,5.893415414813313,5.896561027892323,5.899706514246775,5.902851875637189,5.905997113807761,5.909142230486314,5.912287227384303,5.915432106196802,5.918576868602509,5.921721516263697,5.924866050826245,5.92801047391958,5.931154787156721,5.934298992134234,5.937443090432206,5.94058708361429,5.943730973227608,5.946874760802822,5.950018447854067,5.953162035878962,5.956305526358589,5.959448920757477,5.962592220523607,5.965735427088355,5.968878541866541,5.972021566256356,5.975164501639382,5.978307349380564,5.981450110828207,5.984592787313937,5.987735380152718,5.990877890642813,5.994020320065778,5.99716266968644,6.000304940752892,6.003447134496477,6.00658925213174,6.009731294856471,6.012873263851626,6.016015160281357,6.019156985292956,6.022298740016895,6.025440425566743,6.028582043039191,6.031723593514012,6.034865078054068,6.038006497705272,6.041147853496574,6.044289146439948,6.047430377530368,6.050571547745798,6.053712658047162,6.056853709378328,6.059994702666109,6.063135638820219,6.06627651873326,6.069417343280696,6.072558113320873,6.075698829694951,6.078839493226909,6.081980104723504,6.08512066497429,6.088261174751569,6.091401634810373,6.094542045888455,6.097682408706262,6.100822723966902,6.103962992356169,6.107103214542454,6.110243391176779,6.113383522892764,6.11652361030659,6.119663654017001,6.122803654605248,6.125943612635123,6.129083528652884,6.132223403187254,6.135363236749424,6.13850302983299,6.141642782913957,6.144782496450708,6.147922170883998,6.15106180663691,6.15420140411484,6.157340963705503,6.160480485778852,6.163619970687129,6.166759418764782,6.169898830328467,6.173038205677051,6.176177545091535,6.179316848835084,6.182456117152973,6.185595350272576,6.188734548403353,6.191873711736797,6.195012840446461,6.198151934687887,6.201290994598615,6.204430020298147,6.20756901188793,6.21070796945132,6.213846893053593,6.216985782741897,6.22012463854522,6.22326346047439,6.22640224852204,6.229541002662611,6.232679722852281,6.235818409029003,6.238957061112409,6.24209567900387,6.245234262586409,6.248372811724709,6.251511326265097,6.254649806035491,6.257788250845413,6.260926660485939,6.264065034729699,6.267203373330832,6.270341676024991,6.273479942529303,6.276618172542349,6.279756365744141,6.28289452179612};

I wrote about this code in my blog: https://fightpc.blogspot.com/2018/04/how-to-get-sinusoidal-s-curve-for.html

Maybe what you need is to regenerate the values so it comes back at the end to the same value as at the beginning.

@tohox
Copy link
Author

tohox commented May 1, 2021

Hi Miguel,

Thanks for the reply.

I plotted your current LUT into Excel to see what it looked like:
image

My first attempt based on a sine curve looked like this (2 pi sin(pi/1000 t):
image

This LUT resulted in this rather interesting but unexpected actual velocity profile:
image

That didn't work out like I expected because I believe I misunderstood the purpose of your table...

I generated the velocity curve of the end result I wished to achieve but I think your table actually describes the positionnal displacement between time increments. Correct? So I guess I would need to generate the integral of my sine curve in order to generate a LUT which describes the displacement between time increments?

However, and I'm not sure if this was a fluke, but the velocity profile that resulted from my sine table almost appears to be that of a cosecant which I believe is the reciprocal of a sine curve. So I'm not sure if I should be trying to integrate or instead reciprocate my first attempt ...

Any further thoughts are welcome,

@misan
Copy link
Owner

misan commented May 1, 2021 via email

@misan
Copy link
Owner

misan commented May 1, 2021 via email

@misan
Copy link
Owner

misan commented May 1, 2021

Of course, you can build a longer table that covers both acceleration and deceleration and just use a single loop to go through it all.

@misan
Copy link
Owner

misan commented May 1, 2021

image

If your velocity is going to be sin(t) [t going from 0 to PI], then the distance traveled (which is the table content) will be 1-cos(t) and the acceleration would be cos(t).

Please note that, if you chose so, there will be a sudden acceleration at the beginning and at the end of your move.

@misan
Copy link
Owner

misan commented May 1, 2021

IMHO, it will make more sense to do the velocity 1-cos(t) [t going from 0 to 2*PI] as the image below shows, acceleration will be smooth everywhere (green).
image

@tohox
Copy link
Author

tohox commented May 1, 2021

Hello,

Based on this information and what you mentioned above I came up whit this curve/table ( -A cos(wt) ). I've used a 2Pi amplitude, a Pi/1000 frequency and offset the whole curve vertically by Pi to generate the table. It appears very similar to the position curve in your last suggestion.

image

I realized that I was only switching directions once per cycle instead of twice (duh!) which was adding to the confusion but something is still obviously wrong as the motion profile I'm getting is as follows:

image

Very pretty but not what I'm looking for. Here's what I've done with your code (you can ignore the ClearCore stuff, it is the hardware platform I'm using) :

#include "ClearCore.h"
#define motor ConnectorM0
#define baudRate 9600

// -Acos(wt) with a 2Pi amplitude and Pi/1000 frequency
const double espTime[] = {0.000000,0.000016,0.000062,0.000140,0.000248,0.000388,0.000558,0.000760,0.000992,0.001256,0.001550,0.001876,0.002232,0.002620,0.003038,0.003488,0.003968,0.004479,0.005022,0.005595,0.006199,0.006834,0.007501,0.008198,0.008926,0.009684,0.010474,0.011295,0.012147,0.013029,0.013942,0.014887,0.015862,0.016868,0.017905,0.018972,0.020071,0.021200,0.022360,0.023551,0.024772,0.026025,0.027308,0.028622,0.029966,0.031342,0.032748,0.034184,0.035652,0.037150,0.038678,0.040237,0.041827,0.043448,0.045099,0.046780,0.048493,0.050235,0.052008,0.053812,0.055646,0.057511,0.059406,0.061331,0.063287,0.065273,0.067290,0.069337,0.071414,0.073522,0.075660,0.077828,0.080026,0.082255,0.084514,0.086802,0.089122,0.091471,0.093850,0.096259,0.098699,0.101168,0.103668,0.106197,0.108757,0.111346,0.113965,0.116615,0.119294,0.122002,0.124741,0.127509,0.130308,0.133136,0.135993,0.138880,0.141797,0.144744,0.147720,0.150725,0.153760,0.156825,0.159919,0.163043,0.166196,0.169378,0.172589,0.175830,0.179101,0.182400,0.185729,0.189086,0.192473,0.195889,0.199334,0.202809,0.206312,0.209844,0.213405,0.216995,0.220614,0.224261,0.227938,0.231643,0.235377,0.239140,0.242931,0.246751,0.250599,0.254476,0.258381,0.262315,0.266278,0.270268,0.274287,0.278335,0.282410,0.286514,0.290646,0.294806,0.298995,0.303211,0.307455,0.311728,0.316028,0.320356,0.324712,0.329096,0.333507,0.337946,0.342413,0.346908,0.351430,0.355979,0.360556,0.365161,0.369793,0.374452,0.379139,0.383853,0.388594,0.393362,0.398158,0.402980,0.407830,0.412706,0.417610,0.422540,0.427497,0.432481,0.437492,0.442529,0.447593,0.452684,0.457801,0.462945,0.468115,0.473311,0.478534,0.483783,0.489058,0.494360,0.499687,0.505041,0.510421,0.515826,0.521258,0.526715,0.532199,0.537708,0.543242,0.548803,0.554389,0.560000,0.565637,0.571300,0.576987,0.582700,0.588439,0.594202,0.599991,0.605805,0.611643,0.617507,0.623396,0.629309,0.635248,0.641211,0.647198,0.653211,0.659247,0.665309,0.671395,0.677505,0.683639,0.689798,0.695981,0.702188,0.708420,0.714675,0.720954,0.727257,0.733584,0.739935,0.746309,0.752707,0.759128,0.765574,0.772042,0.778534,0.785049,0.791588,0.798149,0.804734,0.811342,0.817973,0.824627,0.831304,0.838003,0.844725,0.851470,0.858238,0.865028,0.871840,0.878675,0.885532,0.892412,0.899314,0.906237,0.913183,0.920151,0.927141,0.934153,0.941186,0.948241,0.955318,0.962417,0.969536,0.976678,0.983840,0.991024,0.998230,1.005456,1.012704,1.019972,1.027261,1.034572,1.041903,1.049255,1.056627,1.064020,1.071434,1.078868,1.086322,1.093797,1.101291,1.108806,1.116341,1.123896,1.131471,1.139066,1.146681,1.154315,1.161969,1.169642,1.177335,1.185047,1.192779,1.200529,1.208299,1.216088,1.223896,1.231723,1.239569,1.247434,1.255317,1.263219,1.271139,1.279078,1.287035,1.295011,1.303005,1.311017,1.319047,1.327094,1.335160,1.343244,1.351346,1.359465,1.367601,1.375756,1.383927,1.392116,1.400323,1.408546,1.416787,1.425044,1.433319,1.441610,1.449918,1.458243,1.466585,1.474943,1.483317,1.491708,1.500115,1.508538,1.516978,1.525433,1.533905,1.542392,1.550895,1.559414,1.567948,1.576498,1.585063,1.593644,1.602240,1.610851,1.619478,1.628119,1.636775,1.645446,1.654132,1.662833,1.671548,1.680277,1.689022,1.697780,1.706553,1.715339,1.724140,1.732955,1.741784,1.750627,1.759483,1.768353,1.777237,1.786134,1.795044,1.803968,1.812904,1.821854,1.830817,1.839793,1.848782,1.857784,1.866798,1.875825,1.884864,1.893916,1.902980,1.912056,1.921144,1.930245,1.939357,1.948481,1.957617,1.966765,1.975924,1.985095,1.994277,2.003471,2.012676,2.021892,2.031119,2.040357,2.049605,2.058865,2.068135,2.077416,2.086707,2.096009,2.105321,2.114644,2.123976,2.133319,2.142671,2.152033,2.161405,2.170787,2.180178,2.189579,2.198989,2.208409,2.217838,2.227276,2.236722,2.246178,2.255643,2.265116,2.274598,2.284089,2.293588,2.303095,2.312611,2.322135,2.331667,2.341207,2.350755,2.360310,2.369874,2.379445,2.389023,2.398609,2.408202,2.417803,2.427411,2.437025,2.446647,2.456275,2.465911,2.475553,2.485201,2.494856,2.504518,2.514185,2.523859,2.533539,2.543225,2.552917,2.562615,2.572318,2.582027,2.591741,2.601461,2.611187,2.620917,2.630653,2.640394,2.650139,2.659890,2.669645,2.679405,2.689169,2.698938,2.708712,2.718489,2.728271,2.738057,2.747847,2.757640,2.767438,2.777239,2.787044,2.796852,2.806664,2.816479,2.826297,2.836118,2.845943,2.855770,2.865600,2.875433,2.885268,2.895106,2.904947,2.914789,2.924634,2.934481,2.944330,2.954182,2.964034,2.973889,2.983745,2.993603,3.003463,3.013323,3.023185,3.033049,3.042913,3.052778,3.062644,3.072511,3.082379,3.092247,3.102115,3.111984,3.121854,3.131723,3.141593,3.151462,3.161332,3.171201,3.181070,3.190939,3.200807,3.210674,3.220541,3.230407,3.240272,3.250137,3.260000,3.269862,3.279723,3.289582,3.299440,3.309296,3.319151,3.329004,3.338855,3.348704,3.358551,3.368396,3.378239,3.388079,3.397917,3.407753,3.417585,3.427415,3.437243,3.447067,3.456888,3.466706,3.476521,3.486333,3.496141,3.505946,3.515747,3.525545,3.535339,3.545128,3.554914,3.564696,3.574474,3.584247,3.594016,3.603780,3.613540,3.623296,3.633046,3.642792,3.652532,3.662268,3.671999,3.681724,3.691444,3.701158,3.710867,3.720571,3.730268,3.739960,3.749646,3.759326,3.769000,3.778668,3.788329,3.797984,3.807633,3.817275,3.826910,3.836538,3.846160,3.855775,3.865382,3.874983,3.884576,3.894162,3.903741,3.913312,3.922875,3.932431,3.941978,3.951518,3.961050,3.970574,3.980090,3.989597,3.999096,4.008587,4.018069,4.027542,4.037007,4.046463,4.055910,4.065348,4.074776,4.084196,4.093606,4.103007,4.112398,4.121780,4.131152,4.140514,4.149867,4.159209,4.168542,4.177864,4.187176,4.196478,4.205769,4.215050,4.224320,4.233580,4.242829,4.252067,4.261294,4.270510,4.279714,4.288908,4.298090,4.307261,4.316420,4.325568,4.334704,4.343828,4.352940,4.362041,4.371129,4.380206,4.389270,4.398321,4.407361,4.416387,4.425402,4.434403,4.443392,4.452368,4.461331,4.470281,4.479218,4.488141,4.497052,4.505949,4.514832,4.523702,4.532559,4.541401,4.550230,4.559045,4.567846,4.576633,4.585405,4.594164,4.602908,4.611638,4.620353,4.629053,4.637739,4.646410,4.655066,4.663708,4.672334,4.680945,4.689541,4.698122,4.706687,4.715237,4.723772,4.732290,4.740793,4.749281,4.757752,4.766208,4.774647,4.783070,4.791477,4.799868,4.808243,4.816601,4.824942,4.833267,4.841575,4.849867,4.858141,4.866399,4.874639,4.882863,4.891069,4.899258,4.907430,4.915584,4.923721,4.931840,4.939941,4.948025,4.956091,4.964139,4.972169,4.980181,4.988174,4.996150,5.004107,5.012046,5.019967,5.027868,5.035752,5.043616,5.051462,5.059289,5.067097,5.074886,5.082656,5.090407,5.098138,5.105850,5.113543,5.121217,5.128870,5.136505,5.144119,5.151714,5.159289,5.166844,5.174379,5.181894,5.189389,5.196863,5.204318,5.211752,5.219165,5.226558,5.233931,5.241282,5.248614,5.255924,5.263213,5.270482,5.277729,5.284956,5.292161,5.299345,5.306508,5.313649,5.320769,5.327867,5.334944,5.341999,5.349033,5.356044,5.363034,5.370002,5.376948,5.383872,5.390773,5.397653,5.404510,5.411345,5.418158,5.424948,5.431715,5.438460,5.445182,5.451882,5.458558,5.465212,5.471843,5.478451,5.485036,5.491598,5.498136,5.504651,5.511143,5.517612,5.524057,5.530478,5.536876,5.543251,5.549601,5.555928,5.562231,5.568511,5.574766,5.580997,5.587204,5.593387,5.599546,5.605680,5.611791,5.617876,5.623938,5.629975,5.635987,5.641975,5.647938,5.653876,5.659790,5.665678,5.671542,5.677381,5.683195,5.688983,5.694747,5.700485,5.706198,5.711886,5.717548,5.723185,5.728797,5.734383,5.739943,5.745478,5.750987,5.756470,5.761927,5.767359,5.772765,5.778144,5.783498,5.788826,5.794127,5.799402,5.804651,5.809874,5.815071,5.820241,5.825384,5.830501,5.835592,5.840656,5.845693,5.850704,5.855688,5.860645,5.865576,5.870479,5.875356,5.880205,5.885028,5.889823,5.894591,5.899332,5.904046,5.908733,5.913392,5.918024,5.922629,5.927206,5.931756,5.936278,5.940772,5.945239,5.949678,5.954090,5.958474,5.962829,5.967158,5.971458,5.975730,5.979974,5.984191,5.988379,5.992539,5.996671,6.000775,6.004850,6.008898,6.012917,6.016908,6.020870,6.024804,6.028709,6.032586,6.036435,6.040255,6.044046,6.047808,6.051542,6.055248,6.058924,6.062572,6.066190,6.069780,6.073341,6.076874,6.080377,6.083851,6.087296,6.090712,6.094099,6.097457,6.100785,6.104085,6.107355,6.110596,6.113807,6.116990,6.120143,6.123266,6.126360,6.129425,6.132460,6.135466,6.138442,6.141388,6.144305,6.147192,6.150050,6.152878,6.155676,6.158444,6.161183,6.163892,6.166571,6.169220,6.171839,6.174429,6.176988,6.179517,6.182017,6.184486,6.186926,6.189335,6.191715,6.194064,6.196383,6.198672,6.200931,6.203159,6.205357,6.207526,6.209663,6.211771,6.213848,6.215895,6.217912,6.219898,6.221854,6.223779,6.225674,6.227539,6.229373,6.231177,6.232950,6.234693,6.236405,6.238086,6.239738,6.241358,6.242948,6.244507,6.246036,6.247534,6.249001,6.250438,6.251844,6.253219,6.254564,6.255877,6.257161,6.258413,6.259635,6.260825,6.261985,6.263115,6.264213,6.265281,6.266318,6.267323,6.268299,6.269243,6.270156,6.271039,6.271890,6.272711,6.273501,6.274260,6.274988,6.275685,6.276351,6.276986,6.277590,6.278164,6.278706,6.279217,6.279698,6.280147,6.280566,6.280953,6.281310,6.281635,6.281930,6.282193,6.282426,6.282627,6.282798,6.282937,6.283046,6.283123,6.283170,6.283185};

// Pin definitions
#define STEPY M0_INB
#define DIRY  M0_INA
#define STEPS_MM 

int MAX=1000;
const float PI2=PI*PI;
float at=PI*PI/MAX; // 9.867
int i=0;
int M=1;  // number of steps at cruising speed
int N=600;  // number of accel/decel steps
float kt=0.005*10e6; //time adjust constant, accel time = 2*PI*kt = 0.628 seconds
float d=0; // distance traveled 
float t=0; // time elapsed
float last; // keep delay value for cruising speed
float tt;
bool dir = false;

// linear interpolation function
float solve(float t) { // will return the interpolated time (0..2*PI) that corresponds to a given distance (0 .. PI^2) according to espTime table
 if(t>=PI2) return espTime[MAX-1]; 
 int n=(t/at); 
 float dt=t-n*at;
 if(dt==0) return espTime[n];
 return (espTime[n+1]-espTime[n])*dt/at+espTime[n];
}

// generate step pulse
void step(int duration){
  digitalWrite(STEPY, HIGH);
  delayMicroseconds(duration);
  digitalWrite(STEPY, LOW);
}

void setup() {
    // Setup step and direction pins
    pinMode(STEPY, OUTPUT); // stepY
    pinMode(DIRY, OUTPUT); // dirY
    
    // Initialize console serial port
    Serial.begin(baudRate);
    uint32_t timeout = 5000;
    uint32_t startTime = millis();
    while (!Serial && millis() - startTime < timeout) {
        continue;
    }

    // Enable motor
    motor.EnableRequest(true);
    Serial.println("Motor Enabled");

    // Waits for HLFB to assert (waits for homing to complete if applicable)
    //Serial.println("Waiting for HLFB...");
    //while (motor.HlfbState() != MotorDriver::HLFB_ASSERTED) {
    //    continue;
    //}
    Serial.println("Motor Ready");
}

void loop() {
  Serial.println("FORTH and ...");
  for(int i=1; i<N; i++) {
    d=PI2*i/N; 
    tt=solve(d)*kt;
    last=tt-t;
    delayMicroseconds(last);
    step(2);
    t=tt;  
  } 
  
  // Change direction
  dir = !dir;
  digitalWrite(DIRY, dir);
  
  Serial.println("BACK and ...");
  for(int i=N-1; i>0; i--) {
    d=PI2*i/N; 
    tt=solve(d)*kt;
    delayMicroseconds(t-tt);
    step(2);
    t=tt;  
  }
  t=0;

  // Change direction again
  dir = !dir;
  digitalWrite(DIRY, dir);
}

@tohox
Copy link
Author

tohox commented May 2, 2021

Ok, I just understood something else. Your lookup table doesn't exactly give the distance function of time but rather the time required between pulses to achieve the same profile. So it really is the inverse of the position (which, in hindsight, is exactly what you described in your blog post).

@misan
Copy link
Owner

misan commented May 2, 2021 via email

@misan
Copy link
Owner

misan commented May 2, 2021

Here is how I create the table (that's for distance=t-sin(t)) using wxmaxima:

image

@tohox
Copy link
Author

tohox commented May 3, 2021

Hurrah!

I thought your graph above looked a lot like a sin-¹ so I based my LUT on that, rescaled and shifted it until it fit within the required bounds and it works fine now!

image

Next step is to make it dynamic so I can alter amplitude and period on the fly via OSC which shouldn't be too difficult using the N and kt variables you've already defined in your code :)

You've been very generous first with your source code and then with your time. Thanks so much for your help!

@misan
Copy link
Owner

misan commented May 3, 2021

It looks great, congratulations!

@sdydjciv
Copy link

Hello Misan, thank for sharing your source code .but i dont know to how to create the table using wxmaxima with my velocity parameters ?

@sdydjciv
Copy link

i tried in wxmaxima
e: (t-sin(t))/2*%pi;
fr(x):=find_root(e=x,t,0,2*%pi),number;
solve([for i:1 while i<=100 do display(fr(i*9.869604401089358/100))], [x]);

but in exel frequency doesn't like s curve
dasdasf

@misan
Copy link
Owner

misan commented Nov 27, 2022 via email

@misan
Copy link
Owner

misan commented Nov 27, 2022 via email

@sdydjciv
Copy link

I seem to have understood a bit and I want to ask how do you copy these data into the array in the fastest way
value

@misan
Copy link
Owner

misan commented Nov 30, 2022 via email

@tohox
Copy link
Author

tohox commented Nov 30, 2022 via email

@misan
Copy link
Owner

misan commented Nov 30, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants