Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Robot.cpp@42:08b3aea29254, 2017-04-11 (annotated)
- Committer:
- aeschsim
- Date:
- Tue Apr 11 13:34:36 2017 +0000
- Revision:
- 42:08b3aea29254
- Parent:
- 38:3526c36e4c73
- Child:
- 43:9f291a496db8
new servo functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 34:40d8d29b44b8 | 1 | // Roboter file |
cittecla | 34:40d8d29b44b8 | 2 | |
cittecla | 34:40d8d29b44b8 | 3 | |
cittecla | 34:40d8d29b44b8 | 4 | #include "mbed.h" |
cittecla | 34:40d8d29b44b8 | 5 | #include "Robot.h" |
cittecla | 34:40d8d29b44b8 | 6 | #include "EncoderCounter.h" |
cittecla | 34:40d8d29b44b8 | 7 | #include "LowpassFilter.h" |
cittecla | 34:40d8d29b44b8 | 8 | #include "IMU.h" |
cittecla | 34:40d8d29b44b8 | 9 | |
cittecla | 34:40d8d29b44b8 | 10 | #include "IRSensor.h" |
cittecla | 34:40d8d29b44b8 | 11 | |
cittecla | 34:40d8d29b44b8 | 12 | |
cittecla | 34:40d8d29b44b8 | 13 | const float PERIOD = 0.001f; // period of control task, given in [s] |
cittecla | 34:40d8d29b44b8 | 14 | const float COUNTS_PER_TURN = 1200.0f; // resolution of encoder counter |
cittecla | 34:40d8d29b44b8 | 15 | const float LOWPASS_FILTER_FREQUENCY = 300.0f; // frequency of lowpass filter for actual speed values, given in [rad/s] |
cittecla | 34:40d8d29b44b8 | 16 | const float KN = 40.0f; // speed constant of motor, given in [rpm/V] |
cittecla | 34:40d8d29b44b8 | 17 | const float KP = 0.2f; // speed controller gain, given in [V/rpm] |
cittecla | 34:40d8d29b44b8 | 18 | const float MAX_VOLTAGE = 12.0f; // supply voltage for power stage in [V] |
cittecla | 34:40d8d29b44b8 | 19 | const float MIN_DUTY_CYCLE = 0.02f; // minimum allowed value for duty cycle (2%) |
cittecla | 34:40d8d29b44b8 | 20 | const float MAX_DUTY_CYCLE = 0.98f; // maximum allowed value for duty cycle (98%) |
cittecla | 34:40d8d29b44b8 | 21 | const float correction_value = 2.45f; // correction value for desired speed |
cittecla | 34:40d8d29b44b8 | 22 | |
cittecla | 34:40d8d29b44b8 | 23 | LowpassFilter speedLeftFilter; |
cittecla | 34:40d8d29b44b8 | 24 | LowpassFilter speedRightFilter; |
cittecla | 34:40d8d29b44b8 | 25 | |
cittecla | 34:40d8d29b44b8 | 26 | //Motor stuff |
cittecla | 34:40d8d29b44b8 | 27 | EncoderCounter counterLeft(PB_6, PB_7); |
cittecla | 34:40d8d29b44b8 | 28 | EncoderCounter counterRight(PA_6, PC_7); |
cittecla | 34:40d8d29b44b8 | 29 | |
cittecla | 34:40d8d29b44b8 | 30 | DigitalOut enableMotorDriver(PB_2); |
cittecla | 34:40d8d29b44b8 | 31 | PwmOut pwmLeft(PA_8); |
cittecla | 34:40d8d29b44b8 | 32 | PwmOut pwmRight(PA_9); |
cittecla | 34:40d8d29b44b8 | 33 | |
cittecla | 34:40d8d29b44b8 | 34 | DigitalOut my_led(LED1); |
cittecla | 34:40d8d29b44b8 | 35 | |
cittecla | 34:40d8d29b44b8 | 36 | //Periphery for distance sensors |
cittecla | 34:40d8d29b44b8 | 37 | AnalogIn distance(PB_1); |
cittecla | 34:40d8d29b44b8 | 38 | DigitalOut enable(PC_1); |
cittecla | 34:40d8d29b44b8 | 39 | DigitalOut bit0(PH_1); |
cittecla | 34:40d8d29b44b8 | 40 | DigitalOut bit1(PC_2); |
cittecla | 34:40d8d29b44b8 | 41 | DigitalOut bit2(PC_3); |
cittecla | 34:40d8d29b44b8 | 42 | |
cittecla | 34:40d8d29b44b8 | 43 | IRSensor sensors[6]; |
cittecla | 34:40d8d29b44b8 | 44 | |
cittecla | 34:40d8d29b44b8 | 45 | //indicator leds arround robot |
cittecla | 34:40d8d29b44b8 | 46 | DigitalOut leds[] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 }; |
cittecla | 34:40d8d29b44b8 | 47 | |
cittecla | 34:40d8d29b44b8 | 48 | |
cittecla | 34:40d8d29b44b8 | 49 | //Periphery for the IMU |
cittecla | 34:40d8d29b44b8 | 50 | SPI spi(PC_12, PC_11, PC_10); |
cittecla | 34:40d8d29b44b8 | 51 | DigitalOut csG(PA_15); |
cittecla | 34:40d8d29b44b8 | 52 | DigitalOut csXM(PD_2); |
cittecla | 34:40d8d29b44b8 | 53 | |
cittecla | 34:40d8d29b44b8 | 54 | IMU imu(spi, csG, csXM); |
cittecla | 34:40d8d29b44b8 | 55 | |
cittecla | 34:40d8d29b44b8 | 56 | //Periphery for I2C |
cittecla | 34:40d8d29b44b8 | 57 | I2C i2c(PB_9, PB_8); |
cittecla | 34:40d8d29b44b8 | 58 | DigitalOut Servo_enable(PA_10); |
cittecla | 34:40d8d29b44b8 | 59 | |
cittecla | 34:40d8d29b44b8 | 60 | |
cittecla | 34:40d8d29b44b8 | 61 | short previousValueCounterRight = 0; |
cittecla | 34:40d8d29b44b8 | 62 | short previousValueCounterLeft = 0; |
cittecla | 34:40d8d29b44b8 | 63 | |
cittecla | 34:40d8d29b44b8 | 64 | float desiredSpeedLeft = 0; |
cittecla | 34:40d8d29b44b8 | 65 | float desiredSpeedRight = 0; |
cittecla | 34:40d8d29b44b8 | 66 | |
cittecla | 34:40d8d29b44b8 | 67 | float actualSpeedLeft; |
cittecla | 34:40d8d29b44b8 | 68 | float actualSpeedRight; |
cittecla | 34:40d8d29b44b8 | 69 | |
cittecla | 34:40d8d29b44b8 | 70 | |
cittecla | 34:40d8d29b44b8 | 71 | |
cittecla | 34:40d8d29b44b8 | 72 | |
cittecla | 34:40d8d29b44b8 | 73 | void Robot_init_all() |
cittecla | 34:40d8d29b44b8 | 74 | { |
cittecla | 34:40d8d29b44b8 | 75 | Speedcontroller_init(); |
cittecla | 34:40d8d29b44b8 | 76 | Sensor_init(); |
cittecla | 34:40d8d29b44b8 | 77 | } |
cittecla | 34:40d8d29b44b8 | 78 | |
cittecla | 34:40d8d29b44b8 | 79 | //speed controll |
cittecla | 34:40d8d29b44b8 | 80 | void Speedcontroller_init() |
cittecla | 34:40d8d29b44b8 | 81 | { |
cittecla | 34:40d8d29b44b8 | 82 | // Initialisieren der PWM Ausgaenge pwmLeft.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 83 | pwmLeft.period(0.00005f); // Setzt die Periode auf 50 μs |
cittecla | 34:40d8d29b44b8 | 84 | pwmRight.period(0.00005f); |
cittecla | 34:40d8d29b44b8 | 85 | pwmLeft = 0.5f; // Duty-Cycle von 50% pwmRight.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 86 | pwmRight = 0.5f; // Duty-Cycle von 50% |
cittecla | 34:40d8d29b44b8 | 87 | |
cittecla | 34:40d8d29b44b8 | 88 | // Initialisieren von lokalen Variabeln |
cittecla | 34:40d8d29b44b8 | 89 | previousValueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 90 | previousValueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 91 | speedLeftFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 92 | speedLeftFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 93 | speedRightFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 94 | speedRightFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 95 | |
cittecla | 34:40d8d29b44b8 | 96 | desiredSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 97 | desiredSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 98 | actualSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 99 | actualSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 100 | |
cittecla | 34:40d8d29b44b8 | 101 | Ticker t1; |
cittecla | 34:40d8d29b44b8 | 102 | t1.attach( &speedCtrl, PERIOD); |
cittecla | 34:40d8d29b44b8 | 103 | |
cittecla | 34:40d8d29b44b8 | 104 | //desiredSpeedLeft = 50.0f+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 105 | //desiredSpeedRight = -50.0f; //50 RPM |
cittecla | 34:40d8d29b44b8 | 106 | enableMotorDriver = 1; |
cittecla | 34:40d8d29b44b8 | 107 | } |
cittecla | 34:40d8d29b44b8 | 108 | |
cittecla | 34:40d8d29b44b8 | 109 | void set_speed(float left, float right) |
cittecla | 34:40d8d29b44b8 | 110 | { |
cittecla | 34:40d8d29b44b8 | 111 | desiredSpeedLeft = left+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 112 | desiredSpeedRight = -right; //50 RPM |
cittecla | 34:40d8d29b44b8 | 113 | } |
cittecla | 34:40d8d29b44b8 | 114 | |
cittecla | 34:40d8d29b44b8 | 115 | |
cittecla | 34:40d8d29b44b8 | 116 | void speedCtrl() |
cittecla | 34:40d8d29b44b8 | 117 | { |
cittecla | 34:40d8d29b44b8 | 118 | // Berechnen die effektiven Drehzahlen der Motoren in [rpm] |
cittecla | 34:40d8d29b44b8 | 119 | short valueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 120 | short valueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 121 | short countsInPastPeriodLeft = valueCounterLeft-previousValueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 122 | short countsInPastPeriodRight = valueCounterRight-previousValueCounterRight; |
cittecla | 34:40d8d29b44b8 | 123 | |
cittecla | 34:40d8d29b44b8 | 124 | previousValueCounterLeft = valueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 125 | previousValueCounterRight = valueCounterRight; |
cittecla | 34:40d8d29b44b8 | 126 | actualSpeedLeft = speedLeftFilter.filter((float)countsInPastPeriodLeft /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 127 | actualSpeedRight = speedRightFilter.filter((float)countsInPastPeriodRight /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 128 | |
cittecla | 34:40d8d29b44b8 | 129 | // Berechnen der Motorspannungen Uout |
cittecla | 34:40d8d29b44b8 | 130 | float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN; |
cittecla | 34:40d8d29b44b8 | 131 | float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN; |
cittecla | 34:40d8d29b44b8 | 132 | |
cittecla | 34:40d8d29b44b8 | 133 | // Berechnen, Limitieren und Setzen der Duty-Cycle |
cittecla | 34:40d8d29b44b8 | 134 | float dutyCycleLeft = 0.5f+0.5f*voltageLeft/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 135 | if (dutyCycleLeft < MIN_DUTY_CYCLE) dutyCycleLeft = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 136 | else if (dutyCycleLeft > MAX_DUTY_CYCLE) dutyCycleLeft = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 137 | |
cittecla | 34:40d8d29b44b8 | 138 | pwmLeft = dutyCycleLeft; |
cittecla | 34:40d8d29b44b8 | 139 | float dutyCycleRight = 0.5f+0.5f*voltageRight/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 140 | if (dutyCycleRight < MIN_DUTY_CYCLE) dutyCycleRight = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 141 | else if (dutyCycleRight > MAX_DUTY_CYCLE) dutyCycleRight = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 142 | |
cittecla | 34:40d8d29b44b8 | 143 | pwmRight = dutyCycleRight; |
cittecla | 34:40d8d29b44b8 | 144 | } |
cittecla | 34:40d8d29b44b8 | 145 | |
cittecla | 34:40d8d29b44b8 | 146 | //Sensors |
cittecla | 34:40d8d29b44b8 | 147 | void Sensor_init() |
cittecla | 34:40d8d29b44b8 | 148 | { |
cittecla | 34:40d8d29b44b8 | 149 | for( int ii = 0; ii<6; ++ii) |
cittecla | 34:40d8d29b44b8 | 150 | sensors[ii].init(&distance, &bit0, &bit1, &bit2, ii); |
cittecla | 34:40d8d29b44b8 | 151 | } |
cittecla | 34:40d8d29b44b8 | 152 | |
cittecla | 34:40d8d29b44b8 | 153 | float getDistanceIR(int number) |
cittecla | 34:40d8d29b44b8 | 154 | { |
cittecla | 34:40d8d29b44b8 | 155 | return sensors[number]; |
cittecla | 34:40d8d29b44b8 | 156 | } |
cittecla | 34:40d8d29b44b8 | 157 | |
cittecla | 34:40d8d29b44b8 | 158 | //IMU |
cittecla | 34:40d8d29b44b8 | 159 | float read_acc_x() |
cittecla | 34:40d8d29b44b8 | 160 | { |
cittecla | 34:40d8d29b44b8 | 161 | return imu.readAccelerationX(); |
cittecla | 34:40d8d29b44b8 | 162 | } |
cittecla | 34:40d8d29b44b8 | 163 | |
cittecla | 34:40d8d29b44b8 | 164 | float read_acc_y() |
cittecla | 34:40d8d29b44b8 | 165 | { |
cittecla | 34:40d8d29b44b8 | 166 | return imu.readAccelerationY(); |
cittecla | 34:40d8d29b44b8 | 167 | } |
cittecla | 34:40d8d29b44b8 | 168 | |
cittecla | 34:40d8d29b44b8 | 169 | float read_acc_z() |
cittecla | 34:40d8d29b44b8 | 170 | { |
cittecla | 34:40d8d29b44b8 | 171 | return imu.readAccelerationZ(); |
cittecla | 34:40d8d29b44b8 | 172 | } |
cittecla | 34:40d8d29b44b8 | 173 | |
cittecla | 34:40d8d29b44b8 | 174 | float read_gyr_x() |
cittecla | 34:40d8d29b44b8 | 175 | { |
cittecla | 34:40d8d29b44b8 | 176 | return imu.readGyroX(); |
cittecla | 34:40d8d29b44b8 | 177 | } |
cittecla | 34:40d8d29b44b8 | 178 | |
cittecla | 34:40d8d29b44b8 | 179 | float read_gyr_y() |
cittecla | 34:40d8d29b44b8 | 180 | { |
cittecla | 34:40d8d29b44b8 | 181 | return imu.readGyroY(); |
cittecla | 34:40d8d29b44b8 | 182 | } |
cittecla | 34:40d8d29b44b8 | 183 | |
cittecla | 34:40d8d29b44b8 | 184 | float read_gyr_z() |
cittecla | 34:40d8d29b44b8 | 185 | { |
cittecla | 34:40d8d29b44b8 | 186 | return imu.readGyroZ(); |
cittecla | 34:40d8d29b44b8 | 187 | } |
cittecla | 34:40d8d29b44b8 | 188 | |
cittecla | 34:40d8d29b44b8 | 189 | float read_heading() |
cittecla | 34:40d8d29b44b8 | 190 | { |
cittecla | 38:3526c36e4c73 | 191 | double deg = (double)(180.0f/(double)M_PI*imu.readHeading()); |
cittecla | 38:3526c36e4c73 | 192 | return deg; |
cittecla | 34:40d8d29b44b8 | 193 | } |
cittecla | 34:40d8d29b44b8 | 194 | |
cittecla | 34:40d8d29b44b8 | 195 | |
cittecla | 34:40d8d29b44b8 | 196 | // Servo I2C |
aeschsim | 42:08b3aea29254 | 197 | void init_servo(int freq) { |
aeschsim | 42:08b3aea29254 | 198 | uint8_t mode1 = 0x00; |
aeschsim | 42:08b3aea29254 | 199 | uint8_t mode2 = 0x01; |
aeschsim | 42:08b3aea29254 | 200 | |
aeschsim | 42:08b3aea29254 | 201 | i2c.write(0x06); //0000 0110 - PCA reset |
aeschsim | 42:08b3aea29254 | 202 | //freq *= 0.9; |
aeschsim | 42:08b3aea29254 | 203 | float prescaleval = 25000000.0; //25MHz |
aeschsim | 42:08b3aea29254 | 204 | prescaleval /= 4096.0; //12-Bit |
aeschsim | 42:08b3aea29254 | 205 | prescaleval /= (float)freq; |
aeschsim | 42:08b3aea29254 | 206 | prescaleval -= 1.0; |
aeschsim | 42:08b3aea29254 | 207 | uint8_t prescale = floor(prescaleval +0.5); |
aeschsim | 42:08b3aea29254 | 208 | |
aeschsim | 42:08b3aea29254 | 209 | uint8_t oldmode = i2c.read(mode1); |
aeschsim | 42:08b3aea29254 | 210 | uint8_t newmode = (oldmode&0x7f) | 0x10; |
aeschsim | 42:08b3aea29254 | 211 | i2c.write(mode1<<8 + newmode); |
aeschsim | 42:08b3aea29254 | 212 | i2c.write(0xFE<<8 + prescale); //0xFE - Prescale-Register |
aeschsim | 42:08b3aea29254 | 213 | i2c.write(mode1<<8 + oldmode); |
aeschsim | 42:08b3aea29254 | 214 | delay(5); |
aeschsim | 42:08b3aea29254 | 215 | i2c.write((mode1<<8) + (oldmode|0xA1)); |
aeschsim | 42:08b3aea29254 | 216 | } |
aeschsim | 42:08b3aea29254 | 217 | |
aeschsim | 42:08b3aea29254 | 218 | void set_servo_position(int servo, int deg){ |
cittecla | 34:40d8d29b44b8 | 219 | i2c.write((0x40), &data, 2); |
cittecla | 34:40d8d29b44b8 | 220 | } |
cittecla | 34:40d8d29b44b8 | 221 | |
cittecla | 34:40d8d29b44b8 | 222 | void enable_servos(){ |
cittecla | 34:40d8d29b44b8 | 223 | Servo_enable = 1; |
cittecla | 34:40d8d29b44b8 | 224 | } |
cittecla | 34:40d8d29b44b8 | 225 | |
cittecla | 34:40d8d29b44b8 | 226 | void disable_servos(){ |
cittecla | 34:40d8d29b44b8 | 227 | Servo_enable = 0; |
cittecla | 34:40d8d29b44b8 | 228 | } |