Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Robot.cpp@43:9f291a496db8, 2017-04-11 (annotated)
- Committer:
- aeschsim
- Date:
- Tue Apr 11 14:12:25 2017 +0000
- Revision:
- 43:9f291a496db8
- Parent:
- 42:08b3aea29254
- Child:
- 55:a1e6fb87d648
- Child:
- 57:1a395b6928ee
servo implementiert;
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 |
aeschsim | 43:9f291a496db8 | 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 | |
aeschsim | 43:9f291a496db8 | 60 | uint8_t mode1 = 0x00; |
aeschsim | 43:9f291a496db8 | 61 | uint8_t mode2 = 0x01; |
aeschsim | 43:9f291a496db8 | 62 | uint8_t LED0_ON_L = 0x06; |
aeschsim | 43:9f291a496db8 | 63 | uint8_t LED0_ON_H = 0x07; |
aeschsim | 43:9f291a496db8 | 64 | uint8_t LED0_OFF_L = 0x08; |
aeschsim | 43:9f291a496db8 | 65 | uint8_t LED0_OFF_H = 0x09; |
aeschsim | 43:9f291a496db8 | 66 | uint8_t ALL_LED_ON_L = 0xFA; |
aeschsim | 43:9f291a496db8 | 67 | uint8_t ALL_LED_ON_H = 0xFB; |
aeschsim | 43:9f291a496db8 | 68 | uint8_t ALL_LED_OFF_L = 0xFC; |
aeschsim | 43:9f291a496db8 | 69 | uint8_t ALL_LED_OFF_H = 0xFD; |
aeschsim | 43:9f291a496db8 | 70 | |
cittecla | 34:40d8d29b44b8 | 71 | |
cittecla | 34:40d8d29b44b8 | 72 | short previousValueCounterRight = 0; |
cittecla | 34:40d8d29b44b8 | 73 | short previousValueCounterLeft = 0; |
cittecla | 34:40d8d29b44b8 | 74 | |
cittecla | 34:40d8d29b44b8 | 75 | float desiredSpeedLeft = 0; |
cittecla | 34:40d8d29b44b8 | 76 | float desiredSpeedRight = 0; |
cittecla | 34:40d8d29b44b8 | 77 | |
cittecla | 34:40d8d29b44b8 | 78 | float actualSpeedLeft; |
cittecla | 34:40d8d29b44b8 | 79 | float actualSpeedRight; |
cittecla | 34:40d8d29b44b8 | 80 | |
cittecla | 34:40d8d29b44b8 | 81 | |
cittecla | 34:40d8d29b44b8 | 82 | |
cittecla | 34:40d8d29b44b8 | 83 | |
cittecla | 34:40d8d29b44b8 | 84 | void Robot_init_all() |
cittecla | 34:40d8d29b44b8 | 85 | { |
cittecla | 34:40d8d29b44b8 | 86 | Speedcontroller_init(); |
cittecla | 34:40d8d29b44b8 | 87 | Sensor_init(); |
cittecla | 34:40d8d29b44b8 | 88 | } |
cittecla | 34:40d8d29b44b8 | 89 | |
cittecla | 34:40d8d29b44b8 | 90 | //speed controll |
cittecla | 34:40d8d29b44b8 | 91 | void Speedcontroller_init() |
cittecla | 34:40d8d29b44b8 | 92 | { |
cittecla | 34:40d8d29b44b8 | 93 | // Initialisieren der PWM Ausgaenge pwmLeft.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 94 | pwmLeft.period(0.00005f); // Setzt die Periode auf 50 μs |
cittecla | 34:40d8d29b44b8 | 95 | pwmRight.period(0.00005f); |
cittecla | 34:40d8d29b44b8 | 96 | pwmLeft = 0.5f; // Duty-Cycle von 50% pwmRight.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 97 | pwmRight = 0.5f; // Duty-Cycle von 50% |
cittecla | 34:40d8d29b44b8 | 98 | |
cittecla | 34:40d8d29b44b8 | 99 | // Initialisieren von lokalen Variabeln |
cittecla | 34:40d8d29b44b8 | 100 | previousValueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 101 | previousValueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 102 | speedLeftFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 103 | speedLeftFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 104 | speedRightFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 105 | speedRightFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 106 | |
cittecla | 34:40d8d29b44b8 | 107 | desiredSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 108 | desiredSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 109 | actualSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 110 | actualSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 111 | |
cittecla | 34:40d8d29b44b8 | 112 | Ticker t1; |
cittecla | 34:40d8d29b44b8 | 113 | t1.attach( &speedCtrl, PERIOD); |
cittecla | 34:40d8d29b44b8 | 114 | |
cittecla | 34:40d8d29b44b8 | 115 | //desiredSpeedLeft = 50.0f+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 116 | //desiredSpeedRight = -50.0f; //50 RPM |
cittecla | 34:40d8d29b44b8 | 117 | enableMotorDriver = 1; |
cittecla | 34:40d8d29b44b8 | 118 | } |
cittecla | 34:40d8d29b44b8 | 119 | |
cittecla | 34:40d8d29b44b8 | 120 | void set_speed(float left, float right) |
cittecla | 34:40d8d29b44b8 | 121 | { |
cittecla | 34:40d8d29b44b8 | 122 | desiredSpeedLeft = left+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 123 | desiredSpeedRight = -right; //50 RPM |
cittecla | 34:40d8d29b44b8 | 124 | } |
cittecla | 34:40d8d29b44b8 | 125 | |
cittecla | 34:40d8d29b44b8 | 126 | |
cittecla | 34:40d8d29b44b8 | 127 | void speedCtrl() |
cittecla | 34:40d8d29b44b8 | 128 | { |
cittecla | 34:40d8d29b44b8 | 129 | // Berechnen die effektiven Drehzahlen der Motoren in [rpm] |
cittecla | 34:40d8d29b44b8 | 130 | short valueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 131 | short valueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 132 | short countsInPastPeriodLeft = valueCounterLeft-previousValueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 133 | short countsInPastPeriodRight = valueCounterRight-previousValueCounterRight; |
cittecla | 34:40d8d29b44b8 | 134 | |
cittecla | 34:40d8d29b44b8 | 135 | previousValueCounterLeft = valueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 136 | previousValueCounterRight = valueCounterRight; |
cittecla | 34:40d8d29b44b8 | 137 | actualSpeedLeft = speedLeftFilter.filter((float)countsInPastPeriodLeft /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 138 | actualSpeedRight = speedRightFilter.filter((float)countsInPastPeriodRight /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 139 | |
cittecla | 34:40d8d29b44b8 | 140 | // Berechnen der Motorspannungen Uout |
cittecla | 34:40d8d29b44b8 | 141 | float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN; |
cittecla | 34:40d8d29b44b8 | 142 | float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN; |
cittecla | 34:40d8d29b44b8 | 143 | |
cittecla | 34:40d8d29b44b8 | 144 | // Berechnen, Limitieren und Setzen der Duty-Cycle |
cittecla | 34:40d8d29b44b8 | 145 | float dutyCycleLeft = 0.5f+0.5f*voltageLeft/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 146 | if (dutyCycleLeft < MIN_DUTY_CYCLE) dutyCycleLeft = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 147 | else if (dutyCycleLeft > MAX_DUTY_CYCLE) dutyCycleLeft = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 148 | |
cittecla | 34:40d8d29b44b8 | 149 | pwmLeft = dutyCycleLeft; |
cittecla | 34:40d8d29b44b8 | 150 | float dutyCycleRight = 0.5f+0.5f*voltageRight/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 151 | if (dutyCycleRight < MIN_DUTY_CYCLE) dutyCycleRight = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 152 | else if (dutyCycleRight > MAX_DUTY_CYCLE) dutyCycleRight = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 153 | |
cittecla | 34:40d8d29b44b8 | 154 | pwmRight = dutyCycleRight; |
cittecla | 34:40d8d29b44b8 | 155 | } |
cittecla | 34:40d8d29b44b8 | 156 | |
cittecla | 34:40d8d29b44b8 | 157 | //Sensors |
cittecla | 34:40d8d29b44b8 | 158 | void Sensor_init() |
cittecla | 34:40d8d29b44b8 | 159 | { |
cittecla | 34:40d8d29b44b8 | 160 | for( int ii = 0; ii<6; ++ii) |
cittecla | 34:40d8d29b44b8 | 161 | sensors[ii].init(&distance, &bit0, &bit1, &bit2, ii); |
cittecla | 34:40d8d29b44b8 | 162 | } |
cittecla | 34:40d8d29b44b8 | 163 | |
cittecla | 34:40d8d29b44b8 | 164 | float getDistanceIR(int number) |
cittecla | 34:40d8d29b44b8 | 165 | { |
cittecla | 34:40d8d29b44b8 | 166 | return sensors[number]; |
cittecla | 34:40d8d29b44b8 | 167 | } |
cittecla | 34:40d8d29b44b8 | 168 | |
cittecla | 34:40d8d29b44b8 | 169 | //IMU |
cittecla | 34:40d8d29b44b8 | 170 | float read_acc_x() |
cittecla | 34:40d8d29b44b8 | 171 | { |
cittecla | 34:40d8d29b44b8 | 172 | return imu.readAccelerationX(); |
cittecla | 34:40d8d29b44b8 | 173 | } |
cittecla | 34:40d8d29b44b8 | 174 | |
cittecla | 34:40d8d29b44b8 | 175 | float read_acc_y() |
cittecla | 34:40d8d29b44b8 | 176 | { |
cittecla | 34:40d8d29b44b8 | 177 | return imu.readAccelerationY(); |
cittecla | 34:40d8d29b44b8 | 178 | } |
cittecla | 34:40d8d29b44b8 | 179 | |
cittecla | 34:40d8d29b44b8 | 180 | float read_acc_z() |
cittecla | 34:40d8d29b44b8 | 181 | { |
cittecla | 34:40d8d29b44b8 | 182 | return imu.readAccelerationZ(); |
cittecla | 34:40d8d29b44b8 | 183 | } |
cittecla | 34:40d8d29b44b8 | 184 | |
cittecla | 34:40d8d29b44b8 | 185 | float read_gyr_x() |
cittecla | 34:40d8d29b44b8 | 186 | { |
cittecla | 34:40d8d29b44b8 | 187 | return imu.readGyroX(); |
cittecla | 34:40d8d29b44b8 | 188 | } |
cittecla | 34:40d8d29b44b8 | 189 | |
cittecla | 34:40d8d29b44b8 | 190 | float read_gyr_y() |
cittecla | 34:40d8d29b44b8 | 191 | { |
cittecla | 34:40d8d29b44b8 | 192 | return imu.readGyroY(); |
cittecla | 34:40d8d29b44b8 | 193 | } |
cittecla | 34:40d8d29b44b8 | 194 | |
cittecla | 34:40d8d29b44b8 | 195 | float read_gyr_z() |
cittecla | 34:40d8d29b44b8 | 196 | { |
cittecla | 34:40d8d29b44b8 | 197 | return imu.readGyroZ(); |
cittecla | 34:40d8d29b44b8 | 198 | } |
cittecla | 34:40d8d29b44b8 | 199 | |
cittecla | 34:40d8d29b44b8 | 200 | float read_heading() |
cittecla | 34:40d8d29b44b8 | 201 | { |
cittecla | 38:3526c36e4c73 | 202 | double deg = (double)(180.0f/(double)M_PI*imu.readHeading()); |
cittecla | 38:3526c36e4c73 | 203 | return deg; |
cittecla | 34:40d8d29b44b8 | 204 | } |
cittecla | 34:40d8d29b44b8 | 205 | |
cittecla | 34:40d8d29b44b8 | 206 | |
cittecla | 34:40d8d29b44b8 | 207 | // Servo I2C |
aeschsim | 43:9f291a496db8 | 208 | void init_servo(int freq) { |
aeschsim | 42:08b3aea29254 | 209 | i2c.write(0x06); //0000 0110 - PCA reset |
aeschsim | 42:08b3aea29254 | 210 | //freq *= 0.9; |
aeschsim | 42:08b3aea29254 | 211 | float prescaleval = 25000000.0; //25MHz |
aeschsim | 43:9f291a496db8 | 212 | prescaleval /= 4096.0f; //12-Bit |
aeschsim | 42:08b3aea29254 | 213 | prescaleval /= (float)freq; |
aeschsim | 43:9f291a496db8 | 214 | prescaleval -= 1.0f; |
aeschsim | 43:9f291a496db8 | 215 | uint8_t prescale = floor(prescaleval +0.5f); |
aeschsim | 42:08b3aea29254 | 216 | |
aeschsim | 42:08b3aea29254 | 217 | uint8_t oldmode = i2c.read(mode1); |
aeschsim | 42:08b3aea29254 | 218 | uint8_t newmode = (oldmode&0x7f) | 0x10; |
aeschsim | 42:08b3aea29254 | 219 | i2c.write(mode1<<8 + newmode); |
aeschsim | 42:08b3aea29254 | 220 | i2c.write(0xFE<<8 + prescale); //0xFE - Prescale-Register |
aeschsim | 42:08b3aea29254 | 221 | i2c.write(mode1<<8 + oldmode); |
aeschsim | 43:9f291a496db8 | 222 | wait_ms(50); |
aeschsim | 42:08b3aea29254 | 223 | i2c.write((mode1<<8) + (oldmode|0xA1)); |
aeschsim | 42:08b3aea29254 | 224 | } |
aeschsim | 42:08b3aea29254 | 225 | |
aeschsim | 42:08b3aea29254 | 226 | void set_servo_position(int servo, int deg){ |
aeschsim | 43:9f291a496db8 | 227 | uint16_t on = 0x00; |
aeschsim | 43:9f291a496db8 | 228 | uint16_t off; |
aeschsim | 43:9f291a496db8 | 229 | off = 150 + (3 * deg); //deg von 0...150° abgebildet in min150...max600 |
aeschsim | 43:9f291a496db8 | 230 | i2c.write(LED0_ON_L+4*servo); |
aeschsim | 43:9f291a496db8 | 231 | i2c.write(on); |
aeschsim | 43:9f291a496db8 | 232 | i2c.write(on>>8); |
aeschsim | 43:9f291a496db8 | 233 | i2c.write(off); |
aeschsim | 43:9f291a496db8 | 234 | i2c.write(off>>8); |
cittecla | 34:40d8d29b44b8 | 235 | } |
cittecla | 34:40d8d29b44b8 | 236 | |
cittecla | 34:40d8d29b44b8 | 237 | void enable_servos(){ |
cittecla | 34:40d8d29b44b8 | 238 | Servo_enable = 1; |
cittecla | 34:40d8d29b44b8 | 239 | } |
cittecla | 34:40d8d29b44b8 | 240 | |
cittecla | 34:40d8d29b44b8 | 241 | void disable_servos(){ |
cittecla | 34:40d8d29b44b8 | 242 | Servo_enable = 0; |
cittecla | 34:40d8d29b44b8 | 243 | } |