Cov txheej txheem:

Servo Tsav Tswj Nrog STM32F4 ARM MCU: 4 Cov Kauj Ruam
Servo Tsav Tswj Nrog STM32F4 ARM MCU: 4 Cov Kauj Ruam

Video: Servo Tsav Tswj Nrog STM32F4 ARM MCU: 4 Cov Kauj Ruam

Video: Servo Tsav Tswj Nrog STM32F4 ARM MCU: 4 Cov Kauj Ruam
Video: tseb thauj khoom vaj tsev 2024, Kaum ib hlis
Anonim
Servo Tsav Tswj Nrog STM32F4 ARM MCU
Servo Tsav Tswj Nrog STM32F4 ARM MCU
Servo Tsav Tswj Nrog STM32F4 ARM MCU
Servo Tsav Tswj Nrog STM32F4 ARM MCU

Nyob zoo cov phooj ywg:) Yog li, hauv txoj haujlwm no peb yuav tswj lub cav servo nrog STM32F4 ARM MCU. Hauv kuv qhov xwm txheej, kuv yuav siv daim ntawv tshawb pom, tab sis yog tias koj nkag siab qhov ntsiab lus ntawm qhov teeb meem, tom qab ntawd koj tuaj yeem thov nws rau txhua MCU. Yog li cia peb pib:)

Kauj ruam 1: Kho vajtse thiab Software yuav tsum tau

Hais txog kho vajtse peb yuav xav tau:

  • MCU uas yog nyob rau hauv kuv rooj plaub STM32f4 Discovery board
  • Ib qho kev pabcuam servo, zoo li SG90 lossis lwm yam

Hais txog software peb yuav xav tau:

  • STM32CubeMX
  • Keil Ua

Yog tias koj muaj tag nrho cov no, dhia mus rau kauj ruam tom ntej:)

Kauj ruam 2: STM32CubeMX Configuration

Raws li koj paub, rau kev tswj hwm lub cav servo peb xav tau PWM teeb liab. Cov kev xav tau ntawm PWM teeb liab zoo li qhov no:

  • PWM lub sijhawm yuav tsum yog 20 mS
  • Lub sijhawm yuav tsum nyob nruab nrab ntawm 0.5 mS txog 2.5 mS. Thaum lub sijhawm yog 0.5 mS, tom qab ntawd servo yuav tig 0 degrees, 1.5 mS rau 90 degrees, thiab 2.5 mS rau 180 degrees.

Yog li, peb yuav tsum teeb tsa PWM thiab rau lub hom phiaj ntawd peb yuav siv Timer1.

  • Ua ntej, xaiv TIM1 los ntawm ntu Timers. Cov kauj ruam no
  • Tom qab ntawd, los ntawm ntu ntu

    1. Xaiv Internal Clock Cov kauj ruam no
    2. PWM Generation CH1 Cov kauj ruam no
  • Tom qab ntawd, los ntawm Configuration ntu

    1. Teem Prescaler rau 160 Cov kauj ruam no
    2. Teem Sijhawm Sijhawm rau 2000 Cov kauj ruam no
    3. Teem Pulse rau 50 Cov kauj ruam no
  • Ib qho ntxiv, los ntawm Clock Configuration teeb tsa APB1 Timer moos rau 16MHz. Cov kauj ruam no

Tam sim no, wb tham me ntsis txog cov kauj ruam no:

Peb APB1 Timer moos zaus yog 16MHz. Yog li, nws txhais tau tias nws yuav tsum tau 16, 000, 000 zuam kom tau 1 thib ob. Txawm li cas los xij, peb teeb tsa peb cov prescaler rau 160. Nws txhais tau tias, peb faib peb zaus los ntawm tus lej ntawd thiab txo tus lej zuam mus rau 100, 000. Yog li, rau 1 thib ob peb xav tau 100, 000 zuam. Txawm li cas los xij, peb xav tau 20mS ntawm PWM lub sijhawm raws li peb tau hais ua ntej. Yog li, raws cov lej yooj yim, peb xav tau 2000 zuam rau 20mS. Yog li, los ntawm kev teeb Lub Sijhawm Sijhawm rau 2000 peb txiav txim siab lub sijhawm ntawm PWM teeb liab uas yog 20mS. Tam sim no peb yuav tsum txiav txim siab tus lej zauv kom tau txais Sijhawm los ntawm 0.5mS txog 2.5mS. Peb tuaj yeem tau txais qhov sib npaug no los ntawm kev ua lej yooj yim thiab nws yog:

