Collections of BERTL libraries

Committer:
DongExpander
Date:
Mon Apr 18 12:30:42 2016 +0000
Revision:
2:4a9ed5ca8a9a
Parent:
0:46115ad78747
Feature; Added functions_bertl and class_software

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