電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示
創(chuàng)作
電子發(fā)燒友網(wǎng)>電子資料下載>電子資料>L298N電機(jī)模塊服務(wù)

L298N電機(jī)模塊服務(wù)

2022-12-12 | zip | 0.09 MB | 次下載 | 2積分

資料介紹

描述

系列介紹

本系列文章的重點是基于可在所有機(jī)器人中重復(fù)使用的服務(wù)創(chuàng)建可擴(kuò)展的面向?qū)ο蟮哪K化軟件架構(gòu),而無需在每次創(chuàng)建新機(jī)器人時都從頭開始。

也許開始使用機(jī)器人技術(shù)的最具成本效益的方法是使用您可以在任何電子商務(wù)(全球速賣通、banggood 等)上購買的智能機(jī)器人汽車但當(dāng)然,購買它是最簡單的部分……您不需要專門購買它,即使有了Smart Robot Car Yo的描述,您也會發(fā)現(xiàn)許多不同的變體。我將為您使用此套件獲得的所有最常見模塊創(chuàng)建“服務(wù)”,因此您可以選擇您需要的服務(wù),并將它們一起使用來創(chuàng)建您自己的智能機(jī)器人,而無需從頭開始對每個機(jī)器人進(jìn)行操作你做。

指數(shù)

這是我將要創(chuàng)建的泰博機(jī)器人服務(wù)系列或文章的索引。

關(guān)于 L298N 電機(jī)驅(qū)動器

?
poYBAGOSm1GAd6KZAACzxXHGTKY222.jpg
?

這種雙路雙向電機(jī)驅(qū)動器可讓您輕松獨立地控制兩個雙向最高 2A 的電機(jī)。

它是機(jī)器人應(yīng)用的理想選擇,非常適合連接到每個電機(jī)只需要幾條控制線的任何 Arduino

還集成了一個板載用戶可訪問的 5V 穩(wěn)壓器,可用于為需要高達(dá)約 1A 的穩(wěn)壓 5V 直流電源的任何其他電路供電。

特征:

  • 電機(jī)電源:6 至 35 VDC
  • 控制邏輯:標(biāo)準(zhǔn)TTL邏輯電平
  • 輸出功率:每個高達(dá) 2 A
  • 啟用和方向控制引腳
  • IC散熱片
  • 開機(jī) LED 指示燈

創(chuàng)建服務(wù)

我們將使用在上一篇文章中創(chuàng)建的基服務(wù)類。如果您沒有閱讀它,請閱讀它,以便您了解我們將在這里創(chuàng)建的內(nèi)容。

MotorService 基類

我們將創(chuàng)建另一個抽象層,它將定義任何 Motor Service 應(yīng)具有的行為。這樣做,允許我們?yōu)槲覀冑徺I的任何硬件創(chuàng)建不同的電機(jī)服務(wù),并以相同的方式使用我們擁有的任何電機(jī)服務(wù)(因此代碼幾乎沒有變化)。

MotorService 類應(yīng)該允許我們:

  • 設(shè)置電機(jī)的速度
  • 獲取電機(jī)的速度
  • 當(dāng)然,從基服務(wù)類繼承的功能

MotorService 類頭文件 (MotorService.h) 如下所示:

#pragma once 
#include "Arduino.h" 
#include "Service.h" 
namespace Taibot 
{ 
	class MotorService : public Service 
	{ 
	public: 
		MotorService(bool isEnabled, bool isVerbose); 
		// Sets the speed of the motor 
		// speed = 0 then  motor stops 
		// speed between -255 and -1 then motor runs backwards  
		// speed between 1 and 255 then motor runs forward 
		// Method that must be implemented in every MotorService specific implementation inheriting from the MotorService class 
		virtual void SetSpeed(int speed) = 0; 
		// Returns the current speed of the motor 
		// Method that must be implemented in every MotorService specific implementation inheriting from the MotorService class 
		virtual int GetSpeed() const = 0; 
	}; 
}; 

及其實現(xiàn)(MotorService.h):

#include "MotorService.h" 
using namespace Taibot; 
MotorService::MotorService(bool isEnabled, bool isVerbose) : Service(isEnabled, isVerbose) 
{ 
} 

如您所見,MotorService類除了為 MotorServices 實現(xiàn)定義契約外沒有做任何其他事情(現(xiàn)在我們將處理L298N 電機(jī)驅(qū)動模塊的實現(xiàn))。

L298NMotorService類

最后我們得到了真正做某事的代碼!

此類是MotorService的實現(xiàn),它控制L298N 電機(jī)驅(qū)動模塊。它遵循我們在前兩個基類中指定的布局。

像往常一樣,首先是頭文件(L298NMotorService.h):

