Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -247,41 +247,44 @@ ImageFloatBox OliveDetector::align_to_olive(

// todo: detect and handle case where olive is stuck.
// todo: detect and handle case where olive is slightly to the left, and so we need to move on to the next phase
uint16_t OliveDetector::push_olive_forward(
Milliseconds OliveDetector::push_olive_forward(
const ProgramInfo& info,
VideoStream& stream,
ProControllerContext& context,
double direction_facing,
uint16_t total_forward_distance,
uint16_t push_olive,
Milliseconds total_forward_distance,
Milliseconds push_olive,
uint8_t rgb_gap,
ImageFloatBox area_to_check,
bool throw_if_fail_change_dir
){
stream.log("push_olive_forward(): Total distance: " + std::to_string(total_forward_distance));
stream.log("push_olive_forward(): Total distance: " + std::to_string(total_forward_distance.count()) + "ms");
// uint16_t initial_push_olive = push_olive;
uint16_t ticks_walked = 0;
Milliseconds walked = 0ms;
size_t MAX_ATTEMPTS = 10;
for (size_t i = 0; i < MAX_ATTEMPTS; i++){
align_to_olive(info, stream, context, direction_facing, rgb_gap, area_to_check, throw_if_fail_change_dir);
ticks_walked += walk_up_to_olive(info, stream, context, direction_facing, rgb_gap, area_to_check);
walked += walk_up_to_olive(info, stream, context, direction_facing, rgb_gap, area_to_check);


if (ticks_walked >= total_forward_distance){
stream.log("Distance walked: " + std::to_string(ticks_walked) + "/" + std::to_string(total_forward_distance));
return ticks_walked;
if (walked >= total_forward_distance){
stream.log("Distance walked: " + std::to_string(walked.count()) + "/" + std::to_string(total_forward_distance.count()) + " ms");
return walked;
}

align_to_olive(info, stream, context, direction_facing, rgb_gap, area_to_check, throw_if_fail_change_dir);
// check location of olive before and after push
// if olive is approximately in the same location, then the olive is stuck. try moving backward and running forward again.
ImageFloatBox olive_box_1 = get_olive_floatbox(stream, context, rgb_gap, area_to_check);
for (size_t j = 0; j < 3; j++){
stream.log("Distance walked: " + std::to_string(ticks_walked) + "/" + std::to_string(total_forward_distance));
stream.log(
"Distance walked: " + std::to_string(walked.count()) +
"/" + std::to_string(total_forward_distance.count()) + " ms"
);
stream.log("Push the olive.");
pbf_move_left_joystick(context, 128, 0, push_olive, 7 * TICKS_PER_SECOND);
pbf_move_left_joystick(context, 128, 0, push_olive, 7000ms);

ticks_walked += push_olive;
walked += push_olive;
ImageFloatBox olive_box_2 = get_olive_floatbox(stream, context, rgb_gap, area_to_check);
double box_1_area = olive_box_1.width * olive_box_1.height;
double box_2_area = olive_box_2.width * olive_box_2.height;
Expand All @@ -294,8 +297,8 @@ uint16_t OliveDetector::push_olive_forward(
stream.log("Olive 1: area: " + std::to_string(box_1_area) + ", x: " + std::to_string(olive_box_1.x) + ", y: " + std::to_string(olive_box_1.y));
stream.log("Olive 2: area: " + std::to_string(box_2_area) + ", x: " + std::to_string(olive_box_2.x) + ", y: " + std::to_string(olive_box_2.y));
pbf_move_left_joystick(context, {0, -1}, 600ms, 800ms); // walk backwards
ticks_walked -= push_olive;
push_olive = 200; // run forward more on the next push
walked -= push_olive;
push_olive = 1600ms; // run forward more on the next push

if (j == 2){
throw_and_log<OliveActionFailedException>(
Expand All @@ -310,12 +313,18 @@ uint16_t OliveDetector::push_olive_forward(
}
}

if (ticks_walked > total_forward_distance){
stream.log("Distance walked: " + std::to_string(ticks_walked) + "/" + std::to_string(total_forward_distance));
return ticks_walked;
if (walked > total_forward_distance){
stream.log(
"Distance walked: " + std::to_string(walked.count()) +
"/" + std::to_string(total_forward_distance.count()) + " ms"
);
return walked;
}

stream.log("Distance walked: " + std::to_string(ticks_walked) + "/" + std::to_string(total_forward_distance));
stream.log(
"Distance walked: " + std::to_string(walked.count()) +
"/" + std::to_string(total_forward_distance.count()) + " ms"
);

}

Expand All @@ -328,50 +337,51 @@ uint16_t OliveDetector::push_olive_forward(

}

uint16_t OliveDetector::walk_up_to_olive(
Milliseconds OliveDetector::walk_up_to_olive(
const ProgramInfo& info,
VideoStream& stream,
ProControllerContext& context,
double direction_facing,
uint8_t rgb_gap,
ImageFloatBox area_to_check
){
uint16_t ticks_walked = 0;
Milliseconds walked = 0ms;
size_t MAX_ATTEMPTS = 20;
for (size_t i = 0; i < MAX_ATTEMPTS; i++){
ImageFloatBox olive_box = get_olive_floatbox(stream, context, rgb_gap, area_to_check);
std::pair<double, double> olive = box_center(olive_box);
// double olive_x = olive.first;
double olive_y = olive.second;

uint16_t scale_factor = 2000;
uint16_t push_duration = std::max(uint16_t((0.57 - olive_y)*(0.57 - olive_y) * scale_factor), uint16_t(20));
double scale_factor = 16000;
Milliseconds push_duration((int64_t)((0.57 - olive_y) * (0.57 - olive_y) * scale_factor));
push_duration = std::max(push_duration, 160ms);
stream.log("walk_up_to_olive(): olive_y: " + std::to_string(olive_y));
if (olive_y > 0.515){
return ticks_walked;
return walked;
}
stream.log("push duration: " + std::to_string(push_duration));
stream.log("push duration: " + std::to_string(push_duration.count()) + " ms");
// when push durations are low, the player moves less than expected
// once above 45, you walk about as much as expected
double walking_factor = 1;
if (push_duration <= 20){
if (push_duration <= 160ms){
walking_factor = 0.40;
}else if (push_duration <= 25){
}else if (push_duration <= 200ms){
walking_factor = 0.53;
}else if (push_duration <= 30){
}else if (push_duration <= 240ms){
walking_factor = 0.69;
}else if (push_duration <= 35){
}else if (push_duration <= 280ms){
walking_factor = 0.83;
}else if (push_duration <= 40){
}else if (push_duration <= 320ms){
walking_factor = 0.91;
}
ticks_walked += uint16_t(push_duration * walking_factor);
walked += Milliseconds((int64_t)(push_duration.count() * walking_factor));

uint16_t wait_ticks = 50;
Milliseconds wait = 400ms;
if (olive_y > 0.4){
wait_ticks = 100;
wait = 800ms;
}
pbf_move_left_joystick(context, 128, 0, push_duration, wait_ticks);
pbf_move_left_joystick(context, 128, 0, push_duration, wait);
}

throw_and_log<OliveActionFailedException>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ class OliveDetector {
// move forward a certain number of ticks, as per total_forward_distance
// always face a certain direction, as per direction_facing
// return number of ticks walked
uint16_t push_olive_forward(
Milliseconds push_olive_forward(
const ProgramInfo& info,
VideoStream& stream,
ProControllerContext& context,
double direction_facing,
uint16_t total_forward_distance,
uint16_t push_olive = 75,
double direction_facing,
Milliseconds total_forward_distance,
Milliseconds push_olive = Milliseconds(600),
uint8_t rgb_gap = 20,
ImageFloatBox area_to_check = {0, 0.3, 1.0, 0.40}, // {0, 0.15, 1, 0.7}
bool throw_if_fail_change_dir = true
);

uint16_t walk_up_to_olive(
Milliseconds walk_up_to_olive(
const ProgramInfo& info,
VideoStream& stream,
ProControllerContext& context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ void checkpoint_26(
pbf_move_left_joystick(context, 128, 0, 400, 50);
OliveDetector green(env.console);
size_t MAX_ATTEMPTS_SECTION_1 = 2;
uint16_t ticks_to_walk_for_section1 = 650;
uint16_t push_strength_section_1 = 75;
Milliseconds duration_to_walk_for_section1 = 5200ms;
Milliseconds push_strength_section_1 = 600ms;
for (size_t i = 0; i < MAX_ATTEMPTS_SECTION_1; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 4.38, ticks_to_walk_for_section1, push_strength_section_1);
green.push_olive_forward(env.program_info(), env.console, context, 4.38, duration_to_walk_for_section1, push_strength_section_1);
// green.align_to_olive(env.program_info(), env.console, context, 4.38);
green.walk_up_to_olive(env.program_info(), env.console, context, 4.38);
break;
Expand All @@ -290,8 +290,8 @@ void checkpoint_26(
pbf_move_left_joystick(context, {0, -1}, 1600ms, 400ms);
pbf_wait(context, 5000ms);
context.wait_for_all_requests();
ticks_to_walk_for_section1 = 100;
push_strength_section_1 = 50;
duration_to_walk_for_section1 = 800ms;
push_strength_section_1 = 400ms;
}else{ // FAILED_PUSH_OLIVE_TOTAL_DISTANCE
throw e;
}
Expand All @@ -315,7 +315,7 @@ void checkpoint_26(
size_t MAX_ATTEMPTS_SECTION_2_1 = 2;
for (size_t i = 0; i < MAX_ATTEMPTS_SECTION_2_1; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 5.95, 50, 50);
green.push_olive_forward(env.program_info(), env.console, context, 5.95, 400ms, 400ms);
break;
}catch (OliveActionFailedException& e){
if (i >= MAX_ATTEMPTS_SECTION_2_1-1){
Expand Down Expand Up @@ -350,7 +350,7 @@ void checkpoint_26(
// uint16_t ticks_walked_section2_2 = 0;
for (size_t i = 0; i < MAX_ATTEMPTS_SECTION_2_2; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 250, 50);
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 2000ms, 400ms);
break;
}catch (OliveActionFailedException& e){
if (i >= MAX_ATTEMPTS_SECTION_2_2-1){
Expand All @@ -364,7 +364,7 @@ void checkpoint_26(
// push olive parallel to fence
green.align_to_olive(env.program_info(), env.console, context, 4.28, 20);
green.walk_up_to_olive(env.program_info(), env.console, context, 4.28);
green.push_olive_forward(env.program_info(), env.console, context, 4.28, 100);
green.push_olive_forward(env.program_info(), env.console, context, 4.28, 800ms);
green.align_to_olive(env.program_info(), env.console, context, 4.28, 20);
green.walk_up_to_olive(env.program_info(), env.console, context, 4.28);
// back off
Expand Down Expand Up @@ -399,11 +399,11 @@ void checkpoint_26(
pbf_move_left_joystick(context, {0, -1}, 4000ms, 400ms);

// section 2.3 push olive past first NPC
uint16_t ticks_to_walk_for_section2_3 = 950;
Milliseconds duration_to_walk_for_section2_3 = 7600ms;
size_t MAX_ATTEMPTS = 2;
for (size_t i = 0; i < MAX_ATTEMPTS; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 5.95, ticks_to_walk_for_section2_3, 75, 10);
green.push_olive_forward(env.program_info(), env.console, context, 5.95, duration_to_walk_for_section2_3, 600ms, 10);
// green.align_to_olive(env.program_info(), env.console, context, 5.95, 10);
green.walk_up_to_olive(env.program_info(), env.console, context, 5.95, 10);
break;
Expand Down Expand Up @@ -438,11 +438,18 @@ void checkpoint_26(
pbf_move_left_joystick(context, 128, 0, 400ms, 400ms);

// section 3.1 push olive across the hump
uint16_t ticks_to_walk_for_section3_1 = 350;
uint16_t ticks_walked_section3_1 = 0;
Milliseconds duration_to_walk_for_section3_1 = 2800ms;
Milliseconds duration_walked_section3_1 = 0ms;
for (size_t i = 0; i < MAX_ATTEMPTS; i++){
try{
ticks_walked_section3_1 = green.push_olive_forward(env.program_info(), env.console, context, 1.27, ticks_to_walk_for_section3_1, 125);
duration_walked_section3_1 = green.push_olive_forward(
env.program_info(),
env.console,
context,
1.27,
duration_to_walk_for_section3_1,
1000ms
);
break;
}catch (OliveActionFailedException& e){
// may have failed to push the olive past the hump. and walked past it
Expand All @@ -455,14 +462,14 @@ void checkpoint_26(
pbf_move_left_joystick(context, 128, 0, 400ms, 400ms); // move forward slight in case the olive is undetectable since it's right in front of the character
pbf_wait(context, 5000ms);
context.wait_for_all_requests();
ticks_to_walk_for_section3_1 = 200;
duration_to_walk_for_section3_1 = 1600ms;
}else if (e.m_fail_reason == OliveFail::FAILED_WALK_TO_OLIVE || e.m_fail_reason == OliveFail::OLIVE_STUCK){
// try moving back and then ramming forward
pbf_move_left_joystick(context, {0, -1}, 400ms, 400ms);
pbf_move_left_joystick(context, 128, 0, 150, 50);
pbf_wait(context, 7000ms);
context.wait_for_all_requests();
ticks_to_walk_for_section3_1 = 200;
duration_to_walk_for_section3_1 = 1600ms;
}else{ // FAILED_PUSH_OLIVE_TOTAL_DISTANCE,
throw e;
}
Expand All @@ -472,14 +479,14 @@ void checkpoint_26(


// section 3.2 push olive across the hump and into fence
uint16_t total_ticks_to_walk_for_section3 = 550;
uint16_t ticks_to_walk_for_section3_2 = 0;
if (ticks_walked_section3_1 < total_ticks_to_walk_for_section3){
ticks_to_walk_for_section3_2 = (total_ticks_to_walk_for_section3 - ticks_walked_section3_1);
Milliseconds total_duration_to_walk_for_section3 = 4400ms;
Milliseconds duration_to_walk_for_section3_2 = 0ms;
if (duration_walked_section3_1 < total_duration_to_walk_for_section3){
duration_to_walk_for_section3_2 = (total_duration_to_walk_for_section3 - duration_walked_section3_1);
}
for (size_t i = 0; i < MAX_ATTEMPTS; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 1.27, ticks_to_walk_for_section3_2, 125);
green.push_olive_forward(env.program_info(), env.console, context, 1.27, duration_to_walk_for_section3_2, 1000ms);
pbf_move_left_joystick(context, {0, -1}, 800ms, 400ms);
green.align_to_olive(env.program_info(), env.console, context, 1.27);
green.walk_up_to_olive(env.program_info(), env.console, context, 1.27);
Expand All @@ -493,7 +500,7 @@ void checkpoint_26(
pbf_move_left_joystick(context, {0, -1}, 1600ms, 400ms);
pbf_wait(context, 7000ms);
context.wait_for_all_requests();
ticks_to_walk_for_section3_2 += 200;
duration_to_walk_for_section3_2 += 1600ms;
}else if (e.m_fail_reason == OliveFail::FAILED_WALK_TO_OLIVE || e.m_fail_reason == OliveFail::OLIVE_STUCK){
// try moving back and then ramming forward
pbf_move_left_joystick(context, {0, -1}, 400ms, 400ms);
Expand All @@ -519,7 +526,7 @@ void checkpoint_26(
try{
green.align_to_olive(env.program_info(), env.console, context, 6.0);
green.walk_up_to_olive(env.program_info(), env.console, context, 6.0);
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 50, 50);
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 400ms, 400ms);
break;
}catch (OliveActionFailedException& e){
// may have failed to push the olive. and walked past it
Expand Down Expand Up @@ -547,10 +554,10 @@ void checkpoint_26(
[&](ProControllerContext& context){
for (size_t i = 0; i < MAX_ATTEMPTS_SECTION_4; i++){
try{
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 250, 75, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 100, 75, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 200, 75, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 6.1, 200, 75, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 2000ms, 600ms, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 800ms, 600ms, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 6.0, 1600ms, 600ms, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 6.1, 1600ms, 600ms, 20, {0, 0.3, 1.0, 0.40}, false);
break;
}catch (OliveActionFailedException& e){
// may have failed to push the olive. and walked past it
Expand All @@ -565,7 +572,7 @@ void checkpoint_26(
pbf_wait(context, 7000ms);
context.wait_for_all_requests();
// then push angled towards the right
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 100, 75, 20, {0, 0.3, 1.0, 0.40}, false);
green.push_olive_forward(env.program_info(), env.console, context, 5.8, 800ms, 600ms, 20, {0, 0.3, 1.0, 0.40}, false);
}else{ // FAILED_PUSH_OLIVE_TOTAL_DISTANCE,
throw e;
}
Expand Down Expand Up @@ -623,3 +630,4 @@ void checkpoint_27(
}
}
}