Revision: 428
Author: sebastienlelong
Date: Sat Mar 2 01:58:33 2013
Log: Maxbotix EZ1 sonar, first draft
http://code.google.com/p/jaluino/source/detail?r=428
Added:
/trunk/lib/sonar_ez1.jal
/trunk/samples/jaluino_bee_sonar_ez1.jal
=======================================
--- /dev/null
+++ /trunk/lib/sonar_ez1.jal Sat Mar 2 01:58:33 2013
@@ -0,0 +1,46 @@
+-- Title: Maxbotix EZ1 ultra-sonic sensor library:
+-- Author: Sebastien Lelong, Copyright (c) 2008..2009, all rights reserved.
+-- Adapted-by:
+-- Compiler: 2.4p
+-- Revision: $Revision: 412 $
+--
+-- This file is part of jaluino (
http://jaluino.googlecode.com)
+-- Released under the BSD license
(
http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description:
+-- * only readings through ADC is implemented. Serial and PWM modes
aren't.
+-- * There's no check about timeout on PW pin whn no object is detected
+-- (pin is high for more than 37.5ms)
+--
+-- Sources:
+--
+-- Notes:
+--
+
+const byte SONAR_EZ1_5V = 5
+const byte SONAR_EZ1_3V = 3
+const byte SONAR_EZ1_MV10_PER_INCH_5V = 98
+const byte SONAR_EZ1_MV10_PER_INCH_3V3 = 64
+
+-- init procedure, mostly about timing init. If multiple sonars
+-- are used, there's no need to call it more than one time
+procedure sonar_ez1_init() is
+ pragma inline
+ delay_1ms(250) -- after powerup, sonar will be ready after few ms
+end procedure
+
+-- read distance using passed ADC channel number. Return distance in inches
+function sonar_ez1_read(byte in adcchan) return word is
+ var word dist_value = adc_read_high_res(adcchan) -- read on user
defined ADC channel
+ -- distance isn't the same depending on power supply
+ if SONAR_EZ1_POWER_SUPPLY == 5 then
+ -- 5V: ~9.8mV/in
+ var word tmp = word(dword(dist_value) * 10000 * SONAR_EZ1_5V /
SONAR_EZ1_MAX_ADC / SONAR_EZ1_MV10_PER_INCH_5V)
+ return tmp
+ else
+ -- 3V3: ~6.4mV/in
+ var word tmp = word(dword(dist_value) * 10000 * SONAR_EZ1_5V /
SONAR_EZ1_MAX_ADC / SONAR_EZ1_MV10_PER_INCH_3V3)
+ return tmp
+ end if
+end function
+
=======================================
--- /dev/null
+++ /trunk/samples/jaluino_bee_sonar_ez1.jal Sat Mar 2 01:58:33 2013
@@ -0,0 +1,62 @@
+-- Title: Maxbotix EZ1 ultrasonic sensor sample for Jaluino Bee
+-- Author: Sebastien Lelong, Copyright (c) 2008..2013, all rights reserved.
+-- Adapted-by:
+-- Compiler: 2.4p
+--
+-- This file is part of jaluino (
http://jaluino.googlecode.com)
+-- Released under the BSD license
(
http://www.opensource.org/licenses/bsd-license.php)
+--
+-- Description: This program shows how to read data from Maxbotix
ultrasonic EZ1 sensor.
+-- It's done through ADC readings.
+--
+
+
+include jaluino_bee
+include delay
+
+onboard_led_direction = output
+
+-- we'll print results through serial
+const SERIAL_HW_BAUDRATE = 9600
+include serial_hardware
+serial_hw_init()
+
+include print
+
+const byte welcome[] = "-- Maxbotix EZ1 sensor sample --"
+const byte str_dist[] = "Distance value: "
+
+-- Configure ADC
+const byte ADC_NVREF = ADC_NO_EXT_VREF
+const word ADC_RSOURCE = 2_000
+include adc
+adc_init()
+set_analog_pin(9) -- RAB3AN*9*
+
+-- EZ1 can be powered @5V or @3V3. If 5V, set this is constant to 5, else
3.
+const byte SONAR_EZ1_POWER_SUPPLY = 3
+-- Readings will be done through ADC. What is the max value ADC can read ?
+-- (depends on ADC resolution, either: 256, 1024, 2048)
+const word SONAR_EZ1_MAX_ADC = 2048
+include sonar_ez1
+sonar_ez1_init()
+
+print_string(serial_hw_data,welcome)
+print_crlf(serial_hw_data)
+print_crlf(serial_hw_data)
+
+var word dist_value = 0
+forever loop
+
+ onboard_led = on
+ delay_1ms(20)
+ dist_value = sonar_ez1_read(9)
+ print_string(serial_hw_data,str_dist)
+ print_word_dec(serial_hw_data,dist_value)
+ print_crlf(serial_hw_data)
+ onboard_led = off
+
+
+end loop
+
+--