320 likes | 437 Views
伺服機控制實習. VHDL 數位電路實習與專題設計 文魁資訊 -UE301. 單元 11-1 遙控伺服機位置控制實習 I. 實驗目的 瞭解遙控伺服機之驅動與位置控制的方法. 相關知識. 遙控伺服機介紹 體積小、扭力大、重量輕而且省電的馬達 在各種遙控模型上被廣泛使用,如遙控車、遙控船與遙控直昇機等等. 遙控伺服機於三維空間轉動之機器視覺應用, 義守大學機器智慧與自動化實驗室提供. 伺服機外觀圖. 遙控伺服機於機器昆蟲玩具之應用, 義守大學機器智慧與自動化實驗室提供. 相關知識.
E N D
伺服機控制實習 VHDL數位電路實習與專題設計 文魁資訊-UE301
單元11-1 遙控伺服機位置控制實習I • 實驗目的 • 瞭解遙控伺服機之驅動與位置控制的方法 VHDL數位電路實習與專題設計
相關知識 • 遙控伺服機介紹 • 體積小、扭力大、重量輕而且省電的馬達 • 在各種遙控模型上被廣泛使用,如遙控車、遙控船與遙控直昇機等等 遙控伺服機於三維空間轉動之機器視覺應用, 義守大學機器智慧與自動化實驗室提供 伺服機外觀圖 遙控伺服機於機器昆蟲玩具之應用, 義守大學機器智慧與自動化實驗室提供 VHDL數位電路實習與專題設計
相關知識 • 伺服機主要是由外殼、電路板、直流馬達、齒輪與位置檢測器所構成。其工作原理是由遙控接收機發出信號給伺服機,經由電路板上的控制晶片判斷轉動方向,再驅動直流馬達開始轉動,透過減速齒輪將動力傳至轉動軸,同時由位置檢測器送回信號,判斷是否已經到達定位。 伺服機內部架構 伺服機外觀圖 VHDL數位電路實習與專題設計
相關知識 • 遙控伺服機位置控制 • 伺服機控制信號示意圖 VHDL數位電路實習與專題設計
相關知識 • 要產生控制伺服機的脈波訊號,我們需要以0.1MHz的頻率來產生0.01ms的振盪週期。我們可以計數2000次來做為脈波訊號的週期20ms,所以要產生0度的控制訊號只要設定PWM訊號的比較值為70就可以產生0.7ms的脈波訊號。 伺服機控制信號產生器方塊圖 VHDL數位電路實習與專題設計
遙控伺服機位置控制實習I • 實驗功能 • 以CPLD設計一個伺服機控制器以控制伺服機的轉動位置 • 實驗電路圖 VHDL數位電路實習與專題設計
1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:servo_pwm.vhd 4 --功 能:產生伺服機控制脈波訊號 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_arith.all; 10 use ieee.std_logic_unsigned.all; 11 --------------------------------------------------------------- 12 -- 70(0.7ms) => 0 degree ; 110(1.1ms) => 45 degree 13 -- 150(1.5ms) => 90 degree; 230(2.3ms) => 180 degree 14 --------------------------------------------------------------- 15 entity servo_pwm is 16 generic (N : integer :=70 17 divisor: integer :=18 ); 18 port( 19 clk : in std_logic; 20 q : out std_logic 21 ); 22 end servo_pwm; 23 24 architecture a of servo_pwm is 25 signal clk1 : std_logic; 26 signal cnt2 : std_logic; 27 signal period : integer range 0 to 1999; 28 begin 30 process (clk) 31 variable cnt1,divisor2 : integer range 0 to divisor; 32 begin 33 divisor2:=divisor/2; 34 if (clk'event and clk='1') then 35 if cnt1= divisor then 36 cnt1 := 1; 37 else 38 cnt1 := cnt1 + 1; 39 end if; 40 end if; 41 42 if (clk'event and clk='1') then 43 if (( cnt1= divisor2) or (cnt1= divisor)) then 44 cnt2 <= not cnt2; 45 end if; 46 end if; 47 clk1<= cnt2 ; 48 end process; 49 50 ----- pwm generate ----- 51 process(clk1,period) 52 begin 53 if clk1'event and clk1= '1' then 54 period <= period + 1; 55 end if; 56 end process; 57 q <= '1' when period < N else '0'; 58 end a; 伺服機控制實習程式碼 VHDL數位電路實習與專題設計
功能模擬與CPLD下載驗證 • 在功能模擬圖中我們可以看到q為一個固定寬度的脈波信號,符合程式中的參數N設定值,當period信號累加超過N,則q就變為低準位。這樣的模擬結果就符合我們要的脈波寬度,也就是q訊號的輸出腳可以控制伺服機轉到0度的位置。當然我們可以改變N值來控制脈波的寬度以控制伺服機轉動的角度,但是N的值必須介於70到230之間。 VHDL數位電路實習與專題設計
燒錄於力浦電子FPT-3實驗板 • PWM實習腳位配置表(FPT-3) • 伺服機控制實驗實體圖 VHDL數位電路實習與專題設計
燒錄於力浦電子LP-2900實驗板 • 由於LP-2900的振盪器是提供10MHz的頻率,因此我們必須將除頻器的除數值更改為100,這樣才可以得到0.1MHz的計數器頻率。 VHDL數位電路實習與專題設計
1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:servo_pwm_lp2900.vhd 4 --功 能:產生伺服機控制脈波訊號 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_arith.all; 10 use ieee.std_logic_unsigned.all; 11 --------------------------------------------------------------- 12 -- 70(0.7ms) => 0 degree ; 110(1.1ms) => 45 degree 13 -- 150(1.5ms) => 90 degree; 230(2.3ms) => 180 degree 14 --------------------------------------------------------------- 15 entity servo_pwm_lp2900 is 16 generic (N : integer :=70 17 divisor: integer :=100 ); 18 port( 19 clk : in std_logic; 20 q : out std_logic 21 ); 22 end servo_pwm_lp2900; 23 24 architecture a of servo_pwm_lp2900 is 25 signal clk1 : std_logic; 26 signal cnt2 : std_logic; 27 signal period : integer range 0 to 1999; 28 begin 30 process (clk) 31 variable cnt1,divisor2 : integer range 0 to divisor; 32 begin 33 divisor2:=divisor/2; 34 if (clk'event and clk='1') then 35 if cnt1= divisor then 36 cnt1 := 1; 37 else 38 cnt1 := cnt1 + 1; 39 end if; 40 end if; 41 42 if (clk'event and clk='1') then 43 if (( cnt1= divisor2) or (cnt1= divisor)) then 44 cnt2 <= not cnt2; 45 end if; 46 end if; 47 clk1<= cnt2 ; 48 end process; 49 50 ----- pwm generate ----- 51 process(clk1,period) 52 begin 53 if clk1'event and clk1= '1' then 54 period <= period + 1; 55 end if; 56 end process; 57 q <= '1' when period < N else '0'; 58 end a; 伺服機控制實習程式碼(LP-2900) VHDL數位電路實習與專題設計
燒錄於力浦電子LP-2900實驗板 • PWM實習腳位配置表(LP-2900) • LP-2900實驗板元件規劃 q VHDL數位電路實習與專題設計
練習 • 請設計一個以4個按鍵來控制伺服機轉動的角度到達0度、45度、90度、與135度的實驗電路,並自行規劃腳位配置以及下載至CPLD進行功能驗證。 伺服機角度控制電路圖 VHDL數位電路實習與專題設計
1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:servo_control.vhd 4 --功 能:以4個按鍵控制伺服機轉動角度 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_arith.all; 10 use ieee.std_logic_unsigned.all; 12 entity servo_control is 13 generic (divisor: integer :=18); 14 port( 15 clk : in std_logic; 16 BTN : in std_logic_vector(3 downto 0); 17 pwm: out std_logic 18 ); 19 end servo_control; 21 architecture a of servo_control is 22 signal clk1 : std_logic; --new clk : 100000Hz 23 signal cnt2 : std_logic; 24 signal data : integer range 0 to 230; 25 signal period : integer range 0 to 1999; --2000 x 0.01ms = 20ms 26 begin 28 ----- clk divider, generate 100000Hz frequence, 0.01ms ----- 29 process (clk) 30 variable cnt1 : integer range 0 to divisor; 31 variable divisor2 : integer range 0 to divisor; 32 begin 33 divisor2 := divisor/2; 34 if (clk'event and clk='1') then 35 if cnt1=divisor then 36 cnt1 := 1; 37 else 38 cnt1 := cnt1 + 1; 39 end if; 40 end if; 41 42 if (clk'event and clk='1') then 43 if (( cnt1=divisor2) or (cnt1=divisor)) then 44 cnt2 <= not cnt2; 45 end if; 46 end if; 47 clk1<= cnt2 ; 48 end process; 49 process(clk1) 50 begin 51 case BTN is 52 when "1110" => data <= 70; -- 70(0.7ms) => 0 degree 53 when "1101" => data <= 110; -- 110(1.1ms) => 45 degree 54 when "1011" => data <= 150; -- 150(1.5ms) => 90 degree 55 when "0111" => data <= 190; -- 190(1.9ms) => 135 degree 56 when others => null; 57 end case; 58 end process; 59 ----- up counter ----- 60 process(clk1,period) 61 begin 62 if clk1'event and clk1= '1' then 63 period <= period + 1; 64 end if; 65 end process; 66 q <= '1' when period < data else '0'; 67 end a; 程式碼 VHDL數位電路實習與專題設計
功能模擬圖 VHDL數位電路實習與專題設計
燒錄於力浦電子FPT-3實驗板 • 伺服機角度控制實習腳位配置表(FPT-3) • 伺服機控制實驗實體圖1,按下SW1,轉至0度 VHDL數位電路實習與專題設計
燒錄於力浦電子FPT-3實驗板 • 伺服機控制實驗實體圖2,按下SW2,轉至45度 • 伺服機控制實驗實體圖3,按下SW3,轉至90度 • 伺服機控制實驗實體圖4,按下SW4,轉至135度 VHDL數位電路實習與專題設計
燒錄於力浦電子LP-2900實驗板 • 將servo_control.vhd的除數值改為100後另存檔案且命名為servo_control_lp2900.vhd • 伺服機角度控制實習腳位配置表(LP-2900) VHDL數位電路實習與專題設計
BTN0~BTN3 PWM LP-2900實驗板元件規劃 VHDL數位電路實習與專題設計
單元11-2 遙控伺服機位置控制實習II • 實驗目的 • 以8位元資料匯流排控制遙控伺服機之位置 VHDL數位電路實習與專題設計
相關知識 • 如何以8位元的資料匯流排來控制伺服機轉動的角度由0度至180度呢?8位元的資料匯流排範圍有256,但70至230的範圍只有160,所以我們必須將8位元資料匯流的值乘以0.625,以控制伺服機的轉動角度。可是要如何將8位元資料匯流排的值乘以0.625呢? • 乘以0.625就是等於乘以5再除以8,我們將有小數點的問題轉換成整數的問題比較好解決,一個8位元數值乘以5再除以8以VHDL來表示,且不以”*”與”/”這兩個運算子來描述,如下所示 • xin為8位元數值,代表輸入資料,temp為11位元資料暫存器,代表xin乘以5,xout為temp的高8位元資料,代表temp除以8,所以xout則為xin乘0.625後的數值。 VHDL數位電路實習與專題設計
遙控伺服機位置控制實習II • 實驗功能 • 以指撥開關為8位元之輸入,以控制伺服機的轉動位置 • 實驗電路圖 VHDL數位電路實習與專題設計
程式與說明 • 8位元資料匯流排控制伺服機轉動角度之電路圖 • 8位元資料匯流排控制伺服機轉動角度之上層電路圖 VHDL數位電路實習與專題設計
x_625模組程式碼 1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:x_625.vhd 4 --功 能:將8位元資料匯流排的值乘以0.625 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_arith.all; 10 use ieee.std_logic_unsigned.all; 11 entity x_625 is 12 port( 13 xin : in std_logic_vector(7 downto 0); 14 xout: out std_logic_vector(7 downto 0) 15 ); 16 end x_625; 17 18 architecture arch of x_625 is 19 signal temp : std_logic_vector(10 downto 0); 20 begin 21 22 -- xout = xin x 5 / 8 23 temp <= ('0' & xin & "00")+ ("000" & xin); --temp = xin x 5 24 xout <= temp(10 downto 3); --xout = temp / 8 25 end arch; VHDL數位電路實習與專題設計
1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:sv_trans.vhd 4 --功 能:伺服機轉角控制信號產生模組 5 --日 期:2003.8.8 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_unsigned.all; 10 use ieee.std_logic_arith.all; 12 entity servo_trans is 13 generic (divisor: integer :=18); 14 port( 15 clk : in std_logic; 16 xout: in std_logic_vector(7 downto 0); 17 pwm : out std_logic 18 ); 19 end servo_trans; 21 architecture a of servo_trans is 22 signal clk1 : std_logic; 23 signal cnt2 : std_logic; 24 signal data : integer range 0 to 230; 25 signal period : integer range 0 to 1999; 26 begin 27 ---------- clk divider ---------- 28 process (clk) 29 variable cnt1 : integer range 0 to divisor; 30 variable divisor2 : integer range 0 to divisor; 31 begin 32 divisor2 := divisor/2; 33 if (clk'event and clk='1') then 34 if cnt1=divisor then 35 cnt1 := 1; 36 else 37 cnt1 := cnt1 + 1; 38 end if; 39 end if; 41 if (clk'event and clk='1') then 42 if (( cnt1=divisor2) or (cnt1=divisor)) then 43 cnt2 <= not cnt2; 44 end if; 45 end if; 46 clk1 <= cnt2 ; 47 end process; 48 ----- data transfer ----- 49 process(clk1) 50 begin 51 if clk1'event and clk1='1' then 52 data <= CONV_integer(xout) + 70; 53 end if; 54 end process; 55 ----- up counter ----- 56 process(clk1) 57 begin 58 if clk1'event and clk1= '1' then 59 period <= period + 1; 60 end if; 61 end process; 62 pwm <= '1' when period < data else '0'; 63 end a; servo_trans模組程式碼 VHDL數位電路實習與專題設計
1 ------------------------------------------------------------------- 2 --實驗名稱:伺服機控制實習 3 --檔案名稱:sv_motor.vhd 4 --功 能:以8位元資料匯流排控制遙控伺服機之位置 5 --日 期:2003.4.1 6 ------------------------------------------------------------------- 7 library ieee; 8 use ieee.std_logic_1164.all; 9 use ieee.std_logic_unsigned.all; 10 use ieee.std_logic_arith.all; 11 12 entity sv_motor is 13 port( 14 clk : in std_logic; 15 DIP : in std_logic_vector(7 downto 0); 16 pwm : out std_logic 17 ); 18 end sv_motor; 19 20 architecture arch of sv_motor is 22 ---------- 0 to 255 maping to 0 to 160 ---------- 23 component x_625 24 port( 25 xin : in std_logic_vector(7 downto 0); -- range 0 to 255 26 xout: out std_logic_vector(7 downto 0) -- 0 to 160 27 ); 28 end component; 29 30 ---------- servo motor position controller ---------- 31 component servo_trans 32 generic (divisor : integer := 18); 33 port( 34 clk : in std_logic; 35 xout: in std_logic_vector(7 downto 0); 36 pwm : out std_logic 37 ); 38 end component; 39 signal xout : std_logic_vector(7 downto 0); 40 begin 41 u1: x_625 port map(DIP,xout); 42 u2: servo_trans port map(clk,xout,pwm); 43 end arch; sv_motor上層模組程式碼 VHDL數位電路實習與專題設計
功能模擬與CPLD下載驗證 • 我們可以看到xin信號就是8位元資料匯流排的值,且xout等於xin乘以0.625的值,而pwm伺服機轉角控制脈波信號隨著8位元資料匯流排值而改變 VHDL數位電路實習與專題設計
PWM DIP0~DIP7 燒錄於力浦電子FPT-3實驗板 • 伺服機控制實習腳位配置表(FPT-3) FPT-3實驗板元件規劃 VHDL數位電路實習與專題設計
燒錄於力浦電子LP-2900實驗板 • 將servo_trans.vhd的除數值改為100後另存檔案且命名為servo_trans_lp2900.vhd • 主程式電路架構圖(sv_motor_lp2900.gdf) • 伺服機控制實習腳位配置表 (LP-2900) VHDL數位電路實習與專題設計
PWM DIP0~DIP7 LP-2900實驗板元件規劃 VHDL數位電路實習與專題設計
勘誤 • 8-2 圖8-1伺服機外觀圖 • 8-28 圖8-23多一個箭號 VHDL數位電路實習與專題設計