Pictures, Videos, and Scores
-
- Member
- Posts: 9
- Joined: Tue Jan 24, 2017 11:02 am
- Division: C
- State: GA
- Has thanked: 0
- Been thanked: 0
Re: Pictures, Videos, and Scores
Hey Windu or Bazinga, now that electric vehicle has been phased out, do you guys mind sharing the code for your vehicles? I'd really love to see your guys' thought processes on tackling the EV code!
-
- Exalted Member
- Posts: 383
- Joined: Sat Mar 08, 2014 7:10 am
- Division: C
- State: NY
- Has thanked: 0
- Been thanked: 0
Re: Pictures, Videos, and Scores
Mine was very simple. I just used similar encoder code to the one posted by deesnotes earlier in the forums. As for the algorithm I used a DC motor with a 10A motor shield, so I sent the signal for full speed until it reaches 8.5 meters, and then had it brake. After that it went in long jumps (full power, short delay, brake, etc) until it was like 10 cm from the target, after which point it began to go in 2-3mm jumps every half second, slowly inching towards the target until the value read by the encoder reaches the target.fn2187 wrote:Hey Windu or Bazinga, now that electric vehicle has been phased out, do you guys mind sharing the code for your vehicles? I'd really love to see your guys' thought processes on tackling the EV code!
Innovation =/= success
-
- Staff Emeritus
- Posts: 1367
- Joined: Sun Apr 19, 2015 6:37 pm
- Division: Grad
- State: FL
- Has thanked: 2 times
- Been thanked: 31 times
Re: Pictures, Videos, and Scores
I lost my flashdrive that had MK4 and MK5 (which we used at nationals), but this should be similar enough to see what i did
MK3
MK3
Code: Select all
/*Electric Vehicle Nationals 2017
Authors: Kevin Hao and Alex Arber
Date: 2016-2017
Assuming .23m at 9m, 160278 values
Assuming .23m at 12m, 213704 values
Assuming 17808, 160272 values
Assuming 17808, 213696 values
*/
#include <LiquidCrystal.h>
#include <Servo.h>
//#define VALUESTOMETERS 35616.000
#define VALUESTOMETERS 17808.000
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define A 2
#define B 3
Servo motor;
int speeds[] = {90, 118, 77, 97}; //124 is supposed to be 114
int delays[] = {400, 100, 80, 50};
float InterruptPoints[] = {0.000, 6.000, 8.000}; //8 is supposed to be 8.7
int previousDistance[] = {0, 0, 0};
volatile long encoder;
byte inputA, inputB;
float PlaceValue = 1.00000;
boolean approachTarget = false;
int lcd_key = 0;
int adc_key_in = 0;
boolean selectDistance = true;
float distance = 9.000;
int startTime;
int forceStartTime = 3000;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
/* INITIALIZE BUTTON READER */
int read_LCD_buttons() {
adc_key_in = analogRead(0);
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 400) return btnDOWN;
if (adc_key_in < 600) return btnLEFT;
if (adc_key_in < 950) return btnSELECT;
return btnNONE;
}
void setup() {
//Serial.begin(9600);
pinMode(A, INPUT);
//pinMode(B, INPUT);
digitalWrite(A, HIGH);
// digitalWrite(B, HIGH);
attachInterrupt(0, AChange, CHANGE);
//attachInterrupt(1, BChange, CHANGE);
lcd.begin(16, 2);
motor.attach(1);
delay(500);
motor.write(90);
delay(200);
encoder = 0;
}
void loop() {
while (selectDistance == true) {
motor.write(90);
lcd.setCursor(0, 1);
lcd_key = read_LCD_buttons();
SelectDistance();
//Serial.println("In while");
// Serial.println(encoder);
}
// Serial.println(encoder);
if (approachTarget == true) {
ApproachTarget();
}
else{
MotorControl();
}
}
void SelectDistance() {
lcd.setCursor(0, 0);
lcd.print("Boca High C20");
lcd.setCursor(0, 1);
switch (lcd_key) {
case btnRIGHT: {
lcd.setCursor(0, 1);
PlaceValue = PlaceValue * 10.000;
lcd.print(String(distance, 3));
delay(300);
break;
}
case btnLEFT: {
lcd.setCursor(0, 1);
PlaceValue = PlaceValue / 10.000;
lcd.print(String(distance, 3));
delay(300);
break;
}
case btnUP: {
distance += PlaceValue;
lcd.print(String(distance, 3));
delay(300);
break;
}
case btnDOWN: {
distance -= PlaceValue;
lcd.print(String(distance, 3));
delay(300);
break;
}
case btnNONE: {
lcd.print(String(distance, 3));
break;
}
case btnSELECT: {
lcd.setCursor(0, 0);
lcd.print("Run Started");
lcd.setCursor(0, 1);
selectDistance = false;
delay(300);
encoder = 0;
DetermineBrakeDistances();
startTime = millis();
break;
}
}
}
void AChange() {
inputA = (PIND & 0b00000100) >> 2;
inputB = (PIND & 0b00001000) >> 3;
if (inputA == inputB) {
encoder++;
}
else {
encoder--;
}
}
void BChange() {
inputA = (PIND & 0b00000100) >> 2;
inputB = (PIND & 0b00001000) >> 3;
if (inputA != inputB) {
encoder++;
}
else {
encoder--;
}
}
void DetermineBrakeDistances(){
InterruptPoints[1] += (distance - 9.000);
InterruptPoints[2] += (distance - 9.000);
}
void MotorControl() {
if (encoder >= InterruptPoints[2]*VALUESTOMETERS) {
motor.write(speeds[2]);
delay(300);
motor.write(speeds[0]);
delay(2000);
approachTarget = true;
}
else if (encoder >= InterruptPoints[1]*VALUESTOMETERS) {
motor.write(speeds[0]);
if(millis() - startTime > forceStartTime){
motor.write(speeds[3]);
}
}
else if (encoder >= InterruptPoints[0]*VALUESTOMETERS) {
motor.write(speeds[1]);
}
}
void ApproachTarget() {
long totalValuesToTravel = (VALUESTOMETERS * distance);
long valuesLeft = totalValuesToTravel - encoder;
previousDistance[2] = previousDistance[1];
previousDistance[1] = previousDistance[0];
previousDistance[0] = valuesLeft;
/*
Serial.print("Total values to Travel: ");
Serial.println(totalValuesToTravel);
Serial.print("Values Left to Travel: ");
Serial.println(valuesLeft);
*/
int higherSpeed = 105;
int mediumSpeed = 103;
int lowerSpeed = 101;
if (previousDistance[0] == previousDistance[1] && previousDistance[1] == previousDistance[2] && previousDistance[0] == previousDistance[2]) {
higherSpeed++;
mediumSpeed++;
lowerSpeed++;
}
if (valuesLeft >= 30000/2) {
motor.write(higherSpeed);
delay(delays[0]);
motor.write(speeds[0]);
delay(delays[0]);
}
else if (valuesLeft < 30000/2 && valuesLeft >= 10000/4) {
motor.write(mediumSpeed);
delay(delays[2]);
motor.write(speeds[0]);
delay(delays[0]);
}
else if (valuesLeft < 10000/4 && valuesLeft > 1000/4) {
motor.write(lowerSpeed);
delay(delays[3]);
motor.write(speeds[0]);
delay(delays[0]);
}
else {
motor.write(speeds[0]);
// getLit();
}
}
Boca Raton Community High School Alumni
Florida Science Olympiad Board of Directors
National Physical Sciences Rules Committee Member
kevin@floridascienceolympiad.org || windu34's Userpage
Florida Science Olympiad Board of Directors
National Physical Sciences Rules Committee Member
kevin@floridascienceolympiad.org || windu34's Userpage
-
- Member
- Posts: 9
- Joined: Tue Jan 24, 2017 11:02 am
- Division: C
- State: GA
- Has thanked: 0
- Been thanked: 0
Re: Pictures, Videos, and Scores
I used the same method when coding mine but for some reason I could never get consistent results. Did you use direct port reads in your code as well?Bazinga+ wrote:Mine was very simple. I just used similar encoder code to the one posted by deesnotes earlier in the forums. As for the algorithm I used a DC motor with a 10A motor shield, so I sent the signal for full speed until it reaches 8.5 meters, and then had it brake. After that it went in long jumps (full power, short delay, brake, etc) until it was like 10 cm from the target, after which point it began to go in 2-3mm jumps every half second, slowly inching towards the target until the value read by the encoder reaches the target.fn2187 wrote:Hey Windu or Bazinga, now that electric vehicle has been phased out, do you guys mind sharing the code for your vehicles? I'd really love to see your guys' thought processes on tackling the EV code!
-
- Exalted Member
- Posts: 383
- Joined: Sat Mar 08, 2014 7:10 am
- Division: C
- State: NY
- Has thanked: 0
- Been thanked: 0
Re: Pictures, Videos, and Scores
I used interrupts, I think windu also had a similar method as well.fn2187 wrote:I used the same method when coding mine but for some reason I could never get consistent results. Did you use direct port reads in your code as well?Bazinga+ wrote:Mine was very simple. I just used similar encoder code to the one posted by deesnotes earlier in the forums. As for the algorithm I used a DC motor with a 10A motor shield, so I sent the signal for full speed until it reaches 8.5 meters, and then had it brake. After that it went in long jumps (full power, short delay, brake, etc) until it was like 10 cm from the target, after which point it began to go in 2-3mm jumps every half second, slowly inching towards the target until the value read by the encoder reaches the target.fn2187 wrote:Hey Windu or Bazinga, now that electric vehicle has been phased out, do you guys mind sharing the code for your vehicles? I'd really love to see your guys' thought processes on tackling the EV code!
Innovation =/= success