Have a GP2Y1010 dust sensor module

This commit is contained in:
Martin Mueller
2010-10-23 21:03:12 +00:00
parent 8296d3a1b4
commit 135f2477cf
7 changed files with 259 additions and 2 deletions
+2 -1
View File
@@ -61,6 +61,8 @@
<!-- modules -->
<modules>
<load name="dust_gp2y.xml"/>
<!--load name="adc_generic.xml"/>
<load name="alt_srf08.xml"/>
<load name="mag_micromag_fw.xml"/>
<load name="baro_bmp.xml"/>
@@ -73,7 +75,6 @@
<load name="humid_dpicco.xml"/>
<load name="humid_sht.xml"/>
<load name="baro_MS5534A.xml"/>
<!--load name="adc_generic.xml"/>
<load name="baro_scp_i2c.xml"/-->
</modules>
+5 -1
View File
@@ -634,7 +634,11 @@
<field name="f_light" type="float" unit="percent" format="%.2f"/>
</message>
<!-- 98 is free -->
<message name="GP2Y_STATUS" id="98">
<field name="idensity" type="uint16"/>
<field name="density" type="float" unit="mg/m3" format="%.2f"/>
</message>
<!-- 99 is free -->
<message name="PPM" id="100">
+14
View File
@@ -0,0 +1,14 @@
<!DOCTYPE module SYSTEM "module.dtd">
<module name="dust_gp2y" dir="meteo">
<header>
<file name="dust_gp2y.h"/>
</header>
<init fun="dust_gp2y_init()"/>
<periodic fun="dust_gp2y_periodic()" freq="4" delay="1"/>
<event fun="dust_gp2y_event()"/>
<makefile target="ap">
<file name="dust_gp2y.c"/>
</makefile>
</module>
+2
View File
@@ -0,0 +1,2 @@
This folder contains software that runs in separate on-board processors that are not
part of the main autopilot.
@@ -0,0 +1,136 @@
/*
* $Id$
*
* Copyright (C) 2010 Martin Mueller, <martinmm@pfump.org>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/* I2C interface for Sharp GP2Y1010AU optical dust sensor */
#include <Wire.h>
/* infrared LED pin PB1, high active */
#define IR_FLASH_PIN 9
/* adc_in pin ADC0 */
#define ADC_PIN 0
/* green LED pin PB5 (on arduino pro mini) */
#define LED_GR_PIN 13
#define GP2Y_I2C_ADDR 0x76
#define AVG_NB_SAMPLE 25
struct adc_buf {
unsigned long sum;
unsigned short values[AVG_NB_SAMPLE];
unsigned char head;
};
struct adc_buf buf;
unsigned char i2c_out[2];
unsigned long time_us;
unsigned long time_ms;
unsigned long trig_next;
unsigned long cnt;
unsigned short adc_val;
unsigned char new_head;
ISR(ADC_vect) {
adc_val = ADCL;
adc_val |= ADCH << 8;
new_head = buf.head + 1;
if (new_head >= AVG_NB_SAMPLE)
new_head=0;
buf.sum -= buf.values[new_head];
buf.values[new_head] = adc_val;
buf.sum += adc_val;
buf.head = new_head;
}
void read_i2c() {
i2c_out[0] = ((unsigned int)(buf.sum / AVG_NB_SAMPLE) >> 8) & 0xFF;
i2c_out[1] = (unsigned int)(buf.sum / AVG_NB_SAMPLE) & 0xFF;
Wire.send(i2c_out, 2);
}
void setup() {
/* I2C init */
Wire.begin(GP2Y_I2C_ADDR);
Wire.onRequest(read_i2c);
/* green LED init */
digitalWrite(LED_GR_PIN, LOW);
pinMode(LED_GR_PIN, OUTPUT);
/* infrared LED switch init */
digitalWrite(IR_FLASH_PIN, HIGH);
pinMode(IR_FLASH_PIN, OUTPUT);
/* init adc, default reference */
ADCSRA |= (1<<ADIE)|(1<<ADEN);
ADCSRA |= (1<<ADSC);
ADMUX = (DEFAULT << 6) | (ADC_PIN & 0x07);
trig_next = millis() + 10;
cnt = 0;
}
void loop() {
if (millis() >= trig_next) {
trig_next += 10;
noInterrupts();
time_us = micros();
/* flash the infrared, LED on */
digitalWrite(IR_FLASH_PIN, HIGH);
/* delay 0.28ms for reflection */
while((micros() - time_us) < 280);
/* start conversion */
ADCSRA |= 1 << ADSC;
/* fill delay to 0.32ms for end of reflection */
while((micros() - time_us) < 320);
/* infrared LED off */
digitalWrite(IR_FLASH_PIN, LOW);
interrupts();
#if 0
/* blink green LED */
if (cnt++ > 90) {
digitalWrite(LED_GR_PIN, HIGH);
if (cnt > 100) {
digitalWrite(LED_GR_PIN, LOW);
cnt = 0;
}
}
#endif
}
}
+82
View File
@@ -0,0 +1,82 @@
/*
* $Id$
*
* Copyright (C) 2010 Martin Mueller
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
/** \file dust_gp2y.c
* \brief Sharp GP2Y1010AU dust sensor interface
*
* This reads the values for dust density from the Sharp GP2Y1010AU0F sensor
* through I2C (needs I2C ADC at the sensor).
*/
#include "dust_gp2y.h"
#include "i2c.h"
#include "sys_time.h"
#include "uart.h"
#include "messages.h"
#include "downlink.h"
uint8_t dust_gp2y_status;
uint16_t dust_gp2y_density;
float dust_gp2y_density_f;
struct i2c_transaction gp2y_trans;
#ifndef GP2Y_I2C_DEV
#define GP2Y_I2C_DEV i2c0
#endif
#define GP2Y_SLAVE_ADDR 0xED
void dust_gp2y_init( void ) {
dust_gp2y_status = DUST_GP2Y_UNINIT;
}
void dust_gp2y_periodic( void ) {
if (dust_gp2y_status == DUST_GP2Y_IDLE) {
I2CReceive(GP2Y_I2C_DEV, gp2y_trans, GP2Y_SLAVE_ADDR, 2);
}
else if (dust_gp2y_status == DUST_GP2Y_UNINIT && cpu_time_sec > 1) {
dust_gp2y_status = DUST_GP2Y_IDLE;
}
}
void dust_gp2y_event( void ) {
if (gp2y_trans.status == I2CTransSuccess) {
/* read two byte particle density */
dust_gp2y_density = gp2y_trans.buf[0] << 8;
dust_gp2y_density |= gp2y_trans.buf[1];
/* "just for reference and not for guarantee" */
dust_gp2y_density_f = ((dust_gp2y_density / 1024.) * 3.3 * (51. / 33.) - 0.6) * (0.5 / 3.);
if (dust_gp2y_density_f < 0)
dust_gp2y_density_f = 0;
DOWNLINK_SEND_GP2Y_STATUS(DefaultChannel, &dust_gp2y_density, &dust_gp2y_density_f);
gp2y_trans.status = I2CTransDone;
}
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef DUST_GP2Y_H
#define DUST_GP2Y_H
#include "std.h"
#define DUST_GP2Y_UNINIT 0
#define DUST_GP2Y_IDLE 1
extern uint8_t dust_gp2y_status;
extern uint16_t dust_gp2y_density;
extern float dust_gp2y_density_f;
void dust_gp2y_init(void);
void dust_gp2y_periodic(void);
void dust_gp2y_event(void);
#endif