티스토리 뷰

OS/micro:bit

micro:bit - 라인 트레이서(Line Trace Sensor)

파란크리스마스 2018. 3. 18. 17:01
728x90

출처 : Motor Driver Board for the BBC micro:bit - V2 | Kitronik
Blog Yamasho: Delphi 10.2.2:Bleで Microbitと通信する。
googlesamples/android-BluetoothLeGatt - GitHub
ButtonService - micro:bit runtime
microbit-samples/source/examples/bluetooth-services/main.cpp - GitHub
https://github.com/lancaster-university/microbit-dal/issues/337
Advanced - micro:bit runtime - GitHub Pages
Send/Receive Data Using BLE and MBED | BBC:Microbit - Instructables

micro:bit - 라인 트레이서(Line Trace Sensor)

라인트레이서 센서와 초음파 거리센서(HC-SR04)를 이용해서 라인을 따라서 가면서 장애물이 있으면 멈추는 RC카를 구현했습니다.

소스

#include "MicroBit.h"
#include "MicroBitSerial.h"
#include "hcsr04.h"

//MicroBit uBit;
MicroBitSerial serial(USBTX, USBRX);
HCSR04 usensor(P0_0, P0_30); // P0_0 TRIGGER P0_30 ECHO
unsigned int dist;

//
// P0_1, P0_26, P0_17 성공 - line
// P0_18, P0_20, P0_3, P0_16 - moter

//AnalogIn ain(P0_3);
DigitalIn LINE_T1(P0_1);
DigitalIn LINE_T2(P0_23);
DigitalIn LINE_T3(P0_21);
DigitalIn LINE_T4(P0_22);

int main() {
	//uBit.init();
	//uBit.display.scroll("init", 70);
	serial.printf("init : \r\n");
	 
	while(1) {
		usensor.start();
		wait_ms(500);
		dist = usensor.get_dist_cm();
		serial.printf("-------------------------\r\n d:%d\r\n", dist);
		
		/*
		float x = ain.read();
		int aa = (int)(x *  1000);
		serial.printf("%d \r\n", aa);
		*/

		serial.printf("1:%d - ", LINE_T1.read());
		serial.printf("2:%d - ", LINE_T2.read());
		serial.printf("3:%d - ", LINE_T3.read());

		if (LINE_T4 == 1) {
			serial.printf("4:1\r\n");
		} else {
			serial.printf("4:0\r\n");
		}
	}
}

콘솔 로그

소스 - micro:bit - RC Car - Line Trace Sensor

source.zip

#include "MicroBit.h"
#include "MicroBitSerial.h"
#include "hcsr04.h"

MicroBit uBit;
//MicroBitSerial serial(USBTX, USBRX);
HCSR04 usensor(P0_3, P0_2); // P0_0 TRIGGER P0_30 ECHO
unsigned int dist;

//
// P0_1, P0_26, P0_17 성공 - line
// P0_18, P0_20, P0_3, P0_16 - moter

//AnalogIn ain(P0_3);
DigitalIn LINE_T1(P0_1);
DigitalIn LINE_T2(P0_23);
DigitalIn LINE_T3(P0_21);
DigitalIn LINE_T4(P0_22);

#define MOTER_LOW            0
#define MOTER_HIGH           0.32 // 스피드 - 0 ~ 1 | 최대속도 = 1
 
PwmOut moter_right_front(P0_18);    //12 - right
PwmOut moter_right_back(P0_20);     //8
PwmOut moter_left_front(P0_3);      //16 - left
PwmOut moter_left_back(P0_16);          //0
 
//모터A,B 정회전
void bothMotorStart()
{
  moter_right_front = MOTER_HIGH;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_HIGH;
  moter_left_back = MOTER_LOW;
}
  
//모터A,B 역회전
void bothMotorBack()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_HIGH;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_HIGH;
}
    
//모터A,B Stop
void stopAllMotor()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_LOW;
}
    
//모터A 역회전, 모터B 정회전
void turnLeft()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_HIGH;
  moter_left_front = MOTER_HIGH;
  moter_left_back = MOTER_LOW;
}
    
//모터A 정회전, 모터B 역회전
void turnRight()
{
  moter_right_front = MOTER_HIGH;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_HIGH;
}
    
//모터A 정회전, 모터B Stop
void motorA_Rotation()
{
  moter_right_front = MOTER_HIGH;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_LOW;
}
    
//모터A Stop, 모터B 정회전
void motorB_Rotation()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_HIGH;
  moter_left_back = MOTER_LOW;
}
    
//모터A 역회전, 모터B Stop
void motorA_Reverse()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_HIGH;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_LOW;
}
    
//모터A Stop, 모터B 역회전
void motorB_Reverse()
{
  moter_right_front = MOTER_LOW;
  moter_right_back = MOTER_LOW;
  moter_left_front = MOTER_LOW;
  moter_left_back = MOTER_HIGH;
}

int main() {
	uBit.init();
	uBit.display.scroll("init", 70);
	uBit.serial.printf("init : \r\n");
	 
	while(1) {
		usensor.start();
		wait_ms(50);
		dist = usensor.get_dist_cm();
		uBit.serial.printf("-------------------------\r\n d:%d\r\n", dist);
		uBit.serial.printf("%d, %d, %d, %d\r\n", LINE_T1.read(),LINE_T2.read(),LINE_T3.read(),LINE_T4.read());

		if (dist <= 10) {
			stopAllMotor();
		} else {
			if (LINE_T2 == 1 || LINE_T3 == 1) {
				bothMotorStart();
			} else if (LINE_T1 == 1) {
				turnRight();
			} else if (LINE_T4 == 1) {
				turnLeft();
			} else {
				bothMotorStart();
			}
		}

	}
}

micro:bit - RC Car - Line Trace Sensor

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함