ESC Brazil 2012 Demo

Dependencies:   mbed ID12RFID NokiaLCD

Committer:
anaran
Date:
Wed Jun 20 15:35:12 2012 +0000
Revision:
0:4fe41724cceb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
anaran 0:4fe41724cceb 1
anaran 0:4fe41724cceb 2 /*
anaran 0:4fe41724cceb 3 Copyright (c) 2010 Chris Styles ( chris dot styles at mbed dot org )
anaran 0:4fe41724cceb 4
anaran 0:4fe41724cceb 5 Permission is hereby granted, free of charge, to any person obtaining a copy
anaran 0:4fe41724cceb 6 of this software and associated documentation files (the "Software"), to deal
anaran 0:4fe41724cceb 7 in the Software without restriction, including without limitation the rights
anaran 0:4fe41724cceb 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
anaran 0:4fe41724cceb 9 copies of the Software, and to permit persons to whom the Software is
anaran 0:4fe41724cceb 10 furnished to do so, subject to the following conditions:
anaran 0:4fe41724cceb 11
anaran 0:4fe41724cceb 12 The above copyright notice and this permission notice shall be included in
anaran 0:4fe41724cceb 13 all copies or substantial portions of the Software.
anaran 0:4fe41724cceb 14
anaran 0:4fe41724cceb 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
anaran 0:4fe41724cceb 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
anaran 0:4fe41724cceb 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
anaran 0:4fe41724cceb 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
anaran 0:4fe41724cceb 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
anaran 0:4fe41724cceb 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
anaran 0:4fe41724cceb 21 THE SOFTWARE.
anaran 0:4fe41724cceb 22 */
anaran 0:4fe41724cceb 23
anaran 0:4fe41724cceb 24 #include "SRF08.h"
anaran 0:4fe41724cceb 25
anaran 0:4fe41724cceb 26
anaran 0:4fe41724cceb 27 SRF08::SRF08(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
anaran 0:4fe41724cceb 28 char cmd[2];
anaran 0:4fe41724cceb 29
anaran 0:4fe41724cceb 30 // Set up SRF08 max range and receiver sensitivity over I2C bus
anaran 0:4fe41724cceb 31 cmd[0] = 0x02; // Range register
anaran 0:4fe41724cceb 32 cmd[1] = 0x1C; // Set max range about 100cm
anaran 0:4fe41724cceb 33 m_i2c.write(m_addr, cmd, 2);
anaran 0:4fe41724cceb 34 cmd[0] = 0x01; // Receiver gain register
anaran 0:4fe41724cceb 35 cmd[1] = 0x1B; // Set max receiver gain
anaran 0:4fe41724cceb 36 m_i2c.write(m_addr, cmd, 2);
anaran 0:4fe41724cceb 37
anaran 0:4fe41724cceb 38 }
anaran 0:4fe41724cceb 39
anaran 0:4fe41724cceb 40 SRF08::~SRF08() {
anaran 0:4fe41724cceb 41
anaran 0:4fe41724cceb 42 }
anaran 0:4fe41724cceb 43
anaran 0:4fe41724cceb 44 float SRF08::read() {
anaran 0:4fe41724cceb 45
anaran 0:4fe41724cceb 46 char cmd[2];
anaran 0:4fe41724cceb 47 char echo[2];
anaran 0:4fe41724cceb 48
anaran 0:4fe41724cceb 49
anaran 0:4fe41724cceb 50 // Get range data from SRF08
anaran 0:4fe41724cceb 51 // Send Tx burst command over I2C bus
anaran 0:4fe41724cceb 52 cmd[0] = 0x00; // Command register
anaran 0:4fe41724cceb 53 cmd[1] = 0x51; // Ranging results in cm
anaran 0:4fe41724cceb 54 m_i2c.write(m_addr, cmd, 2); // Send ranging burst
anaran 0:4fe41724cceb 55
anaran 0:4fe41724cceb 56 wait(0.07); // Wait for return echo
anaran 0:4fe41724cceb 57
anaran 0:4fe41724cceb 58 // Read back range over I2C bus
anaran 0:4fe41724cceb 59 cmd[0] = 0x02; // Address of first echo
anaran 0:4fe41724cceb 60 m_i2c.write(m_addr, cmd, 1, 1); // Send address of first echo
anaran 0:4fe41724cceb 61 m_i2c.read(m_addr, echo, 2); // Read two-byte echo result
anaran 0:4fe41724cceb 62
anaran 0:4fe41724cceb 63 // Generate PWM mark/space ratio from range data
anaran 0:4fe41724cceb 64 float range = (echo[0]<<8)+echo[1];
anaran 0:4fe41724cceb 65
anaran 0:4fe41724cceb 66 return range;
anaran 0:4fe41724cceb 67 }