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:
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;

View File

@ -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;
};

View File

@ -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();
}

View File

@ -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("---------------------");
}
// 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;
}

View File

@ -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(){