Arduino智能小車制作報告


Arduino智能小車制作報告

制作成員:20135224陳實  20135208賀邦  20135207王國伊

前提:

Arduino,是一個開源的單板機控制器,采用了基於開放源代碼的軟硬件平台,構建於開放源代碼 simple I/O接口版,並且具有使用類似Java,C語言的Processing Wiring開發環境。選擇Arduino作為智能小車的控制板,因為它的小巧但功能強大,能夠方便的和傳感器還有各種電子元件連接,而且還能和很多軟件如Flash、Processing進行連接互動,但是最重要的是它的開源特性,它的電路設計圖和IDE都是開源的,每個人都可以免費的修改和使用,這也是現在Arduino能夠逐漸流行起來的原因,有什么比能夠和別人自由地分享自己的制作更加有吸引力呢!(視頻教材引入)

制作流水線:

1 了解必要的小車安裝知識

2 安裝必要的程序軟件

3 學習視頻教程

4 安裝小車硬件部分

5 熟悉軟件工具使用

6 查看代碼以及燒代碼流程

7 檢驗小車完成質量

 

一,了解必要的小車安裝知識

相關書籍

推薦來自百度

據說這些都是傳說級別的書籍,對於我們只能淺嘗輒止

二, 安裝必要的程序軟件

安裝Arduino開發軟件arduino-1.0.5-windows

 

 

三,學習視頻教程

視頻中有大部分出現問題的講解可以很好的調整自己的機器

 

四,安裝小車硬件部分

安裝小車具體順序:

電機---電池盒---萬向輪---拓展面包板---航機雲台---開發板

注意細節明細:

1 電機在安裝時注意兩個螺絲先不能擰的太緊,以免第二顆放不進去

2 除電池外,其他器件固定防止小車在行走時晃動,電機電源根據卡扣方向插緊即可

3 液晶屏比較注意順序,類似於二極管與三極管連接,在數電模式中有提及

4 給小車烤程序的時候小車狀態要注意

5 電位器控制由各項功能具體表示,在調試時可以根據燈亮區別判斷所處的程序功能

制作過程:

 

五,熟悉軟件工具使用

運行界面

基本功能為:

對鈎是編譯程序

向右的箭頭是編譯程序加燒錄程序

右上角那項是在用超聲波時檢測精確數據時使用,

可以看到數據時分的精確

 

六,查看代碼以及燒代碼流程

一般都由小程序構建大操作的流程,由於實現函數代碼被封裝,一些功能實現有猜測成分

先了解一般基礎構建,前進操作代碼:

前進:

int Left_motor_back=8; //左電機后退(IN1)
int Left_motor_go=9; //左電機前進(IN2)

int Right_motor_go=10; // 右電機前進(IN3)
int Right_motor_back=11; // 右電機后退(IN4)

void setup()
{
//初始化電機驅動IO為輸出方式
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
}
void run(int time) // 前進
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左電機前進
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
analogWrite(Left_motor_back,0);
delay(time * 100); //執行時間,可以調整
}

void loop()
{
delay(500);
run(10); //前進
}

-----------------------------------------------------------------

前后左右復合:

int Left_motor_back=8; //左電機后退(IN1)
int Left_motor_go=9; //左電機前進(IN2)

int Right_motor_go=10; // 右電機前進(IN3)
int Right_motor_back=11; // 右電機后退(IN4)

void setup()
{
//初始化電機驅動IO為輸出方式
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
}
void run(int time) // 前進
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左電機前進
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
analogWrite(Left_motor_back,0);
delay(time * 100); //執行時間,可以調整
}

void brake(int time) //剎車,停車
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
delay(time * 100);//執行時間,可以調整
}

void left(int time) //左轉(左輪不動,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪不動
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,0);//PWM比例0~255調速
delay(time * 100); //執行時間,可以調整
}

void spin_left(int time) //左轉(左輪后退,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,200);
analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,200);//PWM比例0~255調速
delay(time * 100); //執行時間,可以調整
}

void right(int time) //右轉(右輪不動,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右電機不動
digitalWrite(Right_motor_back,LOW);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,HIGH);//左電機前進
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);//PWM比例0~255調速
delay(time * 100); //執行時間,可以調整
}

