Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/movement.cpp@20:859c7aebf8a6, 2017-03-02 (annotated)
- Committer:
- cittecla
- Date:
- Thu Mar 02 16:20:41 2017 +0000
- Revision:
- 20:859c7aebf8a6
- Parent:
- 19:baa8371d55b4
- Child:
- 21:cb40c0533bc2
Movement implemented;
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 | 18:a82994e67297 | 5 | |
cittecla | 18:a82994e67297 | 6 | |
cittecla | 18:a82994e67297 | 7 | #include "mbed.h" |
cittecla | 19:baa8371d55b4 | 8 | #include "move.h" |
cittecla | 18:a82994e67297 | 9 | |
cittecla | 19:baa8371d55b4 | 10 | static double time_counter = 0.0f; |
cittecla | 19:baa8371d55b4 | 11 | static double timer0 = 0.0f; |
cittecla | 20:859c7aebf8a6 | 12 | static float PID_correction_value = 1.0f; |
cittecla | 19:baa8371d55b4 | 13 | |
cittecla | 19:baa8371d55b4 | 14 | static float power_value_slow = 0.6f; |
cittecla | 19:baa8371d55b4 | 15 | static float power_value_medium = 0.7f; |
cittecla | 19:baa8371d55b4 | 16 | static float power_value_fast = 0.8f; |
cittecla | 19:baa8371d55b4 | 17 | static float ludicrous_value = 1.0f; |
cittecla | 19:baa8371d55b4 | 18 | |
cittecla | 20:859c7aebf8a6 | 19 | |
cittecla | 19:baa8371d55b4 | 20 | //motor stuff |
cittecla | 19:baa8371d55b4 | 21 | DigitalOut enableMotorDriver(PB_2); |
cittecla | 19:baa8371d55b4 | 22 | PwmOut pwmL(PA_8); |
cittecla | 19:baa8371d55b4 | 23 | PwmOut pwmR(PA_9); |
cittecla | 18:a82994e67297 | 24 | |
cittecla | 18:a82994e67297 | 25 | void move_init() |
cittecla | 18:a82994e67297 | 26 | { |
cittecla | 19:baa8371d55b4 | 27 | pwmL.period(0.00005f); // Setzt die Periode auf 50 μs |
cittecla | 19:baa8371d55b4 | 28 | pwmR.period(0.00005f); |
cittecla | 19:baa8371d55b4 | 29 | |
cittecla | 19:baa8371d55b4 | 30 | pwmL = 0.5f; // Setzt die Duty-Cycle auf 50% |
cittecla | 19:baa8371d55b4 | 31 | pwmR = 0.5f; |
cittecla | 19:baa8371d55b4 | 32 | enableMotorDriver = 0; |
cittecla | 18:a82994e67297 | 33 | } |
cittecla | 18:a82994e67297 | 34 | |
cittecla | 20:859c7aebf8a6 | 35 | void move_forward_slow(float correction_value) |
cittecla | 18:a82994e67297 | 36 | { |
cittecla | 19:baa8371d55b4 | 37 | pwmL = power_value_slow; |
cittecla | 19:baa8371d55b4 | 38 | pwmR = 1-power_value_slow*correction_value; |
cittecla | 19:baa8371d55b4 | 39 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 40 | } |
cittecla | 18:a82994e67297 | 41 | |
cittecla | 20:859c7aebf8a6 | 42 | void move_forward_medium(float correction_value) |
cittecla | 18:a82994e67297 | 43 | { |
cittecla | 19:baa8371d55b4 | 44 | pwmL = power_value_medium; |
cittecla | 19:baa8371d55b4 | 45 | pwmR = 1-power_value_medium*correction_value; |
cittecla | 19:baa8371d55b4 | 46 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 47 | } |
cittecla | 18:a82994e67297 | 48 | |
cittecla | 20:859c7aebf8a6 | 49 | void move_forward_fast(float correction_value) |
cittecla | 18:a82994e67297 | 50 | { |
cittecla | 19:baa8371d55b4 | 51 | pwmL = power_value_fast; |
cittecla | 19:baa8371d55b4 | 52 | pwmR = 1-power_value_fast*correction_value; |
cittecla | 19:baa8371d55b4 | 53 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 54 | } |
cittecla | 18:a82994e67297 | 55 | |
cittecla | 20:859c7aebf8a6 | 56 | void move_backward_slow(float correction_value) |
cittecla | 18:a82994e67297 | 57 | { |
cittecla | 19:baa8371d55b4 | 58 | pwmL = 1-power_value_slow*correction_value; |
cittecla | 19:baa8371d55b4 | 59 | pwmR = power_value_slow; |
cittecla | 19:baa8371d55b4 | 60 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 61 | } |
cittecla | 18:a82994e67297 | 62 | |
cittecla | 20:859c7aebf8a6 | 63 | void move_backward_medium(float correction_value) |
cittecla | 18:a82994e67297 | 64 | { |
cittecla | 19:baa8371d55b4 | 65 | pwmL = 1-power_value_slow*correction_value; |
cittecla | 19:baa8371d55b4 | 66 | pwmR = power_value_slow; |
cittecla | 19:baa8371d55b4 | 67 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 68 | } |
cittecla | 18:a82994e67297 | 69 | |
cittecla | 20:859c7aebf8a6 | 70 | void move_backward_fast(float correction_value) |
cittecla | 18:a82994e67297 | 71 | { |
cittecla | 19:baa8371d55b4 | 72 | pwmL = 1-power_value_slow*correction_value; |
cittecla | 19:baa8371d55b4 | 73 | pwmR = power_value_slow; |
cittecla | 19:baa8371d55b4 | 74 | enableMotorDriver = 1; |
cittecla | 19:baa8371d55b4 | 75 | } |
cittecla | 18:a82994e67297 | 76 | |
cittecla | 19:baa8371d55b4 | 77 | void stop_movement() |
cittecla | 19:baa8371d55b4 | 78 | { |
cittecla | 19:baa8371d55b4 | 79 | enableMotorDriver = 1; |
cittecla | 18:a82994e67297 | 80 | } |
cittecla | 18:a82994e67297 | 81 | |
cittecla | 20:859c7aebf8a6 | 82 | void sync_movement(bool speed, bool direction) |
cittecla | 20:859c7aebf8a6 | 83 | // PID correction Value calcualtion |
cittecla | 18:a82994e67297 | 84 | |
cittecla | 20:859c7aebf8a6 | 85 | |
cittecla | 20:859c7aebf8a6 | 86 | |
cittecla | 20:859c7aebf8a6 | 87 | |
cittecla | 20:859c7aebf8a6 | 88 | |
cittecla | 20:859c7aebf8a6 | 89 | |
cittecla | 20:859c7aebf8a6 | 90 | |
cittecla | 20:859c7aebf8a6 | 91 | |
cittecla | 20:859c7aebf8a6 | 92 | // Call movement: |
cittecla | 20:859c7aebf8a6 | 93 | // direction 0 = backward, direction 1 = forward |
cittecla | 20:859c7aebf8a6 | 94 | // speed 0 = slow, speed 1 = medium |
cittecla | 20:859c7aebf8a6 | 95 | { |
cittecla | 20:859c7aebf8a6 | 96 | if(direction && speed) { |
cittecla | 20:859c7aebf8a6 | 97 | move_forward_medium(PID_correction_value); |
cittecla | 20:859c7aebf8a6 | 98 | } |
cittecla | 20:859c7aebf8a6 | 99 | if(direction && !speed) { |
cittecla | 20:859c7aebf8a6 | 100 | move_forward_slow(PID_correction_value); |
cittecla | 20:859c7aebf8a6 | 101 | } |
cittecla | 20:859c7aebf8a6 | 102 | if(!direction && speed) { |
cittecla | 20:859c7aebf8a6 | 103 | move_backward_medium(PID_correction_value); |
cittecla | 20:859c7aebf8a6 | 104 | } |
cittecla | 20:859c7aebf8a6 | 105 | if(!direction && !speed { |
cittecla | 20:859c7aebf8a6 | 106 | move_backward_slow(PID_correction_value); |
cittecla | 20:859c7aebf8a6 | 107 | } |
cittecla | 20:859c7aebf8a6 | 108 | } |
cittecla | 20:859c7aebf8a6 | 109 | |
cittecla | 20:859c7aebf8a6 | 110 | void termiante_sync_movement(){ |
cittecla | 20:859c7aebf8a6 | 111 | PID_correction_value = 1.0f; |
cittecla | 20:859c7aebf8a6 | 112 | } |
cittecla | 20:859c7aebf8a6 | 113 |