最近自己做了一個智能小車(算是吧 ),我是拿eclipse開發arduino的,做得不是太好,但還是分享給大家 ,下面先看看幾張實物圖:
IMG20171116171801.jpg (2.3 MB, 下載次數: 78)
下載附件
1-1
2017-12-1 13:37 上傳
IMG20171126005048.jpg (3.48 MB, 下載次數: 52)
下載附件
1-2
2017-12-1 13:38 上傳
IMG20171126005039.jpg (2.8 MB, 下載次數: 65)
下載附件
1-3
2017-12-1 13:39 上傳
IMG20171116171729.jpg (2.62 MB, 下載次數: 53)
下載附件
1-4
2017-12-1 13:39 上傳
程序源碼:
- /*
- * ETARobot.cpp
- *
- * Created on: 2017年11月
- * Author: KACHEN
- */
- #include <WProgram.h>
- #include <main.h>
- #include "OpenContinMotor.h"
- #include "OpenBuzzer.h"
- #include "OpenGasSensor.h"
- #include "OpenFlameSensor.h"
- #include "OpenPhotoelectric.h"
- #include "OpenUltrasonic.h"
- #include "OpenAngleServo.h"
- #include "OpenQti.h"
- #define leftservo 19 //左私服舵機
- #define rightservo 18 //右私服舵機
- #define bell 13 //報警器
- #define servopin 12 //角度舵機
- //傳感器引腳定義
- const int gaspin[3]={2,3}; //氣體傳感器
- const int flamepin[4]={4,5,6}; //火焰傳感器
- const int pelectricpin[3]={7,8}; //光電傳感器
- const int qtipin[3] = {17,16}; //超聲波處QTI
- const int trig = 15; //超聲波Trig引腳
- const int echo = 14; //超聲波Echo引腳
- //子函數
- void FastGoForward(){
- PulseOut(leftservo,1700);
- PulseOut(rightservo,1300);
- delay(15);
- }
- void SlowGoForward(){
- PulseOut(leftservo,1600);
- PulseOut(rightservo,1400);
- delay(20);
- }
- void GoBack(void){
- PulseOut(leftservo,1300);
- PulseOut(rightservo,1700);
- delay(15);
- }
- /*
- * 左旋轉
- */
- void SpinLeft(void){
- PulseOut(leftservo,1300);
- PulseOut(rightservo,1300);
- delay(20);
- }
- /*
- * 右旋轉
- */
- void SpinRight(void){
- PulseOut(leftservo,1700);
- PulseOut(rightservo,1700);
- delay(20);
- }
- void TurnLeft(void){
- PulseOut(leftservo,1500);
- PulseOut(rightservo,1300);
- delay(10);
- }
- void TurnRight(void){
- PulseOut(leftservo,1700);
- PulseOut(rightservo,1500);
- delay(10);
- }
- void Stop(void){
- PulseOut(leftservo,1500);
- PulseOut(rightservo,1500);
- delay(20);
- }
- void TurnLeftAnyPulse(int pulses){
- while(pulses--)
- {
- SpinLeft();
- delay(2);
- }
- }
- void TurnRightAnyPulse(int pulses){
- while(pulses--)
- {
- SpinRight();
- delay(2);
- }
- }
- void InitAngle(void){
- servopulse(servopin,100);
- delay(100);
- servopulse(servopin,100);
- }
- void AngleLeft90(void){
- servopulse(servopin,170);
- }
- void AngleRight90(void){
- servopulse(servopin,30);
- }
- void spinRight90To0(int j=100){
- while(j--)
- {
- servopulse(servopin,j);
- if(j==30)
- break;
- }
- while(j++)
- {
- servopulse(servopin,j);
- if(j==100)
- break;
- }
- }
- void spinLeft90To0(int j=100){
- while(j++)
- {
- servopulse(servopin,j);
- if(j==170)
- break;
- }
- while(j--)
- {
- servopulse(servopin,j);
- if(j==100)
- break;
- }
- }
- /*
- * //Gas傳感器是否檢測到信號并報警
- */
- void IsGasSenser(void){
- if(GetGasStatus(gaspin[0])==true && GetGasStatus(gaspin[1])==true){
- BuzzerOFF(bell);
- }
- else
- {
- BuzzerON(bell);
- delay(500);
- BuzzerOFF(bell);
- delay(500);
- }
- }
- /*
- * //火焰 傳感器是否檢測到信號并報警
- */
- void IsFlameSenser(void){
- if(GetFlameStatus(flamepin[0])==true && GetFlameStatus(flamepin[1])==true && GetFlameStatus(flamepin[2])==true){
- BuzzerOFF(bell);
- }
- else
- {
- BuzzerON(bell);
- delay(500);
- BuzzerOFF(bell);
- delay(500);
- }
- }
- /*
- * 前方光電管尋跡避障
- */
- void IsPhotoelectric(void){
- if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==true)
- {
- for(int i=0;i<20;i++)
- {
- TurnRight();
- }
- }
- else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==false )
- {
- for(int i=0;i<20;i++)
- {
- TurnLeft();
- }
- }
- else if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==false )
- {
- for(int i=0;i<60;i++)
- {
- GoBack();
- }
- TurnLeftAnyPulse(36);
- }
- }
- /*
- * 前方光電管超聲波避障時轉向
- */
- void PhotoelectricTurn(void){
- if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==true)
- {
- TurnRightAnyPulse(25);
- }
- else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==false )
- {
- TurnLeftAnyPulse(25);
- }
- else if(GetPelectricStatus(pelectricpin[0])==false && GetPelectricStatus(pelectricpin[1])==false)
- {
- for(int i=0;i<60;i++)
- {
- GoBack();
- }
- TurnLeftAnyPulse(36);
- }
- else if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==true)
- {
- TurnLeftAnyPulse(56);
- }
- }
- /*
- * //檢測中間左QTI是否在黑線內
- */
- boolean IsMLeftQtiBlack(void){
- if(GetQtiStatus(qtipin[0])==true)
- {
- delay(2);
- if(GetQtiStatus(qtipin[0])==true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /*
- * //檢測中間右QTI是否在黑線內
- */
- boolean IsMRightQtiBlack(void){
- if(GetQtiStatus(qtipin[1])==true)
- {
- delay(2);
- if(GetQtiStatus(qtipin[1])==true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /*
- * 前方中間QTI循線快速
- */
- boolean MoveMiddleQti(void){
- if(GetQtiStatus(qtipin[0])==false && GetQtiStatus(qtipin[1])==false)
- {
- FastGoForward();
- }
- else if(GetQtiStatus(qtipin[0])==false && GetQtiStatus(qtipin[1])==true)
- {
- TurnRight();
- }
- else if(GetQtiStatus(qtipin[0])==true && GetQtiStatus(qtipin[1])==false)
- {
- TurnLeft();
- }
- else if(GetQtiStatus(qtipin[0])==true && GetQtiStatus(qtipin[1])==true)
- {
- return true;
- }
- return false;
- }
- /*
- * 前方中間QTI循線慢速
- */
- boolean MoveQti(void){
- if(GetQtiStatus(qtipin[1])==false && GetQtiStatus(qtipin[2])==false)
- {
- PulseOut(leftservo,1600);
- PulseOut(rightservo,1400);
- }
- else if(GetQtiStatus(qtipin[1])==false && GetQtiStatus(qtipin[2])==true)
- {
- PulseOut(leftservo,1530);
- PulseOut(rightservo,1500);
- }
- else if(GetQtiStatus(qtipin[1])==true && GetQtiStatus(qtipin[2])==false)
- {
- PulseOut(leftservo,1500);
- PulseOut(rightservo,1470);
- }
- else if(GetQtiStatus(qtipin[1])==true && GetQtiStatus(qtipin[2])==true)
- {
- return true;
- }
- return false;
- }
- /*
- * 前方中間QTI調整
- */
- void MiddleQtiAdjust(void){
- if(IsMLeftQtiBlack())
- {
- while(IsMLeftQtiBlack())
- {
- PulseOut(leftservo,1450);
- PulseOut(rightservo,1450);
- delay(20);
- }
- }
- else if(IsMRightQtiBlack())
- {
- while(IsMRightQtiBlack())
- {
- PulseOut(leftservo,1550);
- PulseOut(rightservo,1550);
- delay(20);
- }
- }
- Stop();
- delay(100);
- }
- void SetDistance(void){
- if(GetPelectricStatus(pelectricpin[0])==true && GetPelectricStatus(pelectricpin[1])==true)
- {
- if(DistanceDetection()>100){
- for(int i=0;i<20;i++)
- FastGoForward();
- }
- else if(DistanceDetection()<100 && DistanceDetection()>10){
- for(int i=0;i<10;i++)
- FastGoForward();
- }
- else if(DistanceDetection()<10 && DistanceDetection()>0){
- Stop();
- delay(100);
- PhotoelectricTurn();
- }
- }
- else{
- IsPhotoelectric();
- }
- }
- //初始化
- void setup(void){
- Serial.begin(9600);
- InitContinMotorPin(leftservo);
- InitContinMotorPin(rightservo);
- Stop();
- InitBuzzerPin(bell);
- InitGasSensor(gaspin[0]);
- InitGasSensor(gaspin[1]);
- InitFlameSensor(flamepin[0]);
- InitFlameSensor(flamepin[1]);
- InitFlameSensor(flamepin[2]);
- InitPhotoelectric(pelectricpin[0]);
- InitPhotoelectric(pelectricpin[1]);
- InitSingleQti(qtipin[0]);
- InitSingleQti(qtipin[1]);
- InitUltrasonic(trig,echo);
- InitAngleServo(servopin);
- }
- //loop循環
- void loop(void){
- if(Serial.available()>0)
- {
- char ch = Serial.read();
- switch(ch){
- case 'A':
- Serial.print("A:前進 ");
- for(int i=0;i<5;i++)
- {
- FastGoForward();
- }
- break;
- case 'B':
- Serial.print("B:后退");
- for(int i=0;i<5;i++)
- {
- GoBack();
- }
- break;
- case 'C':
- Serial.print("C:左轉 ");
- for(int i=0;i<5;i++)
- {
- TurnLeft();
- }
- break;
- case 'D':
- Serial.print("D:右轉");
- for(int i=0;i<5;i++)
- {
- TurnRight();
- }
- break;
- case 'e':
- Serial.print("E:QTI尋跡模式");
- while(1){
- MoveMiddleQti();
- char ch = Serial.read();
- if(ch=='a') break;
- }
- break;
- case 'f':
- Serial.print("F:智能避障模式");
- while(1){
- SetDistance();
- char ch = Serial.read();
- if(ch=='a') break;
- }
- break;
- default:
- Stop();
- break;
- }
- }
- }
- //主函數
- int main(void){
- init();
- setup();
- InitAngle();
- while(1){
- IsGasSenser();
- IsFlameSenser();
- loop();
- }
- }
復制代碼
0.png (11.86 KB, 下載次數: 67)
下載附件
2017-12-1 21:03 上傳
全部資料51hei下載地址:
Test_Robot.rar
(153.69 KB, 下載次數: 13)
2017-12-1 13:40 上傳
點擊文件名下載附件
程序源碼 下載積分: 黑幣 -5
|