void spin_right(int time) //右轉(右輪后退,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右電機后退
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,200);//PWM比例0~255調速
digitalWrite(Left_motor_go,HIGH);//左電機前進
digitalWrite(Left_motor_back,LOW);
analogWrite(Left_motor_go,200);
analogWrite(Left_motor_back,0);//PWM比例0~255調速
delay(time * 100); //執行時間,可以調整
}

void back(int time) //后退
{
digitalWrite(Right_motor_go,LOW); //右輪后退
digitalWrite(Right_motor_back,HIGH);
analogWrite(Right_motor_go,0);
analogWrite(Right_motor_back,150);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
analogWrite(Left_motor_go,0);
analogWrite(Left_motor_back,150);//PWM比例0~255調速
delay(time * 100); //執行時間,可以調整
}

void loop()
{
delay(2000); //延時2s后啟動
back(10); //后退1s
brake(5);//停止0.5s
run(10);//前進1s
brake(5);//停止0.5s
left(10);//向左轉1s
right(10);//向右轉1s
spin_right(20); //向右旋轉2s
spin_left(20);//向左旋轉2s
brake(5);//停車
}

前2段代碼比較可以發現:就是將前進的功能按時間控制來調整到是后退還是左右

左右的實現應該是以轉速差別來實現,而轉速可以從提供電壓的函數實現

---------------------------------------------------------------------------

后續代碼:

//#include <Servo.h>
int Left_motor_back=8; //左電機后退(IN1)
int Left_motor_go=9; //左電機前進(IN2)
int Right_motor_go=10; // 右電機前進(IN3)
int Right_motor_back=11; // 右電機后退(IN4)

int key=7;//定義按鍵 數字7 接口
int beep=12;//定義蜂鳴器 數字12 接口

const int SensorRight = 3; //右循跡紅外傳感器(P3.2 OUT1)
const int SensorLeft = 4; //左循跡紅外傳感器(P3.3 OUT2)

int SL; //左循跡紅外傳感器狀態
int SR; //右循跡紅外傳感器狀態

void setup()
{
//初始化電機驅動IO為輸出方式
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
pinMode(key,INPUT);//定義按鍵接口為輸入接口
pinMode(beep,OUTPUT);
pinMode(SensorRight, INPUT); //定義右循跡紅外傳感器為輸入
pinMode(SensorLeft, INPUT); //定義左循跡紅外傳感器為輸入
}

-----------------------------------------------------------

紅外功能:

#include <IRremote.h>//包含紅外庫
int RECV_PIN = A4;//端口聲明
IRrecv irrecv(RECV_PIN);
decode_results results;//結構聲明
int on = 0;//標志位
unsigned long last = millis();

long run_car = 0x00FF18E7;//按鍵2
long back_car = 0x00FF4AB5;//按鍵8
long left_car = 0x00FF10EF;//按鍵4
long right_car = 0x00FF5AA5;//按鍵6
long stop_car = 0x00FF38C7;//按鍵5
long left_turn = 0x00ff30CF;//按鍵1
long right_turn = 0x00FF7A85;//按鍵3
//==============================
int Left_motor_back=8; //左電機后退(IN1)
int Left_motor_go=9; //左電機前進(IN2)

int Right_motor_go=10; // 右電機前進(IN3)
int Right_motor_back=11; // 右電機后退(IN4)

void setup()
{
//初始化電機驅動IO為輸出方式
pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM)
pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
pinMode(13, OUTPUT);////端口模式,輸出
Serial.begin(9600); //波特率9600
irrecv.enableIRIn(); // Start the receiver
}
void run() // 前進
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
//analogWrite(Right_motor_back,0);
digitalWrite(Left_motor_go,HIGH); // 左電機前進
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);//PWM比例0~255調速,左右輪差異略增減
//analogWrite(Left_motor_back,0);
//delay(time * 100); //執行時間,可以調整
}

void brake() //剎車,停車
{
digitalWrite(Right_motor_go,LOW);
digitalWrite(Right_motor_back,LOW);
digitalWrite(Left_motor_go,LOW);
digitalWrite(Left_motor_back,LOW);
//delay(time * 100);//執行時間,可以調整
}

