田中くんのモタドラを制御する石本さんのやつ
T_motor.cpp@6:dc3db7df2a4b, 2017-09-04 (annotated)
- Committer:
- fujikenac
- Date:
- Mon Sep 04 02:04:39 2017 +0000
- Revision:
- 6:dc3db7df2a4b
- Parent:
- 5:0e69f06b24ff
- Child:
- 7:a1d54597161d
fix operator, control
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 | 0:e10d08530490 | 12 | i2c.frequency(freq); |
fujikenac | 0:e10d08530490 | 13 | char d = 0; |
fujikenac | 0:e10d08530490 | 14 | i2c.write((addr+1) << 1,&d,1); |
fujikenac | 0:e10d08530490 | 15 | wait(0.01); |
fujikenac | 0:e10d08530490 | 16 | } |
fujikenac | 0:e10d08530490 | 17 | |
fujikenac | 0:e10d08530490 | 18 | T_motor::T_motor(PinName sda,PinName scl,int addr):i2c(sda,scl) |
fujikenac | 0:e10d08530490 | 19 | { |
fujikenac | 0:e10d08530490 | 20 | init(addr); |
fujikenac | 0:e10d08530490 | 21 | } |
fujikenac | 0:e10d08530490 | 22 | |
fujikenac | 0:e10d08530490 | 23 | T_motor::T_motor(I2C& i2c_,int addr):i2c(i2c_) |
fujikenac | 0:e10d08530490 | 24 | { |
fujikenac | 0:e10d08530490 | 25 | init(addr); |
fujikenac | 0:e10d08530490 | 26 | } |
fujikenac | 0:e10d08530490 | 27 | |
fujikenac | 1:5bd161b83ce0 | 28 | T_motor& T_motor::operator=(float fval)// -1 <= fval <= 1 |
fujikenac | 0:e10d08530490 | 29 | { |
fujikenac | 6:dc3db7df2a4b | 30 | if(fabs(fval) < 0.001f)free(); |
fujikenac | 0:e10d08530490 | 31 | else { |
fujikenac | 5:0e69f06b24ff | 32 | if(fval < 0) run(1,char(-1*fval*255)); |
fujikenac | 5:0e69f06b24ff | 33 | else if(fval > 0) run(0,char(fval*255)); |
fujikenac | 0:e10d08530490 | 34 | } |
fujikenac | 0:e10d08530490 | 35 | return *this; |
fujikenac | 0:e10d08530490 | 36 | } |
fujikenac | 0:e10d08530490 | 37 | |
fujikenac | 1:5bd161b83ce0 | 38 | void T_motor::control(float fval)//copy from operator |
fujikenac | 1:5bd161b83ce0 | 39 | { |
fujikenac | 6:dc3db7df2a4b | 40 | if(fabs(fval) < 0.001f)free(); |
fujikenac | 1:5bd161b83ce0 | 41 | else { |
fujikenac | 5:0e69f06b24ff | 42 | if(fval < 0) run(1,char(-1*fval*255)); |
fujikenac | 5:0e69f06b24ff | 43 | else if(fval > 0) run(0,char(fval*255)); |
fujikenac | 1:5bd161b83ce0 | 44 | } |
fujikenac | 1:5bd161b83ce0 | 45 | } |
fujikenac | 1:5bd161b83ce0 | 46 | |
fujikenac | 0:e10d08530490 | 47 | bool T_motor::run(char mode,char speed) |
fujikenac | 0:e10d08530490 | 48 | { |
fujikenac | 0:e10d08530490 | 49 | bool f = 0; |
fujikenac | 0:e10d08530490 | 50 | char d = speed; |
fujikenac | 0:e10d08530490 | 51 | |
fujikenac | 0:e10d08530490 | 52 | if(mode == 0 || mode == 1)f = i2c.write((addr+mode) << 1,&d,1); |
fujikenac | 0:e10d08530490 | 53 | wait(0.01); |
fujikenac | 0:e10d08530490 | 54 | return f; |
fujikenac | 0:e10d08530490 | 55 | } |
fujikenac | 0:e10d08530490 | 56 | |
fujikenac | 0:e10d08530490 | 57 | bool T_motor::stop() |
fujikenac | 0:e10d08530490 | 58 | { |
fujikenac | 0:e10d08530490 | 59 | return run(1,0); |
fujikenac | 0:e10d08530490 | 60 | } |
fujikenac | 0:e10d08530490 | 61 | |
fujikenac | 0:e10d08530490 | 62 | bool T_motor::free() |
fujikenac | 0:e10d08530490 | 63 | { |
fujikenac | 0:e10d08530490 | 64 | return run(0,0); |
fujikenac | 0:e10d08530490 | 65 | } |
fujikenac | 0:e10d08530490 | 66 |