#pragma once 
#include "MotorService.h" 
namespace Taibot 
{ 
	class L298NMotorService : public MotorService 
	{ 
	public: 
		L298NMotorService(bool isEnabled, bool isVerbose, unsigned int pinEnable, unsigned int pinIn1, unsigned int pinIn2); 
		// Implements the method inherited from the base MotorService class 
		void SetSpeed(int speed); 
		// Implements the method inherited from the base MotorService class 
		int GetSpeed() const; 
		void Setup(); 
		void Update(); 
	private: 
		unsigned int _pinEnable; 
		unsigned int _pinIn1; 
		unsigned int _pinIn2; 
		// Keeps track of the current speed of the Motor driver 
		unsigned int _currentSpeed = 0; 
	}; 
}; 

及其實現(xiàn)(L298NMotorService.cpp):

#include "L298NMotorService.h" 
using namespace Taibot; 
L298NMotorService::L298NMotorService(bool isEnabled, bool isVerbose, unsigned int pinEnable, unsigned int pinIn1, unsigned int pinIn2) 
	: MotorService(isEnabled, isVerbose) 
{ 
	_pinEnable = pinEnable; 
	_pinIn1 = pinIn1; 
	_pinIn2 = pinIn2; 
	_currentSpeed = 0; 
} 
void L298NMotorService::SetSpeed(int speed) 
{ 
	// Save the current speed... 
	_currentSpeed = speed; 
	if (IsVerbose()) 
	{ 
		//If we are logging, print the speed we are giving to the motor 
		Serial.print("L298NMotor: speed="); 
		Serial.println(speed); 
	} 
	// Only activate the motors if the driver is enabled 
	if (IsEnabled()) 
	{ 
		if (speed >= 0) 
		{ 
			// if the speed is positive or 0 then move forward 
			analogWrite(_pinEnable, speed); 
			digitalWrite(_pinIn1, HIGH); 
			digitalWrite(_pinIn2, LOW); 
		} 
		else 
		{ 
			// if the speed is negative then move backwards 
			analogWrite(_pinEnable, -speed); 
			digitalWrite(_pinIn1, LOW); 
			digitalWrite(_pinIn2, HIGH); 
		} 
	} 
} 
int L298NMotorService::GetSpeed() const 
{ 
	return _currentSpeed; 
} 
void L298NMotorService::Setup() 
{ 
	// We have nothing to set up 
} 
void L298NMotorService::Update() 
{ 
	// This service doesn't do anythin in background so this method is empty 
} 

真正的魔法發(fā)生在 SetSpeed 方法中。如您所見,在移動電機(jī)之前,我們正在檢查服務(wù)是否已啟用。此外,在打印調(diào)試信息之前,我們正在檢查它是否為 Verbose。

此方法接收 -255 到 255 范圍內(nèi)的 int 值。

  • 當(dāng)值為負(fù)(-255 到 -1)時,電機(jī)向后移動。
  • 當(dāng)值為正(1 到 255)時,電機(jī)向前移動。
  • 當(dāng)然,當(dāng)速度為 0 時,電機(jī)停止

由于 L298N 模塊的工作方式,我們應(yīng)該向 _pinEnable 發(fā)送一個 PWM 脈沖,以設(shè)置旋轉(zhuǎn)速度。這個脈沖可以在 0 到 255 之間。我們正在使用analogWrite調(diào)用來設(shè)置 Arduino 上數(shù)字引腳的 PWM 輸出。這里有更多詳細(xì)信息)。

我們還將_pinIn1_pinIn2設(shè)置為 HIGH 或 LOW,具體取決于旋轉(zhuǎn)方向,如 L298N 模塊的文檔所述。

測試服務(wù)

我們現(xiàn)在需要將代碼添加到草圖以測試此服務(wù)。我認(rèn)為您可能會理解為什么我們一直在處理所有這些代碼混亂。

這是 Arduino Sketch 的外觀:

/* 
Name:		Taibot.ino 
Created:	12/13/2016 10:27:53 AM 
Author:	Nahuel Taibo  savagemakers.com 
*/ 
#include "L298NMotorService.h" 
using namespace Taibot; 
// Pin definitions for the L298N Motor driver (Change this defines according to your hardware configuration) 
#define PIN_L298N_ENA PIN2 
#define PIN_L298N_IN1 PIN3 
#define PIN_L298N_IN2 PIN4 
#define PIN_L298N_IN3 PIN5 
#define PIN_L298N_IN4 PIN6 
#define PIN_L298N_ENB PIN7 
L298NMotorService rightMotor(true, true, PIN_L298N_ENA, PIN_L298N_IN1, PIN_L298N_IN2); 
L298NMotorService leftMotor(true, true, PIN_L298N_ENB, PIN_L298N_IN3, PIN_L298N_IN4); 
//We will use this variables to change the robot speed on after some seconds (without using delays) 
unsigned long previousTime = millis(); 
unsigned int updateFreq = 5000; 
// the setup function runs once when you press reset or power the board 
void setup() 
{ 
	Serial.begin(115200); 
	// Call the Setup method of all the services we are using 
	rightMotor.Setup(); 
	leftMotor.Setup(); 
	Serial.println("Taibot Started."); 
} 
// the loop function runs over and over again until power down or reset 
void loop() 
{ 
	// Call the Update method of all the service we are using... 
	rightMotor.Update(); 
	leftMotor.Update(); 
	// Do anything else we want to do... 
	if ((previousTime + updateFreq) < millis()) 
	{ 
		previousTime = millis(); 
		if (rightMotor.GetSpeed() > 0) 
		{ 
			rightMotor.SetSpeed(0); 
			leftMotor.SetSpeed(0); 
		} 
		else 
		{ 
			rightMotor.SetSpeed(150); 
			leftMotor.SetSpeed(150); 
		} 
	} 
} 