void left() //左轉(左輪不動,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪不動
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,0);//PWM比例0~255調速
//delay(time * 100); //執行時間,可以調整
}

void spin_left() //左轉(左輪后退,右輪前進)
{
digitalWrite(Right_motor_go,HIGH); // 右電機前進
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,200);
//analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,200);//PWM比例0~255調速
//delay(time * 100); //執行時間,可以調整
}

void right() //右轉(右輪不動,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右電機不動
digitalWrite(Right_motor_back,LOW);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,0);//PWM比例0~255調速
digitalWrite(Left_motor_go,HIGH);//左電機前進
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);
//analogWrite(Left_motor_back,0);//PWM比例0~255調速
//delay(time * 100); //執行時間,可以調整
}

void spin_right() //右轉(右輪后退,左輪前進)
{
digitalWrite(Right_motor_go,LOW); //右電機后退
digitalWrite(Right_motor_back,HIGH);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,200);//PWM比例0~255調速
digitalWrite(Left_motor_go,HIGH);//左電機前進
digitalWrite(Left_motor_back,LOW);
//analogWrite(Left_motor_go,200);
//analogWrite(Left_motor_back,0);//PWM比例0~255調速
//delay(time * 100); //執行時間,可以調整
}

void back() //后退
{
digitalWrite(Right_motor_go,LOW); //右輪后退
digitalWrite(Right_motor_back,HIGH);
//analogWrite(Right_motor_go,0);
//analogWrite(Right_motor_back,150);//PWM比例0~255調速
digitalWrite(Left_motor_go,LOW); //左輪后退
digitalWrite(Left_motor_back,HIGH);
//analogWrite(Left_motor_go,0);
//analogWrite(Left_motor_back,150);//PWM比例0~255調速
//delay(time * 100); //執行時間,可以調整
}

void dump(decode_results *results)
{
int count = results->rawlen;
if (results->decode_type == UNKNOWN)
{
//Serial.println("Could not decode message");
brake();
}

void loop()
{
if (irrecv.decode(&results)) //調用庫函數:解碼
{
// If it's been at least 1/4 second since the last
// IR received, toggle the relay
if (millis() - last > 250) //確定接收到信號
{
on = !on;//標志位置反
digitalWrite(13, on ? HIGH : LOW);//板子上接收到信號閃爍一下led
dump(&results);//解碼紅外信號
}
if (results.value == run_car )//按鍵2
run();//前進
if (results.value == back_car )//按鍵8
back();//后退
if (results.value == left_car )//按鍵4
left();//左轉
if (results.value == right_car )//按鍵6
right();//右轉
if (results.value == stop_car )//按鍵5
brake();//停車
if (results.value == left_turn )//按鍵1
spin_left();//左旋轉
if (results.value == right_turn )//按鍵3
spin_right();//右旋轉
last = millis();
irrecv.resume(); // Receive the next value
}
}

可以發現:只要在前后左右的實現功能上加入控制信號,利用IF或者swich等選擇實現程序的跳轉

 

燒錄:將線連接起來,利用軟件考入即可

七, 檢驗小車完成質量

 

小車不足:

在運行中出現很多不良動作

1 小車左右方向轉動不平衡,這與很多因素有關難以避免

2 小車對於一些障礙物無法判別:透明的,過低的,左右死角的

3 沒有預測,當速度調整過快的時候,會出現反應不過來的現象

實踐總結:

1 通過本次實踐,不僅在考驗我們動手能力的同時,還能提高我們對於代碼作用驅動具體行動的方式的了解

2 學習的方式的多樣化理解:紙上得來終覺淺,很多東西不一定要通過看書來了解的,動手同樣是一種不可或缺的方式

3 能力提升的表現:理解能力,學習能力,動手能力等等多種能力的提升是不言而喻的,這在以后對我們的學習與生活都是很好的教材與鋪墊

4 沒有最好只有更好的意識:精益求精是我們不斷超越自己的指導思想,小車的不足之處對於我們或許也是一種不可缺少的動力吧

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM