Collections of BERTL libraries

Committer:
DongExpander
Date:
Mon Apr 18 09:19:10 2016 +0000
Revision:
0:46115ad78747
Child:
2:4a9ed5ca8a9a
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DongExpander 0:46115ad78747 1 #include "mbed.h"
DongExpander 0:46115ad78747 2 #include "class_hardware.h"
DongExpander 0:46115ad78747 3
DongExpander 0:46115ad78747 4 Motor::Motor(PinName pin_pwm, PinName pin_fwd, PinName pin_rev) :
DongExpander 0:46115ad78747 5 pwm(pin_pwm), fwd(pin_fwd), rev(pin_rev) {
DongExpander 0:46115ad78747 6 pwm=0;
DongExpander 0:46115ad78747 7 pwm.period(0.001);
DongExpander 0:46115ad78747 8 fwd=0; rev=0;
DongExpander 0:46115ad78747 9 }
DongExpander 0:46115ad78747 10
DongExpander 0:46115ad78747 11 void Motor::stop() {
DongExpander 0:46115ad78747 12 pwm=0;
DongExpander 0:46115ad78747 13 fwd=0; rev=0;
DongExpander 0:46115ad78747 14 }
DongExpander 0:46115ad78747 15
DongExpander 0:46115ad78747 16 void Motor::stop(bool powered) {
DongExpander 0:46115ad78747 17 pwm=0;
DongExpander 0:46115ad78747 18 if ( powered )
DongExpander 0:46115ad78747 19 { fwd=1; rev=1; }
DongExpander 0:46115ad78747 20 else
DongExpander 0:46115ad78747 21 { fwd=0; rev=0; }
DongExpander 0:46115ad78747 22 }
DongExpander 0:46115ad78747 23
DongExpander 0:46115ad78747 24 void Motor::set(int speed) {
DongExpander 0:46115ad78747 25 if ( speed > 0 ) {
DongExpander 0:46115ad78747 26 pwm=( (float)(speed) / 100 );
DongExpander 0:46115ad78747 27 fwd=1; rev=0;
DongExpander 0:46115ad78747 28 }
DongExpander 0:46115ad78747 29 else if ( speed < 0 ) {
DongExpander 0:46115ad78747 30 pwm=( (float)(speed) / -100 );
DongExpander 0:46115ad78747 31 fwd=0; rev=1;
DongExpander 0:46115ad78747 32 }
DongExpander 0:46115ad78747 33 else
DongExpander 0:46115ad78747 34 { pwm=0; fwd=0; rev=0; }
DongExpander 0:46115ad78747 35 }
DongExpander 0:46115ad78747 36
DongExpander 0:46115ad78747 37 IRSensor::IRSensor(PinName pin_ir) :
DongExpander 0:46115ad78747 38 ir(pin_ir) {
DongExpander 0:46115ad78747 39 threshold = 500;
DongExpander 0:46115ad78747 40 }
DongExpander 0:46115ad78747 41
DongExpander 0:46115ad78747 42 IRSensor::IRSensor(PinName pin_ir, int threshold_black) :
DongExpander 0:46115ad78747 43 ir(pin_ir) {
DongExpander 0:46115ad78747 44 threshold = threshold_black;
DongExpander 0:46115ad78747 45 }
DongExpander 0:46115ad78747 46
DongExpander 0:46115ad78747 47 bool IRSensor::is_black() {
DongExpander 0:46115ad78747 48 if ( ir.read_u16()>>6 > threshold )
DongExpander 0:46115ad78747 49 return true;
DongExpander 0:46115ad78747 50
DongExpander 0:46115ad78747 51 return false;
DongExpander 0:46115ad78747 52 }
DongExpander 0:46115ad78747 53
DongExpander 0:46115ad78747 54 int IRSensor::get_data() {
DongExpander 0:46115ad78747 55 return ( ir.read_u16()>>6 );
DongExpander 0:46115ad78747 56 }
DongExpander 0:46115ad78747 57
DongExpander 0:46115ad78747 58 USSensor::USSensor(PinName pin_trigger, PinName pin_echo) :
DongExpander 0:46115ad78747 59 trigger(pin_trigger),echo(pin_echo) {
DongExpander 0:46115ad78747 60 echo.rise(this, &USSensor::on_rise);
DongExpander 0:46115ad78747 61 echo.fall(this, &USSensor::on_fall);
DongExpander 0:46115ad78747 62
DongExpander 0:46115ad78747 63 distance = 50;
DongExpander 0:46115ad78747 64 }
DongExpander 0:46115ad78747 65
DongExpander 0:46115ad78747 66 USSensor::USSensor(PinName pin_trigger, PinName pin_echo, int distance_set_bool) :
DongExpander 0:46115ad78747 67 trigger(pin_trigger),echo(pin_echo) {
DongExpander 0:46115ad78747 68 echo.rise(this, &USSensor::on_rise);
DongExpander 0:46115ad78747 69 echo.fall(this, &USSensor::on_fall);
DongExpander 0:46115ad78747 70
DongExpander 0:46115ad78747 71 if (distance_set_bool < 4000 && distance_set_bool > 20)
DongExpander 0:46115ad78747 72 distance = distance_set_bool;
DongExpander 0:46115ad78747 73 else
DongExpander 0:46115ad78747 74 distance = 50;
DongExpander 0:46115ad78747 75 }
DongExpander 0:46115ad78747 76
DongExpander 0:46115ad78747 77 void USSensor::initialize() {
DongExpander 0:46115ad78747 78 ticker.attach(this, &USSensor::measure, 0.06);
DongExpander 0:46115ad78747 79 timer.start();
DongExpander 0:46115ad78747 80 }
DongExpander 0:46115ad78747 81
DongExpander 0:46115ad78747 82 void USSensor::measure() {
DongExpander 0:46115ad78747 83 trigger=1;
DongExpander 0:46115ad78747 84 wait_us(10);
DongExpander 0:46115ad78747 85 trigger=0;
DongExpander 0:46115ad78747 86 }
DongExpander 0:46115ad78747 87
DongExpander 0:46115ad78747 88 void USSensor::on_rise() {
DongExpander 0:46115ad78747 89 timer.reset();
DongExpander 0:46115ad78747 90 }
DongExpander 0:46115ad78747 91
DongExpander 0:46115ad78747 92 void USSensor::on_fall() {
DongExpander 0:46115ad78747 93 distance_mm = ( (float)(timer.read_us()) * 1716E-4 );
DongExpander 0:46115ad78747 94
DongExpander 0:46115ad78747 95 if ( distance_mm < distance )
DongExpander 0:46115ad78747 96 in_distance = true;
DongExpander 0:46115ad78747 97 else
DongExpander 0:46115ad78747 98 in_distance = false;
DongExpander 0:46115ad78747 99 }