Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Movement.cpp@71:ddf4eb5c3081, 2017-04-20 (annotated)
- Committer:
- cittecla
- Date:
- Thu Apr 20 11:37:24 2017 +0000
- Revision:
- 71:ddf4eb5c3081
- Parent:
- 63:b27aa01c2cf6
- Child:
- 74:d9c387b83196
- Child:
- 75:dba260cb5ae4
Init runs successfully, not tested with robot, parameters for init not final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 18:a82994e67297 | 1 | /** |
cittecla | 18:a82994e67297 | 2 | * Movement function library |
cittecla | 18:a82994e67297 | 3 | * Handels Movement of the Robot |
cittecla | 18:a82994e67297 | 4 | **/ |
cittecla | 39:92723f7ea54f | 5 | |
cittecla | 39:92723f7ea54f | 6 | #include "Movement.h" |
cittecla | 39:92723f7ea54f | 7 | |
cittecla | 61:628f8a4e857c | 8 | bool is_turning = false; |
cittecla | 61:628f8a4e857c | 9 | bool direction = false; |
cittecla | 61:628f8a4e857c | 10 | float wanted_deg = 0; |
cittecla | 61:628f8a4e857c | 11 | float current_deg = 0; |
cittecla | 63:b27aa01c2cf6 | 12 | Timer t; |
cittecla | 61:628f8a4e857c | 13 | float previous_t = 0; |
cittecla | 61:628f8a4e857c | 14 | |
cittecla | 52:56399c2f13cd | 15 | int moving() |
cittecla | 52:56399c2f13cd | 16 | { |
cittecla | 52:56399c2f13cd | 17 | |
cittecla | 39:92723f7ea54f | 18 | return 0; |
cittecla | 52:56399c2f13cd | 19 | } |
cittecla | 52:56399c2f13cd | 20 | |
cittecla | 52:56399c2f13cd | 21 | int move_forward_for_distance(float distance) |
cittecla | 52:56399c2f13cd | 22 | { |
cittecla | 52:56399c2f13cd | 23 | |
cittecla | 39:92723f7ea54f | 24 | return 0; |
cittecla | 52:56399c2f13cd | 25 | } |
cittecla | 52:56399c2f13cd | 26 | |
cittecla | 52:56399c2f13cd | 27 | int move_backward_for_distance() |
cittecla | 52:56399c2f13cd | 28 | { |
cittecla | 52:56399c2f13cd | 29 | |
cittecla | 39:92723f7ea54f | 30 | return 0; |
cittecla | 52:56399c2f13cd | 31 | } |
cittecla | 52:56399c2f13cd | 32 | |
cittecla | 61:628f8a4e857c | 33 | void turn_for_deg(float deg) |
cittecla | 52:56399c2f13cd | 34 | { |
cittecla | 52:56399c2f13cd | 35 | |
cittecla | 61:628f8a4e857c | 36 | if(is_turning == false) { |
cittecla | 61:628f8a4e857c | 37 | |
cittecla | 61:628f8a4e857c | 38 | is_turning = true; |
cittecla | 61:628f8a4e857c | 39 | float left = 0; |
cittecla | 61:628f8a4e857c | 40 | float right = 0; |
cittecla | 61:628f8a4e857c | 41 | |
cittecla | 61:628f8a4e857c | 42 | wanted_deg = deg; |
cittecla | 61:628f8a4e857c | 43 | current_deg = 0; |
cittecla | 61:628f8a4e857c | 44 | |
cittecla | 61:628f8a4e857c | 45 | if(deg < 0) { // turn left |
cittecla | 61:628f8a4e857c | 46 | direction = 1; |
cittecla | 61:628f8a4e857c | 47 | left = -50.0f; |
cittecla | 61:628f8a4e857c | 48 | right = 50.0f; |
cittecla | 61:628f8a4e857c | 49 | } else { // turn right |
cittecla | 61:628f8a4e857c | 50 | direction = 0; |
cittecla | 61:628f8a4e857c | 51 | left = 50.0f; |
cittecla | 61:628f8a4e857c | 52 | right = -50.0f; |
cittecla | 61:628f8a4e857c | 53 | } |
cittecla | 61:628f8a4e857c | 54 | set_speed(left, right); |
cittecla | 71:ddf4eb5c3081 | 55 | t.reset(); |
cittecla | 61:628f8a4e857c | 56 | previous_t = 0; |
cittecla | 61:628f8a4e857c | 57 | t.start(); |
cittecla | 61:628f8a4e857c | 58 | |
cittecla | 61:628f8a4e857c | 59 | } else { |
cittecla | 61:628f8a4e857c | 60 | |
cittecla | 61:628f8a4e857c | 61 | float speed_left = get_speed_left(); |
cittecla | 61:628f8a4e857c | 62 | float delta_t = t - previous_t; |
cittecla | 61:628f8a4e857c | 63 | previous_t = t; |
cittecla | 61:628f8a4e857c | 64 | current_deg += 360.0f / (2*radius*M_PI) * delta_t * speed_left; |
cittecla | 61:628f8a4e857c | 65 | if(current_deg * current_deg > wanted_deg * wanted_deg) { |
cittecla | 61:628f8a4e857c | 66 | set_speed(0,0); |
cittecla | 61:628f8a4e857c | 67 | is_turning = false; |
cittecla | 61:628f8a4e857c | 68 | t.stop(); |
cittecla | 61:628f8a4e857c | 69 | } |
cittecla | 61:628f8a4e857c | 70 | } |
cittecla | 52:56399c2f13cd | 71 | } |
cittecla | 52:56399c2f13cd | 72 | |
cittecla | 52:56399c2f13cd | 73 | |
cittecla | 52:56399c2f13cd | 74 | int move_to_next_coord() |
cittecla | 52:56399c2f13cd | 75 | { |
cittecla | 52:56399c2f13cd | 76 | |
cittecla | 52:56399c2f13cd | 77 | float current_heading = get_current_heading(); |
cittecla | 53:453f24775644 | 78 | position current_pos = get_current_pos(); |
cittecla | 52:56399c2f13cd | 79 | position next_pos = get_next_pos(); |
cittecla | 61:628f8a4e857c | 80 | |
cittecla | 52:56399c2f13cd | 81 | float needed_heading = 0; |
cittecla | 52:56399c2f13cd | 82 | float distance = 0; |
cittecla | 61:628f8a4e857c | 83 | |
cittecla | 52:56399c2f13cd | 84 | // nord(-y) = 0 grad |
cittecla | 61:628f8a4e857c | 85 | if(current_pos.y > next_pos.y) { |
cittecla | 61:628f8a4e857c | 86 | if(current_pos.x > next_pos.x) needed_heading = 315; |
cittecla | 61:628f8a4e857c | 87 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 88 | if(current_pos.x == next_pos.x) needed_heading = 0; |
cittecla | 61:628f8a4e857c | 89 | distance = 1; |
cittecla | 61:628f8a4e857c | 90 | if(current_pos.x < next_pos.x) needed_heading = 45; |
cittecla | 61:628f8a4e857c | 91 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 92 | } |
cittecla | 61:628f8a4e857c | 93 | if(current_pos.y == next_pos.y) { |
cittecla | 61:628f8a4e857c | 94 | if(current_pos.x > next_pos.x) needed_heading = 270; |
cittecla | 61:628f8a4e857c | 95 | distance = 1; |
cittecla | 53:453f24775644 | 96 | if(current_pos.x == next_pos.x) //error same position; |
cittecla | 61:628f8a4e857c | 97 | if(current_pos.x < next_pos.x) needed_heading = 90; |
cittecla | 61:628f8a4e857c | 98 | distance = 1; |
cittecla | 61:628f8a4e857c | 99 | } |
cittecla | 61:628f8a4e857c | 100 | if(current_pos.y < next_pos.y) { |
cittecla | 61:628f8a4e857c | 101 | if(current_pos.x > next_pos.x) needed_heading = 225; |
cittecla | 61:628f8a4e857c | 102 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 103 | if(current_pos.x == next_pos.x) needed_heading = 180; |
cittecla | 61:628f8a4e857c | 104 | distance = 1; |
cittecla | 61:628f8a4e857c | 105 | if(current_pos.x < next_pos.x) needed_heading = 135; |
cittecla | 61:628f8a4e857c | 106 | distance = sqrt2; |
cittecla | 61:628f8a4e857c | 107 | } |
cittecla | 52:56399c2f13cd | 108 | |
cittecla | 52:56399c2f13cd | 109 | if(needed_heading != current_heading) { |
cittecla | 53:453f24775644 | 110 | turn_for_deg((needed_heading-current_heading)); |
cittecla | 61:628f8a4e857c | 111 | } else { |
cittecla | 53:453f24775644 | 112 | move_forward_for_distance(distance); |
cittecla | 39:92723f7ea54f | 113 | } |
cittecla | 39:92723f7ea54f | 114 | return 0; |
cittecla | 52:56399c2f13cd | 115 | } |
cittecla | 52:56399c2f13cd | 116 | |
cittecla | 52:56399c2f13cd | 117 | int move_in_search_for_brick() |
cittecla | 52:56399c2f13cd | 118 | { |
cittecla | 52:56399c2f13cd | 119 | |
cittecla | 52:56399c2f13cd | 120 | return 0; |
cittecla | 52:56399c2f13cd | 121 | } |
cittecla | 52:56399c2f13cd | 122 | |
cittecla | 39:92723f7ea54f | 123 | |
cittecla | 39:92723f7ea54f | 124 | |
cittecla | 39:92723f7ea54f | 125 | |
cittecla | 39:92723f7ea54f | 126 | |
cittecla | 39:92723f7ea54f | 127 | |
cittecla | 39:92723f7ea54f | 128 | |
cittecla | 39:92723f7ea54f | 129 |