From 5fda4f890b461c6c30e6c1b795c6b4b4201df32b Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Wed, 27 Nov 2019 15:33:13 +0100 Subject: [PATCH] Error compiling, syntax should be better --- include/data_source.h | 3 +- include/data_source_us.h | 10 ++-- src/data_source.cpp | 5 +- src/data_source_us.cpp | 116 +++++++++++++++------------------------ src/sensors.cpp | 2 +- 5 files changed, 57 insertions(+), 79 deletions(-) diff --git a/include/data_source.h b/include/data_source.h index 6952b68..d437c2e 100644 --- a/include/data_source.h +++ b/include/data_source.h @@ -10,7 +10,7 @@ class DataSource { public: DataSource(); DataSource(HardwareSerial*, int); - DataSource(TwoWire*); + DataSource(TwoWire*, int addr); DataSource(int, bool); public: @@ -32,6 +32,7 @@ class DataSource { HardwareSerial* ser; TwoWire* i2c; + int i2CAddr; Protocols protocol; int pin; diff --git a/include/data_source_us.h b/include/data_source_us.h index 6ba8788..3eabc9f 100644 --- a/include/data_source_us.h +++ b/include/data_source_us.h @@ -1,21 +1,21 @@ #pragma once #include "data_source.h" +#define US_WAIT_TIME 70 + class DataSourceUS : public DataSource{ public: - DataSourceUS(TwoWire* i2c_); + DataSourceUS(TwoWire* i2c_, int addr); void postProcess() override; void test() override; void usMode(); void usTrigger(); void usReceive(); - //void readSensor() override; + void readSensor() override; int reading; long us_t0; long us_t1; - bool us_flag; - int us_values[4]; - int us_sx, us_dx, us_px, us_fr; + bool us_flag; }; \ No newline at end of file diff --git a/src/data_source.cpp b/src/data_source.cpp index 56535c9..3e9d73f 100644 --- a/src/data_source.cpp +++ b/src/data_source.cpp @@ -4,9 +4,12 @@ DataSource::DataSource(void){ protocol = P_NULL; } -DataSource::DataSource(TwoWire* i2c_){ +DataSource::DataSource(TwoWire* i2c_, int addr){ protocol = P_I2C; this->i2c = i2c_; + this->i2CAddr = addr; + + i2c->end(); i2c->begin(); } diff --git a/src/data_source_us.cpp b/src/data_source_us.cpp index 5bc1d8a..d8adf6a 100644 --- a/src/data_source_us.cpp +++ b/src/data_source_us.cpp @@ -1,84 +1,58 @@ #include "data_source_us.h" #include "vars.h" -DataSourceUS :: DataSourceUS(TwoWire* i2c_) : DataSource(i2c_) { - i2c->begin(); +DataSourceUS::DataSourceUS(TwoWire* i2c_, int addr) : DataSource(i2c_, addr) { } + +void DataSourceUS::test(){ + readSensor(); + delay(US_WAIT_TIME + 5); + readSensor(); + DEBUG_PRINT.println(value); } - -void DataSourceUS :: postProcess(){ - usMode(); - us_fr = us_values[0]; // FRONT US - us_dx = us_values[1]; // DX US - us_px = us_values[2]; // BACK US - us_sx = us_values[3]; // SX US -} - -void DataSourceUS :: usMode(){ - if (us_flag == false) { - usTrigger(); - us_flag = true; - us_t0 = millis(); - } else { - us_t1 = millis(); - if ((us_t1 - us_t0) > 70) { - usReceive(); - us_flag = false; +void DataSourceUS::readSensor(){ + if (us_flag == false) { + usTrigger(); + us_flag = true; + us_t0 = millis(); + } else { + us_t1 = millis(); + if ((us_t1 - us_t0) > US_WAIT_TIME) { + usReceive(); + us_flag = false; } } } -void DataSourceUS :: usTrigger(){ - for (int i = 0; i < 4; i++){ - i2c->beginTransmission(112 + i); - i2c->write(byte(0x00)); - i2c->write(byte(0x51)); - i2c->endTransmission(); - } +void DataSourceUS::usTrigger() { + // step 1: instruct sensor to read echoes + // transmit to device #112 (0x70) + i2c->beginTransmission(i2CAddr); + // sets register pointer to the command register (0x00) + i2c->write(byte(0x00)); + // command sensor to measure in "centimeters" (0x51). 0x50 inches and 0x52 + // microseconds + i2c->write(byte(0x51)); + i2c->endTransmission(); } -void DataSourceUS :: usReceive(){ - for (int i = 0; i < 4; i++){ - i2c->beginTransmission(112 + i); - i2c->write(byte(0x00)); - i2c->write(byte(0x51)); - i2c->endTransmission(); - delay(70); - i2c->requestFrom(112 + i, 2); - if(2 <= i2c->available()){ - reading = i2c->read(); - reading = reading << 8; - reading |= i2c->read(); - us_values[i] = reading; - } - } -} +void DataSourceUS::usReceive() { + // transmit to device #112s + i2c->beginTransmission(i2CAddr); + // sets register pointer to echo 1 register(0x02) + i2c->write(byte(0x02)); + i2c->endTransmission(); -void DataSourceUS :: test(){ - for (int i = 0; i < 4; i++){ - i2c->beginTransmission(112 + i); - i2c->write(byte(0x00)); - i2c->write(byte(0x51)); - i2c->endTransmission(); - delay(70); - i2c->requestFrom(112 + i, 2); - if(2 <= i2c->available()){ - reading = i2c->read(); - reading = reading << 8; - reading |= i2c->read(); - us_values[i] = reading; - } - } - us_fr = us_values[0]; - us_dx = us_values[1]; - us_px = us_values[2]; - us_sx = us_values[3]; + // step 4: request reading from sensor + // request 2 bytes from slave device #112 + i2c->requestFrom(i2CAddr, 2); - //printing the result - DEBUG_PRINT.println("---------------------"); - for (int i = 0; i < 4; i++) { - DEBUG_PRINT.println(us_values[i]); - delay(250); - } - DEBUG_PRINT.println("---------------------"); -} \ No newline at end of file + // step 5: receive reading from sensor + // receive high byte (overwrites previous reading) + reading = i2c->read(); + // shift high byte to be high 8 bits + reading = reading << 8; + // receive low byte as lower 8 bit + reading |= i2c->read(); + value = reading; + } \ No newline at end of file diff --git a/src/sensors.cpp b/src/sensors.cpp index 08f878f..f5f6e64 100644 --- a/src/sensors.cpp +++ b/src/sensors.cpp @@ -6,7 +6,7 @@ void initSensors(){ compass = new DataSourceBNO055(); ball = new DataSourceBall(&Serial4, 57600); camera = new DataSourceCamera(&Serial2, 19200); - us = new DataSourceUS(&Wire1); + us = new DataSourceUS(&Wire1, 112); } void updateSensors(){