Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DMdriver by
Revision 0:d7d8bd043212, committed 2018-03-20
- Comitter:
- Tomo073
- Date:
- Tue Mar 20 12:42:45 2018 +0000
- Commit message:
- 2018/3/20
Changed in this revision
DMdriver.cpp | Show annotated file Show diff for this revision Revisions of this file |
DMdriver.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r d7d8bd043212 DMdriver.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DMdriver.cpp Tue Mar 20 12:42:45 2018 +0000 @@ -0,0 +1,151 @@ +#include "DMdriver.h" +#include "mbed.h" + +Driver::Driver(PinName sda, PinName scl) : i2c(sda, scl){ +} + +//六輪車 +void Driver::motor(int ch1, int ch2, int ch3, int ch4, int ch5, int ch6){ + char data[6]; + int ch[] = {ch1, ch2, ch3, ch4, ch5, ch6}; + + //100超えちゃダメだぞ + //符号も判定しなきゃダメだぞ + for(int i = 0; i < 6; i++){ + + if(ch[i] >= 0){ + if(ch[i] > 100) ch[i] = 100; + if(!dir[i]) flag = true; + + dir[i] = true; + data[i] = ch[i]; + }else{ + if(ch[i] < -100) ch[i] = -100; + if(dir[i]) flag = true; + + dir[i] = false; + ch[i] *= -1; + data[i] = ch[i] | 0x80; + } + } + + if(flag){ + char zero[6] = {}; + i2c.write(address, zero, 6); + flag = false; + } + //送信!! + i2c.write(address, data, 6); +} + +//五輪車 +void Driver::motor(int ch1, int ch2, int ch3, int ch4, int ch5){ + char data[6]; + int ch[] = {ch1, ch2, ch3, ch4, ch5}; + + //100超えちゃダメだぞ + //符号も判定しなきゃダメだぞ + for(int i = 0; i < 5; i++){ + + if(ch[i] >= 0){ + if(ch[i] > 100) ch[i] = 100; + if(!dir[i]) flag = true; + + dir[i] = true; + data[i] = ch[i]; + }else{ + if(ch[i] < -100) ch[i] = -100; + if(dir[i]) flag = true; + + dir[i] = false; + ch[i] *= -1; + data[i] = ch[i] | 0x80; + } + } + //空データだよ + data[5] = 0x00; + + + if(flag){ + char zero[6] = {}; + i2c.write(address, zero, 6); + flag = false; + } + //送信!! + i2c.write(address, data, 6); +} + +//四輪車 +void Driver::motor(int ch1, int ch2, int ch3, int ch4){ + char data[6]; + int ch[] = {ch1, ch2, ch3, ch4}; + + + for(int i = 0; i < 4; i++){ + + if(ch[i] >= 0){ + if(ch[i] > 100) ch[i] = 100; + if(!dir[i]) flag = true; + + dir[i] = true; + data[i] = ch[i]; + }else{ + if(ch[i] < -100) ch[i] = -100; + if(dir[i]) flag = true; + + dir[i] = false; + ch[i] *= -1; + data[i] = ch[i] | 0x80; + } + } + //空データだよ + data[4] = 0x00; + data[5] = 0x00; + + if(flag){ + char zero[6] = {}; + i2c.write(address, zero, 6); + flag = false; + } + //送信!! + i2c.write(address, data, 6); +} + +//三輪車 +void Driver::motor(int ch1, int ch2, int ch3){ + char data[6]; + int ch[] = {ch1, ch2, ch3}; + + //100超えちゃダメだぞ + //符号も判定しなきゃダメだぞ + for(int i = 0; i < 6; i++){ + + if(ch[i] >= 0){ + if(ch[i] > 100) ch[i] = 100; + if(!dir[i]) flag = true; + + dir[i] = true; + data[i] = ch[i]; + }else{ + if(ch[i] < -100) ch[i] = -100; + if(dir[i]) flag = true; + + dir[i] = false; + ch[i] *= -1; + data[i] = ch[i] | 0x80; + } + } + //空データだよ + data[3] = 0x00; + data[4] = 0x00; + data[5] = 0x00; + + + if(flag){ + char zero[6] = {}; + i2c.write(address, zero, 6); + flag = false; + } + //送信!! + i2c.write(address, data, 6); +} \ No newline at end of file
diff -r 000000000000 -r d7d8bd043212 DMdriver.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DMdriver.h Tue Mar 20 12:42:45 2018 +0000 @@ -0,0 +1,27 @@ +#ifndef DMdriver_H_ +#define DMdriver_H_ + +/*---------------------------------*/ +//ダイセンのモータードライバーが使えるよ! +//サッカーロボット用だから3輪から6輪だけだよ! +//モーターの強さは-100~100で入力してね! +/*----------------------------------*/ + +#include "mbed.h" + +class Driver{ + +public: + Driver(PinName sda, PinName scl); + void motor(int ch1, int ch2, int ch3, int ch4, int ch5, int ch6); + void motor(int ch1, int ch2, int ch3, int ch4, int ch5); + void motor(int ch1, int ch2, int ch3, int ch4); + void motor(int ch1, int ch2, int ch3); + +private: + static const int address = 0x14; + I2C i2c; + bool dir[6], flag; +}; + +#endif \ No newline at end of file