Ultraschall Test Geschwindigkeitsregelung
Fork of BertlLib by
Bertl14.cpp@1:cb66be3bb78f, 2014-04-25 (annotated)
- Committer:
- hollegha2
- Date:
- Fri Apr 25 18:57:23 2014 +0000
- Revision:
- 1:cb66be3bb78f
- Parent:
- 0:e0131e671ea0
Rev2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hollegha2 | 0:e0131e671ea0 | 1 | |
hollegha2 | 0:e0131e671ea0 | 2 | #include "mbed.h" |
hollegha2 | 0:e0131e671ea0 | 3 | #include "Bertl14.h" |
hollegha2 | 0:e0131e671ea0 | 4 | |
hollegha2 | 1:cb66be3bb78f | 5 | // Aenderung |
hollegha2 | 1:cb66be3bb78f | 6 | |
hollegha2 | 0:e0131e671ea0 | 7 | Motor::Motor(PinName pwm, PinName fwd, PinName rev) : |
hollegha2 | 0:e0131e671ea0 | 8 | _pwm(pwm), _fwd(fwd), _rev(rev) |
hollegha2 | 0:e0131e671ea0 | 9 | { |
hollegha2 | 0:e0131e671ea0 | 10 | _pwm.period(0.001); _pwm=0; |
hollegha2 | 0:e0131e671ea0 | 11 | _fwd=0; _rev=0; |
hollegha2 | 0:e0131e671ea0 | 12 | } |
hollegha2 | 0:e0131e671ea0 | 13 | |
hollegha2 | 0:e0131e671ea0 | 14 | void Motor::SetPow(float aPow) |
hollegha2 | 0:e0131e671ea0 | 15 | { |
hollegha2 | 0:e0131e671ea0 | 16 | if( aPow>=0.0 ) { |
hollegha2 | 0:e0131e671ea0 | 17 | _fwd=1; _rev=0; |
hollegha2 | 0:e0131e671ea0 | 18 | _pwm = aPow; |
hollegha2 | 0:e0131e671ea0 | 19 | } |
hollegha2 | 0:e0131e671ea0 | 20 | else { |
hollegha2 | 0:e0131e671ea0 | 21 | _fwd=0; _rev=1; |
hollegha2 | 0:e0131e671ea0 | 22 | _pwm = -aPow; |
hollegha2 | 0:e0131e671ea0 | 23 | } |
hollegha2 | 0:e0131e671ea0 | 24 | } |
hollegha2 | 0:e0131e671ea0 | 25 | |
hollegha2 | 0:e0131e671ea0 | 26 | BertlDrive::BertlDrive(PinName pwm, PinName fwd, PinName rev, PinName encoder) : |
hollegha2 | 0:e0131e671ea0 | 27 | Motor(pwm,fwd,rev) , _enc(encoder) |
hollegha2 | 0:e0131e671ea0 | 28 | { |
hollegha2 | 0:e0131e671ea0 | 29 | encCnt = 0; |
hollegha2 | 0:e0131e671ea0 | 30 | } |
hollegha2 | 0:e0131e671ea0 | 31 | |
hollegha2 | 0:e0131e671ea0 | 32 | void BertlDrive::Init() |
hollegha2 | 0:e0131e671ea0 | 33 | { |
hollegha2 | 0:e0131e671ea0 | 34 | _enc.rise(this, &BertlDrive::EncoderISR); |
hollegha2 | 0:e0131e671ea0 | 35 | _enc.fall(this, &BertlDrive::EncoderISR); |
hollegha2 | 0:e0131e671ea0 | 36 | } |
hollegha2 | 0:e0131e671ea0 | 37 | |
hollegha2 | 0:e0131e671ea0 | 38 | void BertlDrive::EncoderISR() |
hollegha2 | 0:e0131e671ea0 | 39 | { |
hollegha2 | 0:e0131e671ea0 | 40 | encCnt++; |
hollegha2 | 0:e0131e671ea0 | 41 | } |
hollegha2 | 0:e0131e671ea0 | 42 | |
hollegha2 | 0:e0131e671ea0 | 43 | |
hollegha2 | 0:e0131e671ea0 | 44 | PortEx::PortEx() : |
hollegha2 | 0:e0131e671ea0 | 45 | _i2c(p28,p27), _p6Event(p6) |
hollegha2 | 0:e0131e671ea0 | 46 | { |
hollegha2 | 0:e0131e671ea0 | 47 | btns=btnEvent=0; |
hollegha2 | 0:e0131e671ea0 | 48 | useISR = 1; |
hollegha2 | 0:e0131e671ea0 | 49 | } |
hollegha2 | 0:e0131e671ea0 | 50 | |
hollegha2 | 0:e0131e671ea0 | 51 | void PortEx::Init() |
hollegha2 | 0:e0131e671ea0 | 52 | { |
hollegha2 | 0:e0131e671ea0 | 53 | char cmd[4]; |
hollegha2 | 0:e0131e671ea0 | 54 | _i2c.frequency(100000); |
hollegha2 | 0:e0131e671ea0 | 55 | wait(0.01); |
hollegha2 | 0:e0131e671ea0 | 56 | // Port0 Config Port0 Out Port1 In |
hollegha2 | 0:e0131e671ea0 | 57 | cmd[0]=0x06; cmd[1]=0x00; cmd[2]=0xFF; |
hollegha2 | 0:e0131e671ea0 | 58 | _i2c.write(DEV, cmd, 3, false); |
hollegha2 | 0:e0131e671ea0 | 59 | SetLedPort(0); |
hollegha2 | 0:e0131e671ea0 | 60 | _p6Event.fall(this, &PortEx::p6ISR); |
hollegha2 | 0:e0131e671ea0 | 61 | } |
hollegha2 | 0:e0131e671ea0 | 62 | |
hollegha2 | 0:e0131e671ea0 | 63 | void PortEx::p6ISR() |
hollegha2 | 0:e0131e671ea0 | 64 | { |
hollegha2 | 0:e0131e671ea0 | 65 | if( !useISR ) |
hollegha2 | 0:e0131e671ea0 | 66 | return; |
hollegha2 | 0:e0131e671ea0 | 67 | int16_t prev = btns; |
hollegha2 | 0:e0131e671ea0 | 68 | ReadButtons(); |
hollegha2 | 0:e0131e671ea0 | 69 | if( !btns ) |
hollegha2 | 0:e0131e671ea0 | 70 | btns = prev; |
hollegha2 | 0:e0131e671ea0 | 71 | else |
hollegha2 | 0:e0131e671ea0 | 72 | btnEvent = 1; |
hollegha2 | 0:e0131e671ea0 | 73 | } |
hollegha2 | 0:e0131e671ea0 | 74 | |
hollegha2 | 0:e0131e671ea0 | 75 | void PortEx::SetLedPort(uint8_t aBitPattern) |
hollegha2 | 0:e0131e671ea0 | 76 | { |
hollegha2 | 0:e0131e671ea0 | 77 | char cmd[4]; |
hollegha2 | 0:e0131e671ea0 | 78 | cmd[0]=2; cmd[1]=~aBitPattern; |
hollegha2 | 0:e0131e671ea0 | 79 | _i2c.write(DEV, cmd, 2, false); |
hollegha2 | 0:e0131e671ea0 | 80 | } |
hollegha2 | 0:e0131e671ea0 | 81 | |
hollegha2 | 0:e0131e671ea0 | 82 | void PortEx::SetLeds(uint8_t aBitPattern) |
hollegha2 | 0:e0131e671ea0 | 83 | { |
hollegha2 | 0:e0131e671ea0 | 84 | _currLeds |= aBitPattern; |
hollegha2 | 0:e0131e671ea0 | 85 | SetLedPort(_currLeds); |
hollegha2 | 0:e0131e671ea0 | 86 | } |
hollegha2 | 0:e0131e671ea0 | 87 | |
hollegha2 | 0:e0131e671ea0 | 88 | void PortEx::ToggleLeds(uint8_t aBitPattern) |
hollegha2 | 0:e0131e671ea0 | 89 | { |
hollegha2 | 0:e0131e671ea0 | 90 | _currLeds ^= aBitPattern; |
hollegha2 | 0:e0131e671ea0 | 91 | SetLedPort(_currLeds); |
hollegha2 | 0:e0131e671ea0 | 92 | } |
hollegha2 | 0:e0131e671ea0 | 93 | |
hollegha2 | 0:e0131e671ea0 | 94 | void PortEx::ClearLeds() |
hollegha2 | 0:e0131e671ea0 | 95 | { |
hollegha2 | 0:e0131e671ea0 | 96 | _currLeds=0; SetLedPort(0); |
hollegha2 | 0:e0131e671ea0 | 97 | } |
hollegha2 | 0:e0131e671ea0 | 98 | |
hollegha2 | 0:e0131e671ea0 | 99 | |
hollegha2 | 0:e0131e671ea0 | 100 | void PortEx::ReadButtons() |
hollegha2 | 0:e0131e671ea0 | 101 | { |
hollegha2 | 0:e0131e671ea0 | 102 | char cmd[4]; |
hollegha2 | 0:e0131e671ea0 | 103 | cmd[0]=1; |
hollegha2 | 0:e0131e671ea0 | 104 | _i2c.write(DEV, cmd, 1, true); |
hollegha2 | 0:e0131e671ea0 | 105 | _i2c.read(DEV|1, cmd, 1, false); |
hollegha2 | 0:e0131e671ea0 | 106 | btns = cmd[0]; |
hollegha2 | 0:e0131e671ea0 | 107 | } |
hollegha2 | 0:e0131e671ea0 | 108 | |
hollegha2 | 0:e0131e671ea0 | 109 | void PortEx::WaitUntilButtonPressed() |
hollegha2 | 0:e0131e671ea0 | 110 | { |
hollegha2 | 0:e0131e671ea0 | 111 | int prev = useISR; |
hollegha2 | 0:e0131e671ea0 | 112 | useISR = 0; |
hollegha2 | 0:e0131e671ea0 | 113 | btns = 0; |
hollegha2 | 0:e0131e671ea0 | 114 | while(1) { |
hollegha2 | 0:e0131e671ea0 | 115 | ReadButtons(); |
hollegha2 | 0:e0131e671ea0 | 116 | if( btns ) |
hollegha2 | 0:e0131e671ea0 | 117 | break; |
hollegha2 | 0:e0131e671ea0 | 118 | wait(0.01); |
hollegha2 | 0:e0131e671ea0 | 119 | } |
hollegha2 | 0:e0131e671ea0 | 120 | btns=btnEvent=0; |
hollegha2 | 0:e0131e671ea0 | 121 | useISR = prev; |
hollegha2 | 0:e0131e671ea0 | 122 | } |
hollegha2 | 0:e0131e671ea0 | 123 | |
hollegha2 | 0:e0131e671ea0 | 124 | |
hollegha2 | 0:e0131e671ea0 | 125 | |
hollegha2 | 0:e0131e671ea0 | 126 | UsDistSens::UsDistSens(PinName pinTrigger, PinName pinEcho) : |
hollegha2 | 0:e0131e671ea0 | 127 | trigger(pinTrigger),echo(pinEcho) |
hollegha2 | 0:e0131e671ea0 | 128 | { |
hollegha2 | 0:e0131e671ea0 | 129 | echo.rise(this, &UsDistSens::RisingISR); |
hollegha2 | 0:e0131e671ea0 | 130 | echo.fall(this, &UsDistSens::FallingISR); |
hollegha2 | 0:e0131e671ea0 | 131 | } |
hollegha2 | 0:e0131e671ea0 | 132 | |
hollegha2 | 0:e0131e671ea0 | 133 | void UsDistSens::StartMeas() |
hollegha2 | 0:e0131e671ea0 | 134 | { |
hollegha2 | 0:e0131e671ea0 | 135 | trigger=1; wait_us(12); trigger=0; |
hollegha2 | 0:e0131e671ea0 | 136 | stw.start(); |
hollegha2 | 0:e0131e671ea0 | 137 | } |
hollegha2 | 0:e0131e671ea0 | 138 | |
hollegha2 | 0:e0131e671ea0 | 139 | void UsDistSens::RisingISR() |
hollegha2 | 0:e0131e671ea0 | 140 | { stw.reset(); } |
hollegha2 | 0:e0131e671ea0 | 141 | |
hollegha2 | 0:e0131e671ea0 | 142 | void UsDistSens::FallingISR() |
hollegha2 | 0:e0131e671ea0 | 143 | { |
hollegha2 | 0:e0131e671ea0 | 144 | dist=stw.read_us(); |
hollegha2 | 0:e0131e671ea0 | 145 | distCM = (float)dist*(343.2E-4/2.0); |
hollegha2 | 0:e0131e671ea0 | 146 | } |
hollegha2 | 0:e0131e671ea0 | 147 | |
hollegha2 | 0:e0131e671ea0 | 148 | |
hollegha2 | 0:e0131e671ea0 | 149 |