added camera

code_midgen
u-siri-ous 2019-11-18 18:11:26 +01:00
parent 7cab3a2820
commit 3d012ac59f
6 changed files with 63 additions and 23 deletions

View File

@ -1,3 +1,4 @@
#pragma once
#include "data_source.h" #include "data_source.h"
class DataSourceBall : public DataSource{ class DataSourceBall : public DataSource{

View File

@ -1,3 +1,4 @@
#pragma once
#include "data_source.h" #include "data_source.h"
class DataSourceCamera : public DataSource{ class DataSourceCamera : public DataSource{
@ -6,10 +7,13 @@ class DataSourceCamera : public DataSource{
DataSourceCamera(HardwareSerial* ser, int baud); DataSourceCamera(HardwareSerial* ser, int baud);
void postProcess() override; void postProcess() override;
void test() override; void test() override;
void fixCamIMU(int); int fixCamIMU(int);
void readSensor() override;
int getValueAtk(bool);
int getValueDef(bool);
int goalOrientation, pAtk, pDef, imuOff, portx, valX, valY, valB, oldGoalX, oldGoalY, oldGoalB; int goalOrientation, pAtk, pDef, imuOff, portx, valX, valY, valB, oldGoalX, oldGoalY, oldGoalB;
int cameraready; int cameraReady;
char value; char value;
int startpY = 0; int startpY = 0;
int startpB = 0; int startpB = 0;

View File

@ -1,6 +1,7 @@
#include <Arduino.h> #include <Arduino.h>
#include "data_source_bno055.h" #include "data_source_bno055.h"
#include "data_source_ball.h" #include "data_source_ball.h"
#include "data_source_camera.h"
#include "motor.h" #include "motor.h"
#include "drivecontroller.h" #include "drivecontroller.h"

View File

@ -5,41 +5,39 @@ DataSourceCamera::DataSourceCamera(HardwareSerial* ser_, int baud) : DataSource(
void DataSourceCamera :: readSensor(){ void DataSourceCamera :: readSensor(){
portx = 999; portx = 999;
while(ser->available() > 0) value = ser->read(); while(ser->available() > 0) {
} value = ser->read();
void DataSourceCamera :: postProcess(){
// if the incoming character is a 'Y', set the start packet flag // if the incoming character is a 'Y', set the start packet flag
if (inChar == 'Y') { if (value == 'Y') {
startpY = 1; startpY = 1;
} }
// if the incoming character is a 'Y', set the start packet flag // if the incoming character is a 'Y', set the start packet flag
if (inChar == 'B') { if (value == 'B') {
startpB = 1; startpB = 1;
} }
// if the incoming character is a '.', set the end packet flag // if the incoming character is a '.', set the end packet flag
if (inChar == 'y') { if (value == 'y') {
endpY = 1; endpY = 1;
} }
// if the incoming character is a '.', set the end packet flag // if the incoming character is a '.', set the end packet flag
if (inChar == 'b') { if (value == 'b') {
endpB = 1; endpB = 1;
} }
if ((startpY == 1) && (endpY == 0)) { if ((startpY == 1) && (endpY == 0)) {
if (isDigit(inChar)) { if (isDigit(value)) {
// convert the incoming byte to a char and add it to the string: // convert the incoming byte to a char and add it to the string:
valStringY += inChar; valStringY += value;
}else if(inChar == '-'){ }else if(value == '-'){
negateY = true; negateY = true;
} }
} }
if ((startpB == 1) && (endpB == 0)) { if ((startpB == 1) && (endpB == 0)) {
if (isDigit(inChar)) { if (isDigit(value)) {
// convert the incoming byte to a char and add it to the string: // convert the incoming byte to a char and add it to the string:
valStringB += inChar; valStringB += value;
}else if(inChar == '-'){ }else if(value == '-'){
negateB = true; negateB = true;
} }
} }
@ -62,8 +60,10 @@ void DataSourceCamera :: postProcess(){
negateB = false; negateB = false;
datavalid ++; datavalid ++;
} }
}
}
} // end of while void DataSourceCamera :: postProcess(){
if (valY != -74) if (valY != -74)
oldGoalY = valY; oldGoalY = valY;
@ -77,7 +77,7 @@ void DataSourceCamera :: postProcess(){
// entro qui solo se ho ricevuto i pacchetti completi sia del blu che del giallo // entro qui solo se ho ricevuto i pacchetti completi sia del blu che del giallo
if (datavalid > 1 ) { if (datavalid > 1 ) {
if(goal_orientation == 1){ if(goalOrientation == 1){
//yellow goalpost //yellow goalpost
pAtk = valY; pAtk = valY;
pDef = valB * -1; pDef = valB * -1;
@ -89,6 +89,33 @@ void DataSourceCamera :: postProcess(){
datavalid = 0; datavalid = 0;
cameraReady = 1; //attivo flag di ricezione pacchetto cameraReady = 1; //attivo flag di ricezione pacchetto
}
}
int DataSourceCamera :: getValueAtk(bool fixed){
//attacco gialla
if(digitalRead(SWITCH_DX) == HIGH){
if(fixed) return fixCamIMU(valY);
return valY;
}
//attacco blu
if(digitalRead(SWITCH_DX) == LOW){
if(fixed) return fixCamIMU(valB);
return valB;
}
}
int DataSourceCamera :: getValueDef(bool fixed){
//difendo gialla
if(digitalRead(SWITCH_DX) == HIGH){
if(fixed) return fixCamIMU(valY);
return valY;
}
//difendo blu
if(digitalRead(SWITCH_DX) == LOW){
if(fixed) return fixCamIMU(valB);
return valB;
}
} }
void DataSourceCamera :: test(){ void DataSourceCamera :: test(){
@ -106,7 +133,7 @@ void DataSourceCamera :: test(){
delay(100); delay(100);
} }
void DataSourceCamera :: fixCamIMU(int d){ int DataSourceCamera :: fixCamIMU(int d){
if(compass->getValue() > 0 && compass->getValue() < 180) imuOff = compass->getValue(); if(compass->getValue() > 0 && compass->getValue() < 180) imuOff = compass->getValue();
else if (compass->getValue() <= 360 && compass->getValue() >= 180) imuOff = compass->getValue() - 360; else if (compass->getValue() <= 360 && compass->getValue() >= 180) imuOff = compass->getValue() - 360;
imuOff = constrain(imuOff*0.8, -30, 30); imuOff = constrain(imuOff*0.8, -30, 30);

View File

@ -17,6 +17,7 @@ void loop() {
updateSensors(); updateSensors();
//should recenter using predefined values //should recenter using predefined values
// drive->drive(0, 0, 0); // drive->drive(0, 0, 0);
compass->test(); //compass->test();
delay(100); camera->test();
delay(100);
} }

View File

@ -2,12 +2,18 @@
#include "sensors.h" #include "sensors.h"
void initSensors(){ void initSensors(){
compass = new DataSourceBNO055();
drive = new DriveController(new Motor(12, 11, 2, 45),new Motor(25, 24, 5, 135), new Motor(27, 26, 6, 225), new Motor(21, 22, 23, 315)); drive = new DriveController(new Motor(12, 11, 2, 45),new Motor(25, 24, 5, 135), new Motor(27, 26, 6, 225), new Motor(21, 22, 23, 315));
compass = new DataSourceBNO055();
ball = new DataSourceBall(&Serial4, 57600); ball = new DataSourceBall(&Serial4, 57600);
camera = new DataSourceCamera(&Serial2, 19200); camera = new DataSourceCamera(&Serial2, 19200);
} }
void updateSensors(){ void updateSensors(){
compass->readSensor();
pinMode(SWITCH_DX, INPUT);
pinMode(SWITCH_SX, INPUT);
compass->update();
ball->update();
camera->update();
} }