On_Time = (Tick_Number / 100). Nco ntsoov tias qhov no yog on_time uas hloov lub kaum sab xis ntawm servo motor. Yog li, hauv qab daim duab kuv suav nrog cov kauj ruam no. Yog tias koj muaj lus nug sau hauv cov lus thiab kuv yuav teb sai li sai tau.

Duab ntawm kev suav

Tom qab ua tag nrho cov no tsim cov cai:)

Kauj Ruam 3: Keil UVision Coding

Yog li, ua ntej txiav txim siab seb peb xav ua dab tsi? Peb xav tau, los sau ua haujlwm uas lees paub qib thiab sau nws mus rau servo. Yog li, peb yuav ua li cas? Raws li peb tau hais ua ntej, txhawm rau hloov lub kaum sab xis peb yuav tsum hloov pauv raws sijhawm. Peb cov ces kaum hloov ntawm [0, 180] thiab peb tus lej zuam uas txiav txim siab lub sijhawm hloov ntawm [50, 250]. Yog li, peb xav tau daim phiaj ua haujlwm uas daim duab qhia lub kaum sab xis rau ntau ntawm cov zuam. Piv txwv li, rau 0 degree 50 zuam, rau 180 degree 250 zuam thiab ntxiv rau … Yog li cia peb sau peb daim ntawv qhia ua haujlwm:

int daim ntawv qhia (int st1, int fn1, int st2, int fn2, int tus nqi) {rov qab (1.0*(tus nqi-st1))/((fn1-st1)*1.0)*(fn2-st2)+st2; }

Nov yog peb daim ntawv qhia ua haujlwm. Koj puas txaus siab tias nws muab los li cas? Ces nyeem ntawd. Yog li, peb coj peb cov kab ke thiab tus nqi uas peb xav ua daim duab qhia chaw.

Tam sim no, cia peb sau txoj haujlwm uas lees txais lub kaum ntse ntse thiab qhia nws mus rau ntau yam zuam:

void servo_write (int angle) {htim1Instance-> CCR1 = daim ntawv qhia (0, 180, 50, 250, kaum sab xis); }

Raws li koj tuaj yeem pom, txoj cai no lees txais lub kaum ntse ntse thiab qhia nws rau thaj tsam ntawm tus lej zuam. Ib qho ntawd, tus lej zuam tau muab rau CCR1 sau npe uas tswj hwm lub sijhawm thiab yog li, lub kaum ntse ntse.

Txawm li cas los xij, txhawm rau txhua yam ntawm no los ua haujlwm peb xub pib pwm uas tuaj yeem ua tiav los ntawm kab kab xwb:

HAL_TIM_PWM_Start (& htim1, TIM_CHANNEL_1);

Yog li, peb muaj lub luag haujlwm uas lees txais lub kaum ntse ntse thiab sau nws mus rau servo. Cia peb sim nws thiab sau peb cov haujlwm cheb uas yooj yim heev:

void servo_sweep (tsis muaj dab tsi) {rau (int i = 0; i <= 180; i ++) {servo_write (i); HAL_Delay (10); } rau (int i = 180; i> = 0; i--) {servo_write (i); HAL_Delay (10); }}

Yog li, nws tsuas suav txog 180 thiab tom qab ntawd nqis mus rau 0 thiab sau cov txiaj ntsig no rau servo:) Yog li, cia peb pom qhov tshwm sim!

Kauj ruam 4: Qhov tshwm sim:)

Yog li, qhov no yog qhov kawg. Yog koj muaj lus nug thov nug. Kuv yuav zoo siab txhawm rau teb lawv. Ua tsaug ntau ntau rau kev nyeem thiab vam tias kuv yuav pom koj hauv qhov haujlwm tom ntej:)

Pom zoo: