2 * Copyright Droids, Microb Technology (2010)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: strat.c,v 1.6 2009-11-08 17:24:33 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
30 #include <aversive/pgmspace.h>
31 #include <aversive/error.h>
36 #include <clock_time.h>
41 #include <control_system_manager.h>
42 #include <trajectory_manager.h>
43 #include <trajectory_manager_utils.h>
44 #include <trajectory_manager_core.h>
45 #include <vect_base.h>
48 #include <obstacle_avoidance.h>
49 #include <blocking_detection_manager.h>
50 #include <robot_system.h>
51 #include <position_manager.h>
53 #include <diagnostic.h>
58 #include "../common/i2c_commands.h"
59 #include "i2c_protocol.h"
63 #include "strat_base.h"
64 #include "strat_corn.h"
65 #include "strat_avoid.h"
66 #include "strat_utils.h"
73 #define DPR(fmt, ...) printf_P(PSTR(fmt), ##__VA_ARGS__)
75 #define DPR(args...) do {} while (0)
81 const struct wp_coord *path;
84 const struct wp_coord butterfly_tab[] = {
109 { .i = 10, .j = 2, },
110 { .i = 11, .j = 1, },
111 { .i = 11, .j = 2, },
112 { .i = 11, .j = 3, },
113 { .i = 11, .j = 4, },
114 { .i = 11, .j = 5, },
115 { .i = 11, .j = 6, },
118 const struct circuit butterfly_circuit = {
120 .len = sizeof(butterfly_tab)/sizeof(struct wp_coord),
121 .path = butterfly_tab,
124 const struct wp_coord losange_tab[] = {
125 { .i = 11, .j = 6, },
126 { .i = 10, .j = 6, },
130 { .i = 10, .j = 4, },
131 { .i = 11, .j = 4, },
132 { .i = 11, .j = 5, },
133 { .i = 11, .j = 6, },
136 const struct circuit losange_circuit = {
138 .len = sizeof(losange_tab)/sizeof(struct wp_coord),
142 const struct wp_coord triangle_tab[] = {
143 { .i = 11, .j = 6, },
144 { .i = 10, .j = 6, },
152 { .i = 10, .j = 2, },
153 { .i = 11, .j = 1, },
154 { .i = 11, .j = 2, },
155 { .i = 11, .j = 3, },
156 { .i = 11, .j = 4, },
157 { .i = 11, .j = 5, },
158 { .i = 11, .j = 6, },
161 const struct circuit triangle_circuit = {
163 .len = sizeof(triangle_tab)/sizeof(struct wp_coord),
164 .path = triangle_tab,
167 const struct wp_coord answer_d_tab[] = {
168 { .i = 11, .j = 6, },
169 { .i = 11, .j = 5, },
170 { .i = 11, .j = 4, },
171 { .i = 11, .j = 3, },
172 { .i = 11, .j = 2, },
173 { .i = 11, .j = 1, },
174 { .i = 10, .j = 2, },
178 { .i = 10, .j = 4, },
179 { .i = 11, .j = 4, },
180 { .i = 11, .j = 5, },
181 { .i = 11, .j = 6, },
184 const struct circuit answer_d_circuit = {
186 .len = sizeof(answer_d_tab)/sizeof(struct wp_coord),
187 .path = answer_d_tab,
190 const struct wp_coord h_lambda_tab[] = {
191 { .i = 11, .j = 6, },
192 { .i = 10, .j = 6, },
205 { .i = 10, .j = 6, },
206 { .i = 11, .j = 6, },
209 const struct circuit h_lambda_circuit = {
211 .len = sizeof(h_lambda_tab)/sizeof(struct wp_coord),
212 .path = h_lambda_tab,
215 const struct wp_coord asym_butterfly_tab[] = {
216 { .i = 11, .j = 6, },
217 { .i = 10, .j = 6, },
234 { .i = 10, .j = 2, },
235 { .i = 11, .j = 1, },
236 { .i = 11, .j = 2, },
237 { .i = 11, .j = 3, },
238 { .i = 11, .j = 4, },
239 { .i = 11, .j = 5, },
240 { .i = 11, .j = 6, },
243 const struct circuit asym_butterfly_circuit = {
244 .name = "asym_butterfly",
245 .len = sizeof(asym_butterfly_tab)/sizeof(struct wp_coord),
246 .path = asym_butterfly_tab,
249 const struct wp_coord big_h_lambda_tab[] = {
250 { .i = 11, .j = 6, },
251 { .i = 10, .j = 6, },
274 { .i = 10, .j = 6, },
275 { .i = 11, .j = 6, },
278 const struct circuit big_h_lambda_circuit = {
279 .name = "big_h_lambda",
280 .len = sizeof(big_h_lambda_tab)/sizeof(struct wp_coord),
281 .path = big_h_lambda_tab,
284 const struct wp_coord letter_v_tab[] = {
285 { .i = 11, .j = 6, },
286 { .i = 10, .j = 6, },
306 { .i = 10, .j = 4, },
307 { .i = 11, .j = 4, },
308 { .i = 11, .j = 5, },
309 { .i = 11, .j = 6, },
312 const struct circuit letter_v_circuit = {
314 .len = sizeof(letter_v_tab)/sizeof(struct wp_coord),
315 .path = letter_v_tab,
318 const struct wp_coord sperma_tab[] = {
319 { .i = 11, .j = 6, },
320 { .i = 10, .j = 6, },
341 { .i = 10, .j = 6, },
342 { .i = 11, .j = 6, },
345 const struct circuit sperma_circuit = {
347 .len = sizeof(sperma_tab)/sizeof(struct wp_coord),
351 /* list of all possible circuits */
352 const struct circuit *circuits[] = {
358 &asym_butterfly_circuit,
359 &big_h_lambda_circuit,
365 /* symetric neighbor position */
366 static inline uint8_t opposite_position(uint8_t pos)
375 //#define TEST_STRAT_AVOID
378 #ifdef TEST_STRAT_AVOID
380 uint8_t xget_cob_count(void)
386 uint8_t xget_ball_count(void)
392 uint8_t xtime_get_s(void)
397 #define xget_cob_count() get_cob_count()
398 #define xget_ball_count() get_ball_count()
399 #define xtime_get_s() time_get_s()
402 /* return true if turn is at 60 deg */
403 uint8_t is_60deg(uint8_t dir1, uint8_t dir2)
417 /* return true if turn is at 60 deg */
418 uint8_t is_120deg(uint8_t dir1, uint8_t dir2)
432 /* get the neighbour of the point at specified dir, return -1 if
433 * there is no neighbor */
434 int8_t wp_get_neigh(uint8_t i, uint8_t j, uint8_t *ni, uint8_t *nj,
463 if (i >= WAYPOINTS_NBX || j >= WAYPOINTS_NBY)
471 static int8_t get_line_num(int8_t i, int8_t j, uint8_t dir)
484 if ((mod == 0 || mod == 1) && ((j & 1) == 0))
486 if ((mod == 2 || mod == 3) && ((j & 1) == 1))
494 if ((mod == 0 || mod == 3) && ((j & 1) == 0))
496 if ((mod == 1 || mod == 2) && ((j & 1) == 1))
506 static uint8_t get_dir(uint8_t prev_i, uint8_t prev_j,
507 uint8_t i, uint8_t j)
509 int8_t diff_i, diff_j;
514 if (diff_i == 0 && diff_j == 1)
516 if (diff_i == 0 && diff_j == -1)
519 if ((prev_i & 1) == 0) {
520 if (diff_i == 1 && diff_j == 0)
522 if (diff_i == 1 && diff_j == -1)
524 if (diff_i == -1 && diff_j == 0)
526 if (diff_i == -1 && diff_j == -1)
530 if (diff_i == 1 && diff_j == 1)
532 if (diff_i == 1 && diff_j == 0)
534 if (diff_i == -1 && diff_j == 1)
536 if (diff_i == -1 && diff_j == 0)
544 /* return approximative angle of line */
545 int16_t linedir2angle(uint8_t dir)
559 return COLOR_A(-150);
565 /* return true if a waypoint belongs to a line */
566 uint8_t wp_belongs_to_line(uint8_t i, uint8_t j, uint8_t linenum, uint8_t dir)
569 ln = get_line_num(i, j, dir);
577 /* count the number of non-black corns which are neighbors of
579 uint8_t corn_count_neigh(uint8_t i, uint8_t j)
584 for (dir = LINE_UP; dir <= LINE_R_UP; dir++) {
585 if (wp_get_neigh(i, j, &ni, &nj, dir) < 0)
588 /* is there a corn cob removed for more than 2 secs ? */
589 if (strat_db.wp_table[ni][nj].type == WP_TYPE_CORN &&
590 strat_db.wp_table[ni][nj].corn.color != I2C_COB_BLACK &&
591 (strat_db.wp_table[ni][nj].present ||
592 strat_db.wp_table[ni][nj].time_removed + 2 > time_get_s()))
600 /* fill circuit_wpline table with waypoints from circuit starting at
601 * i,j and using selected face */
602 static int8_t get_path(const struct circuit *circuit,
603 uint8_t starti, uint8_t startj, uint8_t faceA,
604 struct wp_line *circuit_wpline)
606 const struct wp_coord *curcircuit;
607 const struct wp_coord *start;
608 const struct wp_coord *end;
609 uint8_t prev_i, prev_j;
610 uint8_t dir, prev_dir = 0xFF;
611 uint8_t found = 0, i = 0, j = 0;
613 int8_t step = faceA ? 1 : -1;
616 /* start and end of circuit */
618 start = &circuit->path[0];
619 end = start + circuit->len - 1;
622 end = &circuit->path[0];
623 start = end + circuit->len - 1;
626 DPR("%s(): %s face=%d\r\n", __FUNCTION__, circuit->name, faceA);
628 /* check that the point is present in the circuit */
629 for (curcircuit = start; curcircuit != end; curcircuit += step) {
630 if (curcircuit->i == starti && curcircuit->j == startj) {
639 /* browse the circuit from starti, startj in the specified
640 * direction, and fill the table when direction changes */
643 for ( ; curcircuit != end;
644 curcircuit += step, prev_dir = dir, prev_i = i, prev_j = j) {
649 dir = get_dir(prev_i, prev_j, i, j);
651 if (prev_dir != dir) {
652 linenum = get_line_num(prev_i, prev_j, dir);
653 circuit_wpline[path_len].line_num = linenum;
654 circuit_wpline[path_len].dir = dir;
655 DPR("%s(): %d %d -> %d %d / len=%d num=%d dir=%d\r\n",
656 __FUNCTION__, prev_i, prev_j, i, j, path_len, linenum, dir);
664 /* process score from retrieved objects number, and circuit len */
665 static int16_t get_score(uint32_t wcorn_retrieved,
666 uint32_t ucorn_retrieved,
667 uint16_t tomato_retrieved,
668 uint16_t utomato_retrieved,
669 uint8_t len, uint8_t opp_on_path)
676 /* score with corn */
677 n = xget_cob_count() * 2;
678 for (i = 0; i<CORN_NB; i++) {
681 if (wcorn_retrieved & mask) {
687 if (ucorn_retrieved & mask) {
694 DPR("get score: cob %d (->%d)\r\n", n, n/2);
696 /* score with tomato */
697 n = xget_ball_count();
699 for (i = 0; i<TOMATO_NB; i++) {
702 if (tomato_retrieved & mask) {
709 DPR("get score: ball %d\r\n", n);
711 /* malus for long circuits */
713 DPR("malus for length: %d\r\n", len * 20);
715 /* double malus for long circuits if we don't have much
718 if (len * WP_SPEED > (MATCH_TIME - xtime_get_s())) {
720 extra = (len * WP_SPEED) - (MATCH_TIME - xtime_get_s());
721 extra = (200 * extra);
722 if (extra < 0) /* should not happen */
727 DPR("malus for length + time: %d\r\n", extra);
730 /* malus if there is opponent on the path */
732 DPR("malus for opponent: %d\r\n", (500 * opp_on_path));
733 score -= (500 * opp_on_path);
739 /* return the corn type of specified coords: I2C_COB_WHITE,
740 * I2C_COB_UNKNOWN, or I2C_COB_NONE if it is black or not present */
741 static uint8_t get_corn_type(uint8_t i, uint8_t j)
744 /* is there a corn cob ? */
745 if (strat_db.wp_table[i][j].type == WP_TYPE_CORN &&
746 strat_db.wp_table[i][j].present) {
747 color = strat_db.wp_table[i][j].corn.color;
748 if (color == I2C_COB_WHITE)
749 return I2C_COB_WHITE;
750 else if (color == I2C_COB_UNKNOWN)
751 return I2C_COB_UNKNOWN;
756 /* i,j: starting position */
757 static int8_t evaluate_one_face(const struct circuit *circuit,
758 uint8_t starti, uint8_t startj,
759 uint8_t faceA, int16_t *score)
761 const struct wp_coord *curcircuit;
762 const struct wp_coord *start;
763 const struct wp_coord *end;
764 uint32_t wcorn_retrieved = 0; /* bit mask */
765 uint32_t ucorn_retrieved = 0; /* bit mask */
766 uint16_t tomato_retrieved = 0; /* bit mask */
767 uint16_t utomato_retrieved = 0; /* bit mask */
768 uint8_t opponent_on_path = 0;
769 uint8_t len = 0, found = 0;
770 uint8_t i, j, prev_i, prev_j;
771 uint8_t ni = 0, nj = 0;
772 uint8_t dir, color, idx, visited;
773 int8_t step = faceA ? 1 : -1;
775 int32_t d, prev_d = 0;
778 *score = 0x8000; /* -int_max */
780 /* start and end of circuit */
782 start = &circuit->path[0];
783 end = start + circuit->len - 1;
786 end = &circuit->path[0];
787 start = end + circuit->len - 1;
790 DPR("%s() face: %s %d\r\n", __FUNCTION__, circuit->name, faceA);
792 /* check that the point is present in the circuit */
793 for (curcircuit = start; curcircuit != end; curcircuit += step) {
794 if (curcircuit->i == starti && curcircuit->j == startj) {
802 /* get opponent coords */
803 if (get_opponent_xy(&oppx, &oppy) < 0)
804 oppx = I2C_OPPONENT_NOT_THERE;
806 DPR("%s() opponent: %d, %d\r\n", __FUNCTION__, oppx, oppy);
808 /* silent the compiler */
812 /* browse all points and calculate the score */
813 for (; /* start at starti,startj */
815 curcircuit += step, len ++, prev_i = i, prev_j = j) {
819 /* is opponent near the point ? */
820 ijcoord_to_xycoord(i, j, &x, &y);
821 if (oppx != I2C_OPPONENT_NOT_THERE) {
822 d = quad_distance_between(oppx, oppy, x, y);
823 DPR("%s(): opp at %d mm (ij=%d,%d opp=%d,%d pos=%d,%d)\r\n",
824 __FUNCTION__, d, i, j, oppx, oppy, x, y);
825 if (d < (250L*250L) && d < prev_d)
826 opponent_on_path += 3;
827 else if (d < (500L*500L) && d < prev_d)
832 /* don't try to look cobs/tomato for first point */
833 if (curcircuit == start)
836 /* get current direction, we wil check cobs behind us
837 * on left and right */
838 dir = get_dir(prev_i, prev_j, i, j);
840 DPR("%d %d -> %d %d (%d)\n", prev_i, prev_j, i, j, dir);
842 /* is there a tomato ? */
843 if (strat_db.wp_table[i][j].type == WP_TYPE_TOMATO &&
844 strat_db.wp_table[i][j].present) {
845 if (strat_db.wp_table[i][j].opp_visited) {
846 DPR(" TOMATO (opp visited)\n");
847 utomato_retrieved |= (1UL << strat_db.wp_table[i][j].tomato.idx);
851 tomato_retrieved |= (1UL << strat_db.wp_table[i][j].tomato.idx);
856 if (wp_get_neigh(i, j, &ni, &nj, (dir + 2) % 6) == 0) {
857 color = get_corn_type(ni, nj);
858 idx = strat_db.wp_table[ni][nj].corn.idx;
859 visited = strat_db.wp_table[ni][nj].opp_visited;
860 if (color == I2C_COB_WHITE && !visited) {
861 DPR(" LEFT WCORN (%d)\n", idx);
862 wcorn_retrieved |= (1UL << idx);
864 else if (color == I2C_COB_WHITE && visited) {
865 DPR(" LEFT CORN visited (%d)\n", idx);
866 ucorn_retrieved |= (1UL << idx);
868 else if (color == I2C_COB_UNKNOWN) {
869 DPR(" LEFT UCORN (%d)\n", idx);
870 ucorn_retrieved |= (1UL << idx);
875 if (wp_get_neigh(i, j, &ni, &nj, (dir + 4) % 6) == 0) {
876 color = get_corn_type(ni, nj);
877 idx = strat_db.wp_table[ni][nj].corn.idx;
878 visited = strat_db.wp_table[ni][nj].opp_visited;
879 if (color == I2C_COB_WHITE && !visited) {
880 DPR(" RIGHT WCORN (%d)\n", idx);
881 wcorn_retrieved |= (1UL << idx);
883 else if (color == I2C_COB_WHITE && visited) {
884 DPR(" RIGHT CORN visited (%d)\n", idx);
885 ucorn_retrieved |= (1UL << idx);
887 else if (color == I2C_COB_UNKNOWN) {
888 DPR(" RIGHT UCORN (%d)\n", idx);
889 ucorn_retrieved |= (1UL << idx);
893 /* prev_i, prev_j, len and curcircuit are updated in
897 /* write score and exit */
898 *score = get_score(wcorn_retrieved, ucorn_retrieved,
899 tomato_retrieved, utomato_retrieved,
900 len, opponent_on_path);
904 /* i,j: starting position */
905 static int8_t evaluate_one_circuit(const struct circuit *circuit,
906 uint8_t starti, uint8_t startj,
907 int16_t *scoreA, int16_t *scoreB)
909 if (evaluate_one_face(circuit, starti, startj, 1, scoreA) < 0)
912 /* we are on eject point, scoreB is the same */
913 if (starti == 11 && startj == 6) {
918 if (evaluate_one_face(circuit, starti, startj, 0, scoreB) < 0)
923 /* i,j starting position */
924 static int8_t find_best_circuit(uint8_t i, uint8_t j,
925 const struct circuit **selected_circuit,
926 int8_t *selected_face)
928 const struct circuit **circuit;
929 int16_t scoreA, scoreB;
930 int16_t selected_score = 0x8000; /* ~-int_max */
934 *selected_circuit = circuits[0] ;
935 for (circuit = &circuits[0]; *circuit; circuit++) {
936 if (evaluate_one_circuit(*circuit, i, j, &scoreA, &scoreB) < 0)
939 DEBUG(E_USER_STRAT, "Scores for %s are: faceA=%d, faceB=%d",
940 (*circuit)->name, scoreA, scoreB);
941 if (scoreA > selected_score) {
942 *selected_circuit = *circuit;
943 selected_score = scoreA;
946 if (scoreB > selected_score) {
947 *selected_circuit = *circuit;
948 selected_score = scoreB;
954 DEBUG(E_USER_STRAT, "no circuit found");
956 DEBUG(E_USER_STRAT, "circuit found: %s, %s",
957 (*selected_circuit)->name,
958 (*selected_face) ? "faceA":"faceB");
962 static void init_all_circuits(void)
964 const struct circuit **circuit;
965 const struct wp_coord *cur;
966 const struct wp_coord *start;
967 const struct wp_coord *end;
968 uint8_t prev_i, prev_j, i, j, dir;
970 for (circuit = &circuits[0]; *circuit; circuit++) {
971 start = &(*circuit)->path[0];
972 end = start + (*circuit)->len - 1;
978 for (cur = start; cur != end;
979 cur ++, prev_i = i, prev_j = j) {
984 strat_db.wp_table[i][j].on_circuit = 1;
986 dir = get_dir(prev_i, prev_j, i, j);
988 printf_P("Bad circuit %s %d %d\r\n", (*circuit)->name, i, j);
994 static void dump_circuit_wp(struct wp_line *circuit_wpline, int8_t len)
999 for (i = 0; i < len; i ++) {
1000 DEBUG(E_USER_STRAT, "linenum %d dir %d",
1001 circuit_wpline[i].line_num,
1002 circuit_wpline[i].dir);
1007 /* choose a circuit, then harvest on this circuit */
1008 uint8_t strat_harvest_circuit(void)
1010 const struct circuit *selected_circuit;
1011 int8_t selected_face;
1012 struct wp_line circuit_wpline[MAX_CIRCUIT_WPLINE];
1016 uint8_t linenum, prev_linenum;
1017 uint8_t dir, prev_dir;
1020 strat_set_speed(SPEED_CLITOID_SLOW, SPEED_ANGLE_SLOW);
1021 strat_want_pack = 1;
1023 x = position_get_x_s16(&mainboard.pos);
1024 y = position_get_y_s16(&mainboard.pos);
1026 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0) {
1027 DEBUG(E_USER_STRAT, "%s(): cannot find waypoint at %d,%d",
1028 __FUNCTION__, x, y);
1033 if (find_best_circuit(i, j, &selected_circuit, &selected_face) < 0) {
1034 DEBUG(E_USER_STRAT, "%s(): cannot find a good circuit",
1040 len = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1042 DEBUG(E_USER_STRAT, "%s(): cannot find a path",
1048 dump_circuit_wp(circuit_wpline, len);
1050 prev_linenum = circuit_wpline[0].line_num;
1051 prev_dir = circuit_wpline[0].dir;
1053 /* fix orientation first */
1054 trajectory_a_abs(&mainboard.traj, linedir2angle(prev_dir));
1055 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1056 if (!TRAJ_SUCCESS(err))
1059 strat_want_pack = 0;
1061 /* do all lines of circuit */
1062 for (idx = 1; idx < len; idx ++) {
1064 linenum = circuit_wpline[idx].line_num;
1065 dir = circuit_wpline[idx].dir;
1067 DEBUG(E_USER_STRAT, "%s(): line %d dir %d -> line %d dir %d",
1068 __FUNCTION__, prev_linenum, prev_dir, linenum, dir);
1069 err = line2line(prev_linenum, prev_dir, linenum, dir,
1070 TRAJ_FLAGS_NO_NEAR);
1072 /* in some cases it is better to wait that obstacle is
1073 * gone before starting to avoid it */
1074 if (err == END_OBSTACLE &&
1075 strat_conf.flags & STRAT_CONF_WAIT_OBSTACLE &&
1076 time_get_s() > strat_conf.prev_wait_obstacle + 5) {
1077 strat_conf.prev_wait_obstacle = time_get_s();
1081 if (!TRAJ_SUCCESS(err))
1084 prev_linenum = linenum;
1090 strat_want_pack = 0;
1094 /* list of waypoints when we are not on a circuit */
1095 const struct xy_point unblock_pts[] = {
1096 { .x = 375, .y = 597 }, /* 1,1 */
1097 { .x = 2625, .y = 597 }, /* 11,1 */
1098 { .x = 1500, .y = 722 }, /* 6,2 */
1099 { .x = 375, .y = 1097 }, /* 1,3 */
1100 { .x = 375, .y = 1597 }, /* 1,5 */
1101 { .x = 2625, .y = 1097 }, /* 11,3 */
1102 { .x = 2625, .y = 1597 }, /* 11,5 */
1103 { .x = 1500, .y = 1722 }, /* 6,6 */
1107 /* try to unblock in any situation */
1108 uint8_t strat_unblock(void)
1110 int16_t x, y, posx, posy;
1112 uint16_t old_dspeed, old_aspeed;
1114 uint16_t d_min = 0x7FFF, d;
1115 const struct xy_point *pt;
1117 DEBUG(E_USER_STRAT, "%s()", __FUNCTION__);
1119 strat_want_pack = 1;
1120 strat_get_speed(&old_dspeed, &old_aspeed);
1123 strat_set_speed(SPEED_DIST_SLOW, SPEED_ANGLE_SLOW);
1124 posx = position_get_x_s16(&mainboard.pos);
1125 posy = position_get_y_s16(&mainboard.pos);
1129 if (xycoord_to_ijcoord_not_corn(&x, &y, &i, &j) < 0)
1131 else if (strat_db.wp_table[i][j].on_circuit == 0)
1134 /* find the nearest unblock point */
1137 /* browse all points and find the nearest */
1138 for (k = 0; k < sizeof(unblock_pts)/sizeof(*unblock_pts); k++) {
1139 pt = &unblock_pts[k];
1140 d = distance_between(posx, posy, pt->x, COLOR_Y(pt->y));
1148 DEBUG(E_USER_STRAT, "%s() unblock point is %d,%d",
1149 __FUNCTION__, x, y);
1151 /* XXX if opponent is too close, go back, or wait ? */
1153 /* go to nearest waypoint */
1154 trajectory_goto_xy_abs(&mainboard.traj, x, y);
1155 err = wait_traj_end(TRAJ_FLAGS_NO_NEAR);
1156 if (err == END_TIMER)
1159 if (!TRAJ_SUCCESS(err))
1162 strat_set_speed(old_dspeed, old_aspeed);
1166 void strat_avoid_init(void)
1168 init_all_circuits();
1170 #ifdef TEST_STRAT_AVOID
1172 const struct circuit *selected_circuit;
1173 int8_t selected_face;
1174 struct wp_line circuit_wpline[MAX_CIRCUIT_WPLINE];
1178 printf_P(PSTR("========= i=%d, j=%d\r\n"), i, j);
1180 ts = 0; bc = 0; cc = 0;
1181 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1182 find_best_circuit(i, j, &selected_circuit, &selected_face);
1183 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1184 dump_circuit_wp(circuit_wpline, ret);
1186 ts = 0; bc = 3; cc = 0;
1187 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1188 find_best_circuit(i, j, &selected_circuit, &selected_face);
1189 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1190 dump_circuit_wp(circuit_wpline, ret);
1192 ts = 0; bc = 4; cc = 0;
1193 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1194 find_best_circuit(i, j, &selected_circuit, &selected_face);
1195 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1196 dump_circuit_wp(circuit_wpline, ret);
1198 ts = 0; bc = 3; cc = 5;
1199 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1200 find_best_circuit(i, j, &selected_circuit, &selected_face);
1201 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1202 dump_circuit_wp(circuit_wpline, ret);
1204 ts = 0; bc = 4; cc = 5;
1205 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1206 find_best_circuit(i, j, &selected_circuit, &selected_face);
1207 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1208 dump_circuit_wp(circuit_wpline, ret);
1210 ts = 80; bc = 0; cc = 0;
1211 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1212 find_best_circuit(i, j, &selected_circuit, &selected_face);
1213 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1214 dump_circuit_wp(circuit_wpline, ret);
1217 printf_P(PSTR("========= i=%d, j=%d\r\n"), i, j);
1219 ts = 0; bc = 0; cc = 0;
1220 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1221 find_best_circuit(i, j, &selected_circuit, &selected_face);
1222 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1223 dump_circuit_wp(circuit_wpline, ret);
1225 ts = 0; bc = 3; cc = 0;
1226 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1227 find_best_circuit(i, j, &selected_circuit, &selected_face);
1228 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1229 dump_circuit_wp(circuit_wpline, ret);
1231 ts = 80; bc = 0; cc = 0;
1232 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1233 find_best_circuit(i, j, &selected_circuit, &selected_face);
1234 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1235 dump_circuit_wp(circuit_wpline, ret);
1238 printf_P(PSTR("========= i=%d, j=%d\r\n"), i, j);
1240 ts = 0; bc = 0; cc = 0;
1241 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1242 find_best_circuit(i, j, &selected_circuit, &selected_face);
1243 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1244 dump_circuit_wp(circuit_wpline, ret);
1246 ts = 0; bc = 3; cc = 0;
1247 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1248 find_best_circuit(i, j, &selected_circuit, &selected_face);
1249 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1250 dump_circuit_wp(circuit_wpline, ret);
1252 ts = 80; bc = 0; cc = 0;
1253 printf_P(PSTR("=== time=%"PRIu32", ball=%d, corn=%d\r\n"), ts, bc, cc);
1254 find_best_circuit(i, j, &selected_circuit, &selected_face);
1255 ret = get_path(selected_circuit, i, j, selected_face, circuit_wpline);
1256 dump_circuit_wp(circuit_wpline, ret);