test
Dependencies: mbed TB6612FNG HMC6352
Revision 0:4c10d798d1bb, committed 2021-10-13
- Comitter:
- ushiroji
- Date:
- Wed Oct 13 10:49:38 2021 +0000
- Commit message:
- test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HMC6352.lib Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/aberk/code/HMC6352/#83c0cb554099
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TB6612FNG.lib Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/jksoft/code/TB6612FNG/#95d5e9ee8c15
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,77 @@ +#include "mbed.h" +#include "TB6612.h" +#include "HMC6352.h" +#include "us015.h" + +TB6612 motor_a(D10,D6,D7); //モータA制御用(pwma,ain1,ain2) +TB6612 motor_b(D11,D8,D9); //モータB制御用(pwmb,bin1,bin2) +Serial pc(USBTX,USBRX); //USBシリアル通信用 +HMC6352 compass(D4, D5); +US015 hs(D2,D3); +int d; +int n; + +int motor(char m) { + float motor_speed; //モータスピード情報格納用 + while(1) { + motor_speed=0.5; //モータスピード(低速運転させるため2分の1の値とする。) + switch(m) + { + case '1': motor_a=motor_speed; //モータA正転 + break; + case '2': motor_a=0; //モータAブレーキ + break; + case '3': motor_a=-motor_speed; //モータA逆転 + break; + case '7': motor_b=motor_speed; //モータB正転 + break; + case '8': motor_b=0; //モータBブレーキ + break; + case '9': motor_b=-motor_speed; //モータB逆転 + break; + default : motor_a=0; + motor_b=0; //両方モータブレーキ + break; + } + } +} + +int Compass() +{ + int angle; + compass.setOpMode(HMC6352_CONTINUOUS, 1, 20); + angle = compass.sample() / 10; + return angle; +} + +int Sonic() +{ + int distance; + distance = hs.GetDistance(); + return distance; +} + +int main() +{ + motor(1); + motor(7); + while(1) + { + n = Compass(); + if(n >= 5) { //5°ずれると片方のモーターが逆転する + motor(3); + wait(5.0); + } + else { + motor(1); + motor(7); + } + + d = Sonic(); + if(d<=2000){ //超音波反応 + motor(3); + wait(5.0); + motor(1); + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/us015.cpp Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,75 @@ +//************************************************** +// +// us015.cpp +// 概要:超音波距離センサ US-015 +// +//************************************************** +#include "mbed.h" +#include "us015.h" + + + +//********************************* +// コンストラクタ +//********************************* +US015::US015(PinName trg, PinName ech) + :_trigerOut(trg),_interruptEcho(ech) +{ + // タイマーの開始 + timer.start(); + // トリガー信号の定期出力を行う + //tk.attach_us(this,&US015::TrigerOut,US015_PERIODIC_TRIGER); + // エコーバック信号の立ち上がり・立ち下がり処置の割り込み + _interruptEcho.fall(this,&US015::EchoFall); + _interruptEcho.rise(this,&US015::EchoRise); +} +// End of Constructor + + +//********************************* +// トリガー信号を出力する +//********************************* +void US015::TrigerOut() +{ + // トリガー出力として10[us]のパルス信号を出す + _trigerOut = US015_TRIGER_ON; + wait_us(US015_TRIGER_PALUSE_WIDTH); + _trigerOut = US015_TRIGER_OFF; +} +// End of TrigerOut + + +//********************************* +// 距離情報の取得 +//********************************* +float US015::GetDistance() +{ + return distance; +} +// End of GetDistance + + +//********************************* +// エコーの信号の立ち下がり +//********************************* +void US015::EchoFall() +{ + //エコー信号の立ち下がり時間を取得 + tmEnd=timer.read_us(); + + // 反射面距離の計算(往復の距離を音速と経過時間から求め、その半分を片道の距離とする) + // (エコー受信時間 - トリガー発信時間)* 音速0.340[mm/us]/2 + distance = ((tmEnd-tmBegin)*(0.340))/2; +} +// End of EchoFall + + +//********************************* +// エコーの信号の立ち上がり処理 +//********************************* +void US015::EchoRise() +{ + //エコー信号の立ち上がり時間を記録 + tmBegin=timer.read_us(); +} +// End of EchoRise
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/us015.h Wed Oct 13 10:49:38 2021 +0000 @@ -0,0 +1,40 @@ +#ifndef __US015_H +#define __US015_H + +#include "mbed.h" + + +//==================================== +// define定義 +//==================================== +#define US015_TRIGER_ON 1 // トリガーON +#define US015_TRIGER_OFF 0 // トリガーOFF +#define US015_TRIGER_PALUSE_WIDTH 10 // トリガーの幅 +#define US015_PERIODIC_TRIGER 100000 // 音響測距トリガー +#define US015_SOUND_OF_SPEED 0.340 // 音速[mm/us] + + +//==================================== +// 音響測距センサ(US015)の制御クラス +//==================================== +class US015 +{ + public: + US015(PinName trg, PinName ech); + US015(PinName trg, PinName ech, float tkTime); + float GetDistance(); + void TrigerOut(); // トリガー出力 + + private: + DigitalOut _trigerOut; // U015トリガー出力 + InterruptIn _interruptEcho; // U015エコー入力 + Timer timer; // 時間計測用のタイマ + Ticker tk; // 周期処理用のチッカー + float tmBegin; // エコーの立ち上がり時間 + float tmEnd; // エコーの立ち下がり時間 + float distance; // 距離測定 + void EchoFall(); // エコーの立ち下がり処理 + void EchoRise(); // エコーの立ち上がり処理 +}; + +#endif \ No newline at end of file