田中くんのモタドラを制御する石本さんのやつ
T_motor.cpp@8:2759e619969a, 2017-09-27 (annotated)
- Committer:
- fujikenac
- Date:
- Wed Sep 27 10:28:52 2017 +0000
- Revision:
- 8:2759e619969a
- Parent:
- 7:a1d54597161d
fix i2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fujikenac | 0:e10d08530490 | 1 | #include "mbed.h" |
fujikenac | 0:e10d08530490 | 2 | #include "T_motor.h" |
fujikenac | 0:e10d08530490 | 3 | |
fujikenac | 0:e10d08530490 | 4 | void T_motor::setAddr(int addr_) |
fujikenac | 0:e10d08530490 | 5 | { |
fujikenac | 0:e10d08530490 | 6 | addr = addr_; |
fujikenac | 0:e10d08530490 | 7 | } |
fujikenac | 0:e10d08530490 | 8 | |
fujikenac | 0:e10d08530490 | 9 | void T_motor::init(char addr,int freq = 100000) |
fujikenac | 0:e10d08530490 | 10 | { |
fujikenac | 0:e10d08530490 | 11 | setAddr(addr); |
fujikenac | 8:2759e619969a | 12 | i2c->frequency(freq); |
fujikenac | 0:e10d08530490 | 13 | char d = 0; |
fujikenac | 8:2759e619969a | 14 | i2c->write((addr+1) << 1,&d,1); |
fujikenac | 0:e10d08530490 | 15 | wait(0.01); |
fujikenac | 0:e10d08530490 | 16 | } |
fujikenac | 0:e10d08530490 | 17 | |
fujikenac | 8:2759e619969a | 18 | T_motor::T_motor(I2C* i2c_,int addr):i2c(i2c_) |
fujikenac | 0:e10d08530490 | 19 | { |
fujikenac | 0:e10d08530490 | 20 | init(addr); |
fujikenac | 0:e10d08530490 | 21 | } |
fujikenac | 0:e10d08530490 | 22 | |
fujikenac | 1:5bd161b83ce0 | 23 | T_motor& T_motor::operator=(float fval)// -1 <= fval <= 1 |
fujikenac | 0:e10d08530490 | 24 | { |
fujikenac | 7:a1d54597161d | 25 | if(fabs(fval) < 0.005f)free(); |
fujikenac | 0:e10d08530490 | 26 | else { |
fujikenac | 5:0e69f06b24ff | 27 | if(fval < 0) run(1,char(-1*fval*255)); |
fujikenac | 5:0e69f06b24ff | 28 | else if(fval > 0) run(0,char(fval*255)); |
fujikenac | 0:e10d08530490 | 29 | } |
fujikenac | 0:e10d08530490 | 30 | return *this; |
fujikenac | 0:e10d08530490 | 31 | } |
fujikenac | 0:e10d08530490 | 32 | |
fujikenac | 1:5bd161b83ce0 | 33 | void T_motor::control(float fval)//copy from operator |
fujikenac | 1:5bd161b83ce0 | 34 | { |
fujikenac | 7:a1d54597161d | 35 | if(fabs(fval) < 0.005f)free(); |
fujikenac | 1:5bd161b83ce0 | 36 | else { |
fujikenac | 5:0e69f06b24ff | 37 | if(fval < 0) run(1,char(-1*fval*255)); |
fujikenac | 5:0e69f06b24ff | 38 | else if(fval > 0) run(0,char(fval*255)); |
fujikenac | 1:5bd161b83ce0 | 39 | } |
fujikenac | 1:5bd161b83ce0 | 40 | } |
fujikenac | 1:5bd161b83ce0 | 41 | |
fujikenac | 0:e10d08530490 | 42 | bool T_motor::run(char mode,char speed) |
fujikenac | 0:e10d08530490 | 43 | { |
fujikenac | 0:e10d08530490 | 44 | bool f = 0; |
fujikenac | 0:e10d08530490 | 45 | char d = speed; |
fujikenac | 0:e10d08530490 | 46 | |
fujikenac | 8:2759e619969a | 47 | if(mode == 0 || mode == 1)f = i2c->write((addr+mode) << 1,&d,1); |
fujikenac | 0:e10d08530490 | 48 | wait(0.01); |
fujikenac | 0:e10d08530490 | 49 | return f; |
fujikenac | 0:e10d08530490 | 50 | } |
fujikenac | 0:e10d08530490 | 51 | |
fujikenac | 0:e10d08530490 | 52 | bool T_motor::stop() |
fujikenac | 0:e10d08530490 | 53 | { |
fujikenac | 0:e10d08530490 | 54 | return run(1,0); |
fujikenac | 0:e10d08530490 | 55 | } |
fujikenac | 0:e10d08530490 | 56 | |
fujikenac | 0:e10d08530490 | 57 | bool T_motor::free() |
fujikenac | 0:e10d08530490 | 58 | { |
fujikenac | 0:e10d08530490 | 59 | return run(0,0); |
fujikenac | 0:e10d08530490 | 60 | } |
fujikenac | 0:e10d08530490 | 61 |