Error compiling, syntax should be better

code_midgen
EmaMaker 2019-11-27 15:33:13 +01:00
parent 076a14e3e6
commit 5fda4f890b
5 changed files with 57 additions and 79 deletions

View File

@ -10,7 +10,7 @@ class DataSource {
public: public:
DataSource(); DataSource();
DataSource(HardwareSerial*, int); DataSource(HardwareSerial*, int);
DataSource(TwoWire*); DataSource(TwoWire*, int addr);
DataSource(int, bool); DataSource(int, bool);
public: public:
@ -32,6 +32,7 @@ class DataSource {
HardwareSerial* ser; HardwareSerial* ser;
TwoWire* i2c; TwoWire* i2c;
int i2CAddr;
Protocols protocol; Protocols protocol;
int pin; int pin;

View File

@ -1,21 +1,21 @@
#pragma once #pragma once
#include "data_source.h" #include "data_source.h"
#define US_WAIT_TIME 70
class DataSourceUS : public DataSource{ class DataSourceUS : public DataSource{
public: public:
DataSourceUS(TwoWire* i2c_); DataSourceUS(TwoWire* i2c_, int addr);
void postProcess() override; void postProcess() override;
void test() override; void test() override;
void usMode(); void usMode();
void usTrigger(); void usTrigger();
void usReceive(); void usReceive();
//void readSensor() override; void readSensor() override;
int reading; int reading;
long us_t0; long us_t0;
long us_t1; long us_t1;
bool us_flag; bool us_flag;
int us_values[4];
int us_sx, us_dx, us_px, us_fr;
}; };

View File

@ -4,9 +4,12 @@ DataSource::DataSource(void){
protocol = P_NULL; protocol = P_NULL;
} }
DataSource::DataSource(TwoWire* i2c_){ DataSource::DataSource(TwoWire* i2c_, int addr){
protocol = P_I2C; protocol = P_I2C;
this->i2c = i2c_; this->i2c = i2c_;
this->i2CAddr = addr;
i2c->end();
i2c->begin(); i2c->begin();
} }

View File

@ -1,27 +1,23 @@
#include "data_source_us.h" #include "data_source_us.h"
#include "vars.h" #include "vars.h"
DataSourceUS :: DataSourceUS(TwoWire* i2c_) : DataSource(i2c_) { DataSourceUS::DataSourceUS(TwoWire* i2c_, int addr) : DataSource(i2c_, addr) { }
i2c->begin();
void DataSourceUS::test(){
readSensor();
delay(US_WAIT_TIME + 5);
readSensor();
DEBUG_PRINT.println(value);
} }
void DataSourceUS::readSensor(){
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) { if (us_flag == false) {
usTrigger(); usTrigger();
us_flag = true; us_flag = true;
us_t0 = millis(); us_t0 = millis();
} else { } else {
us_t1 = millis(); us_t1 = millis();
if ((us_t1 - us_t0) > 70) { if ((us_t1 - us_t0) > US_WAIT_TIME) {
usReceive(); usReceive();
us_flag = false; us_flag = false;
} }
@ -29,56 +25,34 @@ void DataSourceUS :: usMode(){
} }
void DataSourceUS::usTrigger() { void DataSourceUS::usTrigger() {
for (int i = 0; i < 4; i++){ // step 1: instruct sensor to read echoes
i2c->beginTransmission(112 + i); // transmit to device #112 (0x70)
i2c->beginTransmission(i2CAddr);
// sets register pointer to the command register (0x00)
i2c->write(byte(0x00)); i2c->write(byte(0x00));
// command sensor to measure in "centimeters" (0x51). 0x50 inches and 0x52
// microseconds
i2c->write(byte(0x51)); i2c->write(byte(0x51));
i2c->endTransmission(); i2c->endTransmission();
} }
}
void DataSourceUS::usReceive() { void DataSourceUS::usReceive() {
for (int i = 0; i < 4; i++){ // transmit to device #112s
i2c->beginTransmission(112 + i); i2c->beginTransmission(i2CAddr);
i2c->write(byte(0x00)); // sets register pointer to echo 1 register(0x02)
i2c->write(byte(0x51)); i2c->write(byte(0x02));
i2c->endTransmission(); 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 :: test(){ // step 4: request reading from sensor
for (int i = 0; i < 4; i++){ // request 2 bytes from slave device #112
i2c->beginTransmission(112 + i); i2c->requestFrom(i2CAddr, 2);
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];
//printing the result // step 5: receive reading from sensor
DEBUG_PRINT.println("---------------------"); // receive high byte (overwrites previous reading)
for (int i = 0; i < 4; i++) { reading = i2c->read();
DEBUG_PRINT.println(us_values[i]); // shift high byte to be high 8 bits
delay(250); reading = reading << 8;
} // receive low byte as lower 8 bit
DEBUG_PRINT.println("---------------------"); reading |= i2c->read();
value = reading;
} }

View File

@ -6,7 +6,7 @@ void initSensors(){
compass = new DataSourceBNO055(); compass = new DataSourceBNO055();
ball = new DataSourceBall(&Serial4, 57600); ball = new DataSourceBall(&Serial4, 57600);
camera = new DataSourceCamera(&Serial2, 19200); camera = new DataSourceCamera(&Serial2, 19200);
us = new DataSourceUS(&Wire1); us = new DataSourceUS(&Wire1, 112);
} }
void updateSensors(){ void updateSensors(){