磁懸浮Arduino代碼_第1頁(yè)
磁懸浮Arduino代碼_第2頁(yè)
磁懸浮Arduino代碼_第3頁(yè)
磁懸浮Arduino代碼_第4頁(yè)
磁懸浮Arduino代碼_第5頁(yè)
已閱讀5頁(yè),還剩6頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、折騰了幾天終于把這個(gè)東西搞定了。看似簡(jiǎn)單,還是有一些小技巧的。主要材料:1. Arduino UNO主控2. SS495線性霍爾采集位置信息3. L298N驅(qū)動(dòng)4. 直徑12.7mm強(qiáng)磁球5. 電磁鐵12V,線圈電阻10歐主要參數(shù):PID控制,采樣周期1ms,PWM頻率 3921Hz,懸浮距離30mm,電流300mA筷子版的,下面是一個(gè)磁體,上面是一個(gè)小鐵塊,中間是一截筷子。懸浮距離比較小,但是方便進(jìn)行建模和控制理論分析。 磁球版,懸浮距離很大,不容易進(jìn)行理論分析。 ARDUINO 代碼復(fù)制打印1. const int SS495_PIN = A0;  

2、/ Analog input pin that the SS495 is attached to2. const int PWM_PIN=3;  / Pins 3 and 11 are connected to Timer 2.3.  4. const int SampleTime=600;5. const int PWM_BIAS=128;6.  7. int Setpoint=850;8. double Kp=1, Ki=0.001, Kd=50;9.  10. void setupCoilPWM()11.   12.

3、   / Setup the timer 2 as Phase Correct PWM, 3921 Hz.13.    pinMode(3, OUTPUT);14.    / Timer 2 register: WGM20 sets PWM phase correct mode, COM2x1 sets the PWM out to channels A and B.15.    TCCR2A = 0;16.    TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV

4、(WGM20);17.    / Set the prescaler to 8, the PWM freq is 16MHz/255/2/<prescaler>18.    TCCR2B = 0;19.    TCCR2B = _BV(CS21);20. 21.  22. void writeCoilPWM(uint8_t pin, int val)23. 24.       OCR2B = val;25. 26.  27. void PID_Compute( )

5、28. 29.   static int lastError=0;30.   static unsigned long int lastTime=0;31.   static double iiterm=0;32.  33.   unsigned long now,timeChange;34.   int input,output,error,piterm,diterm;35.  36.   now = micros();37.   t

6、imeChange = (now - lastTime);38.   if(timeChange>=SampleTime)39.   40.     lastTime = now;41.  42.     input=read_input();43.  44.     error = -(Setpoint - input);45.  46.     iiterm+= (Ki * error);47.     iiterm

7、=constrain(iiterm,-255,255);48.  49.     piterm=Kp * error;50.     diterm=Kd * (error - lastError)*1000/timeChange;51.     output = PWM_BIAS+(piterm + diterm);52.     output=constrain(output,0,255);53.     writeCoilPWM(PWM_PIN, output);54.  

8、;55.     / print the results to the serial monitor:56.     /sprintf(str,"%04d,%+04d,%+04d,%+04d,%04d",error,piterm,diterm,(int)iiterm,output);57.     /Serial.println(str);   58.  59.     /*Remember some variables for next time*/60. 

9、60;   lastError = error;61.    62. 63.  64. int read_input()65. 66.   static int last=0;67.   int r;68.   r=analogRead(SS495_PIN);69.   if(abs(r-last)<4)70.     r=last;71.   else72.     last=r;73.  

10、0;return r;74. 75.  76. void setup() 77.   setupCoilPWM();78. 79.  80. void loop() 81.   / read the analog in value:82.   PID_Compute();83. 下一步準(zhǔn)備加上串口通訊,用matlab做上位機(jī)界面。已經(jīng)做好了Arduino的自平衡車和兩旋翼模型,有空也放上來(lái)。完整版,用串口向PC機(jī)發(fā)送信息:ARDUINO 代碼復(fù)制打印1. const int SS495_PIN = A0; 

11、; / Analog input pin that the SS495 is attached to2. const int PWM_PIN=3;  / Pins 3 and 11 are connected to Timer 2.3.  4. const double SampleTime=0.5;  /ms5. const int PWM_BIAS=128;6.  7. int Setpoint=850;8. double Kp=1, Ki=0.01, Kd=90;9. double piterm,diterm, iit

