Junichi Katsu
/
wallbotLED_base
うおーるぼっとLEDのワークショップ用
wallbotLED/TB6612/TB6612.cpp@0:9fcc79b3f00e, 2017-02-03 (annotated)
- Committer:
- jksoft
- Date:
- Fri Feb 03 06:26:54 2017 +0000
- Revision:
- 0:9fcc79b3f00e
??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jksoft | 0:9fcc79b3f00e | 1 | /** |
jksoft | 0:9fcc79b3f00e | 2 | * Motor Driver TB6612 Control Library |
jksoft | 0:9fcc79b3f00e | 3 | * |
jksoft | 0:9fcc79b3f00e | 4 | * -- TB6612 is a device of the TOSHIBA. |
jksoft | 0:9fcc79b3f00e | 5 | * |
jksoft | 0:9fcc79b3f00e | 6 | * Copyright (C) 2012 Junichi Katsu (JKSOFT) |
jksoft | 0:9fcc79b3f00e | 7 | */ |
jksoft | 0:9fcc79b3f00e | 8 | |
jksoft | 0:9fcc79b3f00e | 9 | |
jksoft | 0:9fcc79b3f00e | 10 | #include "TB6612.h" |
jksoft | 0:9fcc79b3f00e | 11 | |
jksoft | 0:9fcc79b3f00e | 12 | // TB6612 Class Constructor |
jksoft | 0:9fcc79b3f00e | 13 | TB6612::TB6612(PinName pwm, PinName fwd, PinName rev): |
jksoft | 0:9fcc79b3f00e | 14 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
jksoft | 0:9fcc79b3f00e | 15 | |
jksoft | 0:9fcc79b3f00e | 16 | _fwd = 0; |
jksoft | 0:9fcc79b3f00e | 17 | _rev = 0; |
jksoft | 0:9fcc79b3f00e | 18 | _pwm = 0.0; |
jksoft | 0:9fcc79b3f00e | 19 | _pwm.period(0.001); |
jksoft | 0:9fcc79b3f00e | 20 | |
jksoft | 0:9fcc79b3f00e | 21 | bspeed = 0.0; |
jksoft | 0:9fcc79b3f00e | 22 | timer_flag = false; |
jksoft | 0:9fcc79b3f00e | 23 | } |
jksoft | 0:9fcc79b3f00e | 24 | |
jksoft | 0:9fcc79b3f00e | 25 | // Speed Control |
jksoft | 0:9fcc79b3f00e | 26 | // arg |
jksoft | 0:9fcc79b3f00e | 27 | // float speed -1.0 - 0.0 - 1.0 |
jksoft | 0:9fcc79b3f00e | 28 | void TB6612::speed(float speed) { |
jksoft | 0:9fcc79b3f00e | 29 | |
jksoft | 0:9fcc79b3f00e | 30 | if( timer_flag == true ) return; |
jksoft | 0:9fcc79b3f00e | 31 | |
jksoft | 0:9fcc79b3f00e | 32 | bspeed = speed; |
jksoft | 0:9fcc79b3f00e | 33 | |
jksoft | 0:9fcc79b3f00e | 34 | if( speed > 0.0 ) |
jksoft | 0:9fcc79b3f00e | 35 | { |
jksoft | 0:9fcc79b3f00e | 36 | _pwm = speed; |
jksoft | 0:9fcc79b3f00e | 37 | _fwd = 1; |
jksoft | 0:9fcc79b3f00e | 38 | _rev = 0; |
jksoft | 0:9fcc79b3f00e | 39 | } |
jksoft | 0:9fcc79b3f00e | 40 | else if( speed < 0.0 ) |
jksoft | 0:9fcc79b3f00e | 41 | { |
jksoft | 0:9fcc79b3f00e | 42 | _pwm = -speed; |
jksoft | 0:9fcc79b3f00e | 43 | _fwd = 0; |
jksoft | 0:9fcc79b3f00e | 44 | _rev = 1; |
jksoft | 0:9fcc79b3f00e | 45 | } |
jksoft | 0:9fcc79b3f00e | 46 | else |
jksoft | 0:9fcc79b3f00e | 47 | { |
jksoft | 0:9fcc79b3f00e | 48 | _fwd = 1; |
jksoft | 0:9fcc79b3f00e | 49 | _rev = 1; |
jksoft | 0:9fcc79b3f00e | 50 | } |
jksoft | 0:9fcc79b3f00e | 51 | } |
jksoft | 0:9fcc79b3f00e | 52 | |
jksoft | 0:9fcc79b3f00e | 53 | |
jksoft | 0:9fcc79b3f00e | 54 | // Speed Control with time-out |
jksoft | 0:9fcc79b3f00e | 55 | // arg |
jksoft | 0:9fcc79b3f00e | 56 | // float sspeed:-1.0 - 0.0 - 1.0 |
jksoft | 0:9fcc79b3f00e | 57 | // float time :0.0- |
jksoft | 0:9fcc79b3f00e | 58 | void TB6612::move(float sspeed , float time) |
jksoft | 0:9fcc79b3f00e | 59 | { |
jksoft | 0:9fcc79b3f00e | 60 | speed(sspeed); |
jksoft | 0:9fcc79b3f00e | 61 | timer_flag = true; |
jksoft | 0:9fcc79b3f00e | 62 | timer.attach(this,&TB6612::timeout,time); |
jksoft | 0:9fcc79b3f00e | 63 | } |
jksoft | 0:9fcc79b3f00e | 64 | |
jksoft | 0:9fcc79b3f00e | 65 | |
jksoft | 0:9fcc79b3f00e | 66 | void TB6612::timeout() |
jksoft | 0:9fcc79b3f00e | 67 | { |
jksoft | 0:9fcc79b3f00e | 68 | timer_flag = false; |
jksoft | 0:9fcc79b3f00e | 69 | speed(bspeed); |
jksoft | 0:9fcc79b3f00e | 70 | } |
jksoft | 0:9fcc79b3f00e | 71 |