1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M5NanoC6のPWMを計算して遊ぶ(0°...180°から500us...2400us)(サーボ)

Last updated at Posted at 2024-10-10

x 業務連絡 マルチメディア飲み会あるよ(絶滅メディア博物館) なんのこと?

SG-90

o_coq464.jpg

● 0°から180°に変化する間隔の時間

2,400us-500us = 1,900us

● 1秒の波長

1000,000us / 50Hz = 20,000us

● 1カウント当たりの波長の幅

20,000us / 4096カウント = 4.8828125us

1024倍掛けた値

(20,000us * 1024 ) / 4096 = 5000

5000を1024で割ると4.8828125usになる

● 開始のカウントを求める

(500 * 1024) / 5000 = 102.4カウント

普通に計算した場合 500/4.8828125=102.4

● 終了のカウントを求める

(2400 * 1024) / 5000 = 491.52カウント

普通に計算した場合 2400/4.8828125=491.52

● 180に割り振る

だいたい20000usの内の2400usだから約1/10
4096だから1/10で約400ぐらい

491.52カウント - 102.4カウント = 389.12カウント

(389.12 * 1024) / (180 * 1024) = 2.161777777777778

398,458.88 / 184,320 = 2.161777777777778

398,458.88 / 180 = 2,213.660444444444

例 90°の時は、

(90 * 2,213.660444444444) / 1024 = 194.56

194.56でだいたいあっている

開始を足す

102.4カウント + 194.56カウント = 296.96カウント

_____ オンライコンパイラで試す _____

プログラム



#define V_0    500
#define V_180 2400
#define Width_0_180   (V_180-V_0)
#define Wavelength_1count (((1000000/50)*1024)/4096)
#define T_MIN ((V_0*1024)/Wavelength_1count)
#define T_MAX ((V_180*1024)/Wavelength_1count)
#define HH  ((T_MAX-T_MIN)*1024)/180
#define SO(AA) (T_MIN+(((AA)*(HH))>>10))

#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int a;
    for(int i=0;i<(180+1);i++){
        a = SO(i);
        //a = SO(180-i);
        printf("[%d]<%d>\n",i,a);
    }//for
}//main






[0]<102>
[1]<104>
[2]<106>
[3]<108>
[4]<110>
[5]<112>
[6]<114>
[7]<117>
[8]<119>
[9]<121>
[10]<123>
[11]<125>
[12]<127>
[13]<130>
[14]<132>
[15]<134>
[16]<136>
[17]<138>
[18]<140>
[19]<143>
[20]<145>
[21]<147>
[22]<149>
[23]<151>
[24]<153>
[25]<156>
[26]<158>
[27]<160>
[28]<162>
[29]<164>
[30]<166>
[31]<168>
[32]<171>
[33]<173>
[34]<175>
[35]<177>
[36]<179>
[37]<181>
[38]<184>
[39]<186>
[40]<188>
[41]<190>
[42]<192>
[43]<194>
[44]<197>
[45]<199>
[46]<201>
[47]<203>
[48]<205>
[49]<207>
[50]<210>
[51]<212>
[52]<214>
[53]<216>
[54]<218>
[55]<220>
[56]<222>
[57]<225>
[58]<227>
[59]<229>
[60]<231>
[61]<233>
[62]<235>
[63]<238>
[64]<240>
[65]<242>
[66]<244>
[67]<246>
[68]<248>
[69]<251>
[70]<253>
[71]<255>
[72]<257>
[73]<259>
[74]<261>
[75]<264>
[76]<266>
[77]<268>
[78]<270>
[79]<272>
[80]<274>
[81]<276>
[82]<279>
[83]<281>
[84]<283>
[85]<285>
[86]<287>
[87]<289>
[88]<292>
[89]<294>
[90]<296>
[91]<298>
[92]<300>
[93]<302>
[94]<305>
[95]<307>
[96]<309>
[97]<311>
[98]<313>
[99]<315>
[100]<318>
[101]<320>
[102]<322>
[103]<324>
[104]<326>
[105]<328>
[106]<330>
[107]<333>
[108]<335>
[109]<337>
[110]<339>
[111]<341>
[112]<343>
[113]<346>
[114]<348>
[115]<350>
[116]<352>
[117]<354>
[118]<356>
[119]<359>
[120]<361>
[121]<363>
[122]<365>
[123]<367>
[124]<369>
[125]<372>
[126]<374>
[127]<376>
[128]<378>
[129]<380>
[130]<382>
[131]<384>
[132]<387>
[133]<389>
[134]<391>
[135]<393>
[136]<395>
[137]<397>
[138]<400>
[139]<402>
[140]<404>
[141]<406>
[142]<408>
[143]<410>
[144]<413>
[145]<415>
[146]<417>
[147]<419>
[148]<421>
[149]<423>
[150]<426>
[151]<428>
[152]<430>
[153]<432>
[154]<434>
[155]<436>
[156]<438>
[157]<441>
[158]<443>
[159]<445>
[160]<447>
[161]<449>
[162]<451>
[163]<454>
[164]<456>
[165]<458>
[166]<460>
[167]<462>
[168]<464>
[169]<467>
[170]<469>
[171]<471>
[172]<473>
[173]<475>
[174]<477>
[175]<480>
[176]<482>
[177]<484>
[178]<486>
[179]<488>
[180]<490>


1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?