12、erm=0;10. int input,output,error;11. unsigned long now,timeChange;12.  13. void setupCoilPWM()14.   15.   / Setup the timer 2 as Phase Correct PWM, 3921 Hz.16.   pinMode(3, OUTPUT);17.   / Timer 2 register: WGM20 sets PWM phase correct mode, COM2x1 sets t

13、he PWM out to channels A and B.18.   TCCR2A = 0;19.   TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20);20.   / Set the prescaler to 8, the PWM freq is 16MHz/255/2/<prescaler>21.   TCCR2B = 0;22.   TCCR2B = _BV(CS21);23. 24.  25. void setupC

14、ontrolTimer()26.   27.   /set timer1 in CTC mode.28.   /"ATmega4_88_168_328_DataSheet" , section 16.9.2 Clear Timer on Compare Match (CTC) Mode29.   cli();          / disable global interrupts30.   TCCR1A = 0;&

15、#160;    / set entire TCCR1A register to 031.   TCCR1B = 0;     / same for TCCR1B32.  33.   / turn on CTC mode:34.   TCCR1B |= (1 << WGM12);35.   / Set CS12 bits for 256 prescaler:36.   TCCR1B |= (1 <&

16、lt; CS12);37.   / set compare match register to desired timer count:38.   OCR1A = (SampleTime/1000)*(16000000/256);39.   / enable timer compare interrupt:40.   TIMSK1 |= (1 << OCIE1A);41.   sei();          / enable

17、 global interrupts42. 43.  44. void writeCoilPWM(uint8_t pin, int val)45. 46.     OCR2B = val;47. 48.  49. ISR(TIMER1_COMPA_vect)50. 51.   PID_Compute();52. 53.  54. void PID_Compute( )55. 56.   static int lastError=0;57.   static unsigned long

18、int lastTime=0;58.  59.   now = micros();60.   timeChange = (now - lastTime);61.   if(timeChange>=SampleTime)62.   63.     lastTime = now;64.  65.     input=read_input();66.  67.     error = -(Setpoint - input);6

19、8.  69.     iiterm+= (Ki * error);70.     iiterm=constrain(iiterm,-255,255);71.  72.     piterm=Kp * error;73.     diterm=Kd * (error - lastError)/SampleTime;74.     output = PWM_BIAS+(piterm + diterm);75.     output=constrain(out

20、put,0,255);76.     writeCoilPWM(PWM_PIN, output);77.  78.      /*Remember some variables for next time*/79.     lastError = error;80.   81. 82.  83. int read_input()84. 85.   static int last=0;86.   int r;87.   

21、;r=analogRead(SS495_PIN);88.   if(abs(r-last)<4)89.     r=last;90.   else91.     last=r;92.   return r;93. 94.  95. void setup() 96.   setupCoilPWM();97.   setupControlTimer();98.   / initialize serial communicat

22、ions at 9600 bps:99.   Serial.begin(115200); 100. 101.  102. void loop() 103.   / read the analog in value:104.   / User commands.105.   /   if( Serial.available() ) 106.   /    107.   /      pro

23、cessCommand( Serial.read() ); 108.   /    109.      / print the results to the serial monitor:110.   char str50;111.   sprintf(str,"%ld, %03d, %04d, %+04d, %+04d, %+04d, %04d",now/1000, input,error,piterm,diterm,(int)iiterm,output

24、);112.   Serial.println(str);   113.  114.   delay(20);115. PC機(jī)監(jiān)控程序。Matlab,能夠自動(dòng)滾屏ARDUINO 代碼復(fù)制打印1. s1=serial('COM4','Baudrate',115200);2. fopen(s1);3. x=0;time=0;input=0;error=0;piterm=0;diterm=0;iiterm=0;pwm=0;4. str=''5. sen=0;6.  7. &

25、#160;8. try                             % use try catch to ensure fclose9. figure(1)10. hErr=plot(x,error);11. %title('Dados de LDR Aquisitados');12. hold all13. hP=plot(x,piterm);14. hD=plot(

26、x,diterm);15. hPWM=plot(x,pwm);16.  17. j=1;18. while(j<2000)19.     str=fscanf(s1);20.     sen=str2num(str);21.     if length(sen)<722.         continue23.     end24.  25.     x(j)=j26.    time

27、(j)=sen(1);27.    input(j)=sen(2);28.    error(j)=sen(3);29.    piterm(j)=sen(4);30.    diterm(j)=sen(5);31.    iiterm(j)=sen(6);32.    pwm(j)=sen(7);33.  34.    low=j-200;35.    if(low<1)36.        low=1;37.    end38.    up=j;39.  40.    x_1=x(low:up);41.    error_1=error(low:up);42.    piterm_1=piterm(low

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論