如果一切正常,構(gòu)建此代碼并將其上傳到您的機(jī)器人后,您應(yīng)該會看到它前進(jìn) 5 秒,停止 5 秒,并重復(fù)該行為,直到您將其關(guān)閉。

結(jié)論

是的,它有很多代碼只是為了推動機(jī)器人前進(jìn),但是一旦我們開始添加越來越多的服務(wù),您就會注意到這些代碼比無休止的意大利面條代碼更有意義。我希望您能關(guān)注本系列,因為我們會在每一篇新文章中不斷為您的機(jī)器人提供越來越多的智能。

您將找到本文附帶的代碼存儲庫。我創(chuàng)建了一個僅包含此服務(wù)實現(xiàn)的分支,因此如果您有意的話,您只能獲得此服務(wù)。

如果您不理解本文的任何部分、無法使此代碼工作、發(fā)現(xiàn)錯誤或有任何建議,請告訴我,我愿意改進(jìn)它并使其成為任何人的干凈代碼庫想要重用它。


下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1山景DSP芯片AP8248A2數(shù)據(jù)手冊
  2. 1.06 MB  |  532次下載  |  免費
  3. 2RK3399完整板原理圖(支持平板,盒子VR)
  4. 3.28 MB  |  339次下載  |  免費
  5. 3TC358743XBG評估板參考手冊
  6. 1.36 MB  |  330次下載  |  免費
  7. 4DFM軟件使用教程
  8. 0.84 MB  |  295次下載  |  免費
  9. 5元宇宙深度解析—未來的未來-風(fēng)口還是泡沫
  10. 6.40 MB  |  227次下載  |  免費
  11. 6迪文DGUS開發(fā)指南
  12. 31.67 MB  |  194次下載  |  免費
  13. 7元宇宙底層硬件系列報告
  14. 13.42 MB  |  182次下載  |  免費
  15. 8FP5207XR-G1中文應(yīng)用手冊
  16. 1.09 MB  |  178次下載  |  免費

本月

  1. 1OrCAD10.5下載OrCAD10.5中文版軟件
  2. 0.00 MB  |  234315次下載  |  免費
  3. 2555集成電路應(yīng)用800例(新編版)
  4. 0.00 MB  |  33566次下載  |  免費
  5. 3接口電路圖大全
  6. 未知  |  30323次下載  |  免費
  7. 4開關(guān)電源設(shè)計實例指南
  8. 未知  |  21549次下載  |  免費
  9. 5電氣工程師手冊免費下載(新編第二版pdf電子書)
  10. 0.00 MB  |  15349次下載  |  免費
  11. 6數(shù)字電路基礎(chǔ)pdf(下載)
  12. 未知  |  13750次下載  |  免費
  13. 7電子制作實例集錦 下載
  14. 未知  |  8113次下載  |  免費
  15. 8《LED驅(qū)動電路設(shè)計》 溫德爾著
  16. 0.00 MB  |  6656次下載  |  免費

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935054次下載  |  免費
  3. 2protel99se軟件下載(可英文版轉(zhuǎn)中文版)
  4. 78.1 MB  |  537798次下載  |  免費
  5. 3MATLAB 7.1 下載 (含軟件介紹)
  6. 未知  |  420027次下載  |  免費
  7. 4OrCAD10.5下載OrCAD10.5中文版軟件
  8. 0.00 MB  |  234315次下載  |  免費
  9. 5Altium DXP2002下載入口
  10. 未知  |  233046次下載  |  免費
  11. 6電路仿真軟件multisim 10.0免費下載
  12. 340992  |  191187次下載  |  免費
  13. 7十天學(xué)會AVR單片機(jī)與C語言視頻教程 下載
  14. 158M  |  183279次下載  |  免費
  15. 8proe5.0野火版下載(中文版免費下載)
  16. 未知  |  138040次下載  |  免費