0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

TouchGFXを使おう10~Servo編~

Posted at

TouchGFXを使おう~I2C編~の続きとして今回はServoモータを動かします。

STM32CubeIDEを使ってみよう How To STM32CubeIDE 日本語版 (13) PWMでサーボモーター(SERVO)を動かしてみようを元にプログラム作成をします。

画面の作成

新規プロジェクトを作成し、画面上にサーボモーターの角度を表示するテキストエリアを作成します。
TranstationServo ° にし クリック します。
image.png

wildcardの設定

Wildcard1クリック します。
image.png
クリック します。
image.png
Initial Value000 とします。
Buffer size12 にしました。
image.png
左の Textsクリック し、文字設定を行います。
Typographies を選択し、LargeWildcard Ranges0-9 にします。
image.png

pin設定

今回、Timerを使用出来るピンとしてPD15を使用しますが、PWMが使用出来るピンであればほかのピンでも問題ありません。
image.png
コード生成後、STM32CubeIDEプロジェクト を開き
PD15クリックTIM4_CH4 を選択します。
image.png
TIM4を開き、ModeInternal Clock,Channel4PWM Generation CH4 にします。
image.png
ConfigurationPrescaler550、Counter period10000 とします。

275MHz50Hz にします。
275MHz/550/10000=50Hz
image.png
Yes で進みます。
image.png

プログラム

Duty設定

__ HAL_TIM_SET_COMPAREPWM を生成します。

__ HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,duty);
275MHz/550=500000Hz⇒2us
1us500カウント になります。

dutyの計算式

250+角度*(1000/180)

0.5ms 0度

500us/2=2500 counts

2.5ms 180度

2500us/2=1250 counts

Screen1View.cpp

\TouchGFX\gui\src\screen1_screen\Screen1View.cpp

#include <gui/screen1_screen/Screen1View.hpp>
#ifndef SIMULATOR
#include "stm32h7xx_hal.h"
#include "stm32h7xx_hal_tim.h"
#include "stm32h7xx_hal_tim_ex.h"
#include "main.h"
#endif

setupScreen内にHAL_TIM_PWM_Startを追加していますが、handleTickEvent内でも宣言しています。

void Screen1View::setupScreen()
{
    Screen1ViewBase::setupScreen();
	#ifndef SIMULATOR
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
	#endif
}
void Screen1View::tearDownScreen()
{
    Screen1ViewBase::tearDownScreen();
}
void Screen1View::handleTickEvent()
{
    tickCounter++;
    #ifndef SIMULATOR
    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
	#endif

	if(tickCounter >= 150)
	{
		touchgfx::Unicode::strncpy(textArea1Buffer,"0", TEXTAREA1_SIZE);
		textArea1.invalidate();
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,250);
	#endif
	}
	if(tickCounter >= 300)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"30", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,417);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 450)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"60", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,583);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 600)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"90", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,750);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 750)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"120", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,917);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 900)
	{
		touchgfx::Unicode::strncpy(textArea1Buffer,"150", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,1083);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 1050)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"180", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,1250);
	#endif
	textArea1.invalidate();
	}
	if(tickCounter >= 1200)
	{
	touchgfx::Unicode::strncpy(textArea1Buffer,"120", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
		__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,917);
	#endif
	textArea1.invalidate();
	}

	if(tickCounter >= 1350)
	{
	tickCounter = 0;
		touchgfx::Unicode::strncpy(textArea1Buffer,"60", TEXTAREA1_SIZE);
	#ifndef SIMULATOR
	__HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_4,417);
	#endif
	textArea1.invalidate();
	}
}

image.png

Screen1View.hpp

\TouchGFXgui\screen1_screen\Screen1View.hpp
public:

virtual void handleTickEvent();

protected:
uint16_t tickCounter;

image.png

main.h

main.hにexternを追加します。

image.png

以上でコンパイル&書込みすることでサーボモーターが回ります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?