Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color wheel dev #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3913355
initial commit.
erinleonello Feb 6, 2020
03e6a3d
adding rotate to color method
greamy Feb 6, 2020
0eafde2
vaughan, comment out code that won't compile
greamy Feb 8, 2020
74374bd
Added comments to use ColorMatch class
greamy Feb 8, 2020
a361fb9
vaughan - added color matcher code to determine if we are on Red.
greamy Feb 10, 2020
b4d59ac
Vaughan - removed all non color wheel code
slvaughan Feb 12, 2020
1239803
Finishing rotoate to color.
schelcc Feb 13, 2020
cfa311d
Finishing rotoate to color.
erinleonello Feb 13, 2020
cc6d40b
Merge branch 'ColorWheelDev' of https://github.com/Dreadbot/Dreadbots…
schelcc Feb 13, 2020
52d2345
does not compile, trying to fix errors
erinleonello Feb 13, 2020
e43de37
schelcc Feb 18, 2020
6f87b43
color wheel code by Maura and Erin
schelcc Feb 18, 2020
6408eb0
color wheel code by Maura and Erin
schelcc Feb 18, 2020
d0274ef
added print color by Maura and Erin
schelcc Feb 18, 2020
cb104a8
erin - Finished rotate to number
greamy Feb 20, 2020
7a8b50b
erin - added rotate to color code
greamy Feb 20, 2020
b90eb9b
Vaughan, added smart dashboard calls
slvaughan Feb 22, 2020
eb9e4fc
erin and maura Check for correct button and activate actuator
greamy Feb 24, 2020
4296c13
erin and maura speed and sensor changes
greamy Feb 24, 2020
91eac59
maura and erin changed initialization of variables
greamy Feb 28, 2020
fca2ff1
erin and maura solenoid controls and confidence
greamy Feb 29, 2020
dcdf634
erin does not build work in progress
schelcc Mar 3, 2020
f41cb23
erin - builds but not tested
schelcc Mar 5, 2020
96e8709
erin - tested but doesn't work
schelcc Mar 5, 2020
204ee9f
erin - chaged motor identifiers
greamy Mar 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
249 changes: 218 additions & 31 deletions robot/src/main/cpp/ColorWheel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc/Joystick.h>
#include <ColorWheel.h>

#include <iostream>
#include <string>
#include <RobotUtilities.h>
using namespace std;

enum WheelState{
NotSpinning,
Expand All @@ -9,57 +13,240 @@ enum WheelState{
};
WheelState spinState = WheelState::NotSpinning;

frc::Color ColorState;
frc::Color CurrentColor;

frc::Color PreviousColor;

int NumSpins = 0;

ColorWheel::ColorWheel(){
//Add color match code here
bool OnRed = false;

double ColorConfidenceTarget = 0.9;

int NumColorSamples = 0;

int CurrentButton = 0;

// Joystick2Layout deployColorWheelButton = Joystick2Layout::kDeployColorWheelButton;

//Joystick2Layout RotateToNumberButton = Joystick2Layout::kColorWheelRotationControl;

//Joystick2Layout RotateToColorButton = Joystick2Layout::kColorWheelColorControl;


static constexpr auto i2cPort = frc::I2C::Port::kOnboard;
rev::ColorSensorV3 m_colorSensor(i2cPort);
rev::ColorMatch m_colorMatcher;
WPI_TalonSRX *colormotor;
frc::Joystick *colorjoystick;
frc::Solenoid *colorsolenoid;

ColorWheel::ColorWheel(WPI_TalonSRX *motor, frc::Joystick *joystick, frc::Solenoid *solenoid){
m_colorMatcher.AddColorMatch(kBlueTarget);
m_colorMatcher.AddColorMatch(kGreenTarget);
m_colorMatcher.AddColorMatch(kRedTarget);
m_colorMatcher.AddColorMatch(kYellowTarget);
colormotor = motor;
colorjoystick = joystick;
colorsolenoid = solenoid;
}

frc::Color ColorWheel::getPreviousColor(frc::Color color){
//Color order: Red, Yellow, Blue, Green
if (color == kGreenTarget)
{
return kBlueTarget;
}
else if (color == kBlueTarget)
{
return kYellowTarget;
}
else if (color == kYellowTarget)
{
return kRedTarget;
}
else if (color == kRedTarget)
{
return kGreenTarget;
}
}
frc::Color ColorWheel::getNextColor(frc::Color color){
if (color == kGreenTarget)
{
return kRedTarget;
}
else if (color == kBlueTarget)
{
return kGreenTarget;
}
else if (color == kYellowTarget)
{
return kBlueTarget;
}
else if (color == kRedTarget)
{
return kYellowTarget;
}
}
frc::Color ColorWheel::getSpinTargetColor(frc::Color color){
if (color == kGreenTarget)
{
return kYellowTarget;
}
else if (color == kBlueTarget)
{
return kRedTarget;
}
else if (color == kYellowTarget)
{
return kGreenTarget;
}
else if (color == kRedTarget)
{
return kBlueTarget;
}

}
void ColorWheel::ControlSolenoid(){
if (colorjoystick->GetRawButtonPressed(1)){
cout <<"button 1 pressed"<< endl;
bool isup = colorsolenoid->Get();

if(isup == true){
colorsolenoid->Set(false);
}
else{
colorsolenoid->Set(true);
}
}

}

void ColorWheel::RotateToNumber(WPI_TalonSRX *motor, frc::Joystick *joystick, rev::ColorSensorV3 *sensor){
if (spinState == WheelState::NotSpinning && joystick->GetRawButton(1))

void ColorWheel::RotateToNumber(){
if (spinState == WheelState::NotSpinning && colorjoystick->GetRawButtonPressed (2))
{
cout <<"button 2 pressed"<< endl;
spinState = WheelState::InitSpinning;
CurrentButton = 1;

}
if (spinState == WheelState::InitSpinning)
if (spinState == WheelState::InitSpinning && CurrentButton == 1)
{
NumSpins = 0;
motor->Set(ControlMode::PercentOutput,0.2);
colormotor->Set(ControlMode::PercentOutput,0.7);
spinState = WheelState::Spinning;
}
if (spinState == WheelState::Spinning)
{
if (joystick->GetRawButton(1) || NumSpins>6)
if (spinState == WheelState::Spinning && CurrentButton == 1)
{
if (colorjoystick->GetRawButtonPressed(2) || NumSpins>7)
{
motor->Set(ControlMode::PercentOutput,0.0);
colormotor->Set(ControlMode::PercentOutput,0.0);
spinState = WheelState::NotSpinning;
CurrentButton = 0;
return;

}
if (sensor->GetColor()== kRedTarget){
NumSpins = NumSpins+1;

//A value betwen 0 and 1, 1 being absolute perfect color match
double colorConfidence = 0.0;
frc::Color detectedColor = m_colorSensor.GetColor();
frc::Color matchedColor = m_colorMatcher.MatchClosestColor(detectedColor, colorConfidence);
PrintColor(matchedColor, colorConfidence);
//do we need to check confidence number in this condition to see how confident the color matcher
//thinks the color is red? A value close to one means more confident.

if (matchedColor == kRedTarget && OnRed == false && colorConfidence >= ColorConfidenceTarget){
NumSpins = NumSpins+1;
OnRed = true;
}
else if (!(matchedColor == kRedTarget)) {
OnRed = false;
}
}
}

/*ColorWheel::ColorWheel(WPI_TalonSRX *colorMotor)
{
m_colorMotor = colorMotor;
m_colorSensor = new rev::ColorSensorV3(frc::I2C::Port::kOnboard);

m_colorMatch->AddColorMatch(kBlueTarget);
m_colorMatch->AddColorMatch(kRedTarget);
m_colorMatch->AddColorMatch(kYellowTarget);
m_colorMatch->AddColorMatch(kGreenTarget);
}
frc::Color ColorWheel::GetCurrentColor(){
return m_colorMatch->MatchClosestColor(m_colorSensor->GetColor(), confidence);
void ColorWheel::RotateToColor(frc::Color *targetcolor){
double colorConfidence = 0.0;
frc::Color spinTargetColor = getSpinTargetColor(*targetcolor);
frc::Color detectedColor = m_colorSensor.GetColor();
frc::Color matchedColor = m_colorMatcher.MatchClosestColor(detectedColor, colorConfidence);
frc::SmartDashboard::PutNumber("SpinState", spinState);
if (spinState == WheelState::NotSpinning && colorjoystick->GetRawButton(3))
{
spinState = WheelState::InitSpinning;
CurrentButton = 2;
cout <<"button 3 pressed"<< endl;
}
if (spinState == WheelState::InitSpinning && CurrentButton == 2)
{
spinState = WheelState::Spinning;
colormotor->Set(ControlMode::PercentOutput, 0.2);
//*********
// check to make sure we're getting a unique copy of matched color
//*********
PreviousColor = getPreviousColor(matchedColor);
CurrentColor = matchedColor;
}
if (spinState == WheelState::Spinning && CurrentButton == 2)
{
PrintColor(matchedColor, colorConfidence);
if (!(matchedColor == CurrentColor))
{
if (matchedColor == getNextColor(PreviousColor))
{
if (matchedColor == spinTargetColor && colorConfidence >= ColorConfidenceTarget)
{
if (NumColorSamples > 1)
{
spinState = WheelState::NotSpinning;
colormotor->Set(ControlMode::PercentOutput, 0.0);
NumColorSamples = 0;
CurrentButton = 0;
}
else
{
NumColorSamples += 1;
}
}
else
{
PreviousColor = getPreviousColor(matchedColor);
CurrentColor = matchedColor;
}
}
}

}
}
void ColorWheel::TurnToColor(frc::Color targetColor){
if(targetColor == GetCurrentColor()){}
else{
m_colorMotor->Set(ControlMode::PercentOutput, .1);
}
}*/

void ColorWheel::PrintColor(frc::Color color, double colorConfidence){
if(color == kBlueTarget){
frc::SmartDashboard::PutString("color","Blue");
cout << "Blue\t" << colorConfidence << endl;
}
else if(color == kRedTarget){
frc::SmartDashboard::PutString("color","Red");
cout << "Red\t" << colorConfidence << endl;
}
else if(color == kYellowTarget){
frc::SmartDashboard::PutString("color","Yellow");
cout << "Yellow\t" << colorConfidence << endl;
}
else if(color == kGreenTarget){
frc::SmartDashboard::PutString("color","Green");
cout << "Green\t" << colorConfidence << endl;
}
else{
frc::SmartDashboard::PutString("color","No color detected");
cout << "no color detected" << endl;
}
frc::SmartDashboard::PutNumber("NumSpins", NumSpins);
cout << "Green\t" << colorConfidence << endl;
cout << "Yellow\t" << colorConfidence << endl;
frc::SmartDashboard::PutNumber("Confidence", colorConfidence);
frc::SmartDashboard::PutNumber("SpinState", spinState);

}

Loading