Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Robot.cpp@136:906ac19fb850, 2017-05-22 (annotated)
- Committer:
- PESGruppe1
- Date:
- Mon May 22 13:18:13 2017 +0000
- Revision:
- 136:906ac19fb850
- Parent:
- 132:8ae08f41bb43
New finepostitioning mit vor und zur?ckdrehen 1 grad;
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 | |
aeschsim | 55:a1e6fb87d648 | 12 | //Define I2C-Servoboard Consts |
aeschsim | 55:a1e6fb87d648 | 13 | #define ADRESS 0x40 |
aeschsim | 55:a1e6fb87d648 | 14 | #define MODE1 0x00 |
aeschsim | 55:a1e6fb87d648 | 15 | #define MODE2 0x01 |
aeschsim | 55:a1e6fb87d648 | 16 | #define PRESCALE 0xFE |
aeschsim | 55:a1e6fb87d648 | 17 | #define LED0_ON_L 0x06 |
aeschsim | 55:a1e6fb87d648 | 18 | #define LED0_ON_H 0x07 |
aeschsim | 55:a1e6fb87d648 | 19 | #define LED0_OFF_L 0x08 |
cittecla | 60:b57577b0072f | 20 | #define LED0_OFF_H 0x09 |
aeschsim | 55:a1e6fb87d648 | 21 | |
aeschsim | 55:a1e6fb87d648 | 22 | #define RESTART 0x80 |
aeschsim | 55:a1e6fb87d648 | 23 | #define SLEEP 0x10 |
aeschsim | 55:a1e6fb87d648 | 24 | |
cittecla | 120:cdf7a6751f9e | 25 | #define servo0center 350 |
cittecla | 120:cdf7a6751f9e | 26 | #define servo2center 480 |
aeschsim | 73:f7657ddb7827 | 27 | |
cittecla | 89:7f9d6e641a01 | 28 | DigitalIn user(USER_BUTTON); |
cittecla | 34:40d8d29b44b8 | 29 | |
cittecla | 34:40d8d29b44b8 | 30 | const float PERIOD = 0.001f; // period of control task, given in [s] |
cittecla | 34:40d8d29b44b8 | 31 | const float COUNTS_PER_TURN = 1200.0f; // resolution of encoder counter |
cittecla | 34:40d8d29b44b8 | 32 | const float LOWPASS_FILTER_FREQUENCY = 300.0f; // frequency of lowpass filter for actual speed values, given in [rad/s] |
cittecla | 34:40d8d29b44b8 | 33 | const float KN = 40.0f; // speed constant of motor, given in [rpm/V] |
cittecla | 34:40d8d29b44b8 | 34 | const float KP = 0.2f; // speed controller gain, given in [V/rpm] |
cittecla | 34:40d8d29b44b8 | 35 | const float MAX_VOLTAGE = 12.0f; // supply voltage for power stage in [V] |
cittecla | 34:40d8d29b44b8 | 36 | const float MIN_DUTY_CYCLE = 0.02f; // minimum allowed value for duty cycle (2%) |
cittecla | 34:40d8d29b44b8 | 37 | const float MAX_DUTY_CYCLE = 0.98f; // maximum allowed value for duty cycle (98%) |
cittecla | 34:40d8d29b44b8 | 38 | const float correction_value = 2.45f; // correction value for desired speed |
cittecla | 61:628f8a4e857c | 39 | |
cittecla | 34:40d8d29b44b8 | 40 | LowpassFilter speedLeftFilter; |
cittecla | 34:40d8d29b44b8 | 41 | LowpassFilter speedRightFilter; |
cittecla | 34:40d8d29b44b8 | 42 | |
cittecla | 34:40d8d29b44b8 | 43 | //Motor stuff |
aeschsim | 99:78d87027c85b | 44 | Ticker t4; |
cittecla | 34:40d8d29b44b8 | 45 | EncoderCounter counterLeft(PB_6, PB_7); |
cittecla | 34:40d8d29b44b8 | 46 | EncoderCounter counterRight(PA_6, PC_7); |
cittecla | 34:40d8d29b44b8 | 47 | |
cittecla | 34:40d8d29b44b8 | 48 | DigitalOut enableMotorDriver(PB_2); |
cittecla | 34:40d8d29b44b8 | 49 | PwmOut pwmLeft(PA_8); |
cittecla | 34:40d8d29b44b8 | 50 | PwmOut pwmRight(PA_9); |
cittecla | 34:40d8d29b44b8 | 51 | |
cittecla | 34:40d8d29b44b8 | 52 | DigitalOut my_led(LED1); |
cittecla | 34:40d8d29b44b8 | 53 | |
cittecla | 89:7f9d6e641a01 | 54 | short previousValueCounterRight = 0; |
cittecla | 89:7f9d6e641a01 | 55 | short previousValueCounterLeft = 0; |
cittecla | 89:7f9d6e641a01 | 56 | |
cittecla | 89:7f9d6e641a01 | 57 | float desiredSpeedLeft = 0; |
cittecla | 89:7f9d6e641a01 | 58 | float desiredSpeedRight = 0; |
cittecla | 89:7f9d6e641a01 | 59 | |
cittecla | 89:7f9d6e641a01 | 60 | float actualSpeedLeft; |
cittecla | 89:7f9d6e641a01 | 61 | float actualSpeedRight; |
cittecla | 89:7f9d6e641a01 | 62 | |
cittecla | 89:7f9d6e641a01 | 63 | |
cittecla | 34:40d8d29b44b8 | 64 | //Periphery for distance sensors |
cittecla | 34:40d8d29b44b8 | 65 | AnalogIn distance(PB_1); |
aeschsim | 55:a1e6fb87d648 | 66 | AnalogIn distance2(PA_1); |
cittecla | 80:92b9d083322d | 67 | DigitalOut IRenable(PC_1); |
cittecla | 34:40d8d29b44b8 | 68 | DigitalOut bit0(PH_1); |
cittecla | 34:40d8d29b44b8 | 69 | DigitalOut bit1(PC_2); |
cittecla | 34:40d8d29b44b8 | 70 | DigitalOut bit2(PC_3); |
cittecla | 34:40d8d29b44b8 | 71 | |
cittecla | 34:40d8d29b44b8 | 72 | IRSensor sensors[6]; |
cittecla | 34:40d8d29b44b8 | 73 | |
cittecla | 34:40d8d29b44b8 | 74 | //indicator leds arround robot |
cittecla | 34:40d8d29b44b8 | 75 | DigitalOut leds[] = { PC_8, PC_6, PB_12, PA_7, PC_0, PC_9 }; |
cittecla | 34:40d8d29b44b8 | 76 | |
cittecla | 34:40d8d29b44b8 | 77 | |
cittecla | 34:40d8d29b44b8 | 78 | //Periphery for the IMU |
cittecla | 34:40d8d29b44b8 | 79 | SPI spi(PC_12, PC_11, PC_10); |
cittecla | 34:40d8d29b44b8 | 80 | DigitalOut csG(PA_15); |
cittecla | 34:40d8d29b44b8 | 81 | DigitalOut csXM(PD_2); |
cittecla | 34:40d8d29b44b8 | 82 | |
cittecla | 34:40d8d29b44b8 | 83 | IMU imu(spi, csG, csXM); |
cittecla | 34:40d8d29b44b8 | 84 | |
cittecla | 34:40d8d29b44b8 | 85 | //Periphery for I2C |
cittecla | 34:40d8d29b44b8 | 86 | I2C i2c(PB_9, PB_8); |
cittecla | 34:40d8d29b44b8 | 87 | DigitalOut Servo_enable(PA_10); |
cittecla | 34:40d8d29b44b8 | 88 | |
aeschsim | 55:a1e6fb87d648 | 89 | //Color sensor |
cittecla | 60:b57577b0072f | 90 | DigitalIn red(PB_13); |
cittecla | 60:b57577b0072f | 91 | DigitalIn green(PC_4); |
cittecla | 34:40d8d29b44b8 | 92 | |
cittecla | 34:40d8d29b44b8 | 93 | |
cittecla | 34:40d8d29b44b8 | 94 | |
cittecla | 61:628f8a4e857c | 95 | bool get_user() |
aeschsim | 55:a1e6fb87d648 | 96 | { |
aeschsim | 55:a1e6fb87d648 | 97 | return user; |
cittecla | 61:628f8a4e857c | 98 | } |
cittecla | 34:40d8d29b44b8 | 99 | |
cittecla | 34:40d8d29b44b8 | 100 | |
cittecla | 34:40d8d29b44b8 | 101 | void Robot_init_all() |
cittecla | 34:40d8d29b44b8 | 102 | { |
cittecla | 34:40d8d29b44b8 | 103 | Speedcontroller_init(); |
cittecla | 93:837a13760026 | 104 | Sensor_init(); |
cittecla | 93:837a13760026 | 105 | init_servo(60); |
cittecla | 34:40d8d29b44b8 | 106 | } |
cittecla | 34:40d8d29b44b8 | 107 | |
cittecla | 34:40d8d29b44b8 | 108 | //speed controll |
cittecla | 34:40d8d29b44b8 | 109 | void Speedcontroller_init() |
cittecla | 34:40d8d29b44b8 | 110 | { |
cittecla | 34:40d8d29b44b8 | 111 | // Initialisieren der PWM Ausgaenge pwmLeft.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 112 | pwmLeft.period(0.00005f); // Setzt die Periode auf 50 μs |
cittecla | 34:40d8d29b44b8 | 113 | pwmRight.period(0.00005f); |
cittecla | 34:40d8d29b44b8 | 114 | pwmLeft = 0.5f; // Duty-Cycle von 50% pwmRight.period(0.00005f); // PWM Periode von 50 us |
cittecla | 34:40d8d29b44b8 | 115 | pwmRight = 0.5f; // Duty-Cycle von 50% |
cittecla | 34:40d8d29b44b8 | 116 | |
cittecla | 34:40d8d29b44b8 | 117 | // Initialisieren von lokalen Variabeln |
cittecla | 34:40d8d29b44b8 | 118 | previousValueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 119 | previousValueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 120 | speedLeftFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 121 | speedLeftFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 122 | speedRightFilter.setPeriod(PERIOD); |
cittecla | 34:40d8d29b44b8 | 123 | speedRightFilter.setFrequency(LOWPASS_FILTER_FREQUENCY); |
cittecla | 34:40d8d29b44b8 | 124 | |
cittecla | 34:40d8d29b44b8 | 125 | desiredSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 126 | desiredSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 127 | actualSpeedLeft = 0.0f; |
cittecla | 34:40d8d29b44b8 | 128 | actualSpeedRight = 0.0f; |
cittecla | 34:40d8d29b44b8 | 129 | |
cittecla | 88:b89cace9329b | 130 | |
aeschsim | 99:78d87027c85b | 131 | t4.attach( &speedCtrl, PERIOD); |
cittecla | 34:40d8d29b44b8 | 132 | |
cittecla | 89:7f9d6e641a01 | 133 | //desiredSpeedLeft = 50.0f+correction_value; //50 RPM |
cittecla | 89:7f9d6e641a01 | 134 | //desiredSpeedRight = -50.0f; //50 RPM |
cittecla | 34:40d8d29b44b8 | 135 | enableMotorDriver = 1; |
cittecla | 34:40d8d29b44b8 | 136 | } |
cittecla | 34:40d8d29b44b8 | 137 | |
cittecla | 126:d0b2057272d0 | 138 | void disable_motors() |
cittecla | 126:d0b2057272d0 | 139 | { |
cittecla | 126:d0b2057272d0 | 140 | enableMotorDriver = 0; |
cittecla | 126:d0b2057272d0 | 141 | } |
cittecla | 126:d0b2057272d0 | 142 | |
cittecla | 34:40d8d29b44b8 | 143 | void set_speed(float left, float right) |
cittecla | 34:40d8d29b44b8 | 144 | { |
cittecla | 94:0381e8b1beda | 145 | enableMotorDriver = 0; |
cittecla | 34:40d8d29b44b8 | 146 | desiredSpeedLeft = left+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 147 | desiredSpeedRight = -right; //50 RPM |
cittecla | 89:7f9d6e641a01 | 148 | enableMotorDriver = 1; |
cittecla | 132:8ae08f41bb43 | 149 | //printf("real speed set as: %f\r\n",desiredSpeedLeft); |
cittecla | 34:40d8d29b44b8 | 150 | } |
cittecla | 34:40d8d29b44b8 | 151 | |
cittecla | 61:628f8a4e857c | 152 | float get_speed_left() |
cittecla | 61:628f8a4e857c | 153 | { |
cittecla | 61:628f8a4e857c | 154 | return actualSpeedLeft; |
cittecla | 61:628f8a4e857c | 155 | } |
cittecla | 61:628f8a4e857c | 156 | |
cittecla | 61:628f8a4e857c | 157 | float get_speed_right() |
cittecla | 61:628f8a4e857c | 158 | { |
cittecla | 61:628f8a4e857c | 159 | return actualSpeedRight; |
cittecla | 61:628f8a4e857c | 160 | } |
cittecla | 61:628f8a4e857c | 161 | |
cittecla | 34:40d8d29b44b8 | 162 | |
cittecla | 34:40d8d29b44b8 | 163 | void speedCtrl() |
cittecla | 34:40d8d29b44b8 | 164 | { |
cittecla | 34:40d8d29b44b8 | 165 | // Berechnen die effektiven Drehzahlen der Motoren in [rpm] |
cittecla | 34:40d8d29b44b8 | 166 | short valueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 167 | short valueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 168 | short countsInPastPeriodLeft = valueCounterLeft-previousValueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 169 | short countsInPastPeriodRight = valueCounterRight-previousValueCounterRight; |
cittecla | 34:40d8d29b44b8 | 170 | |
cittecla | 34:40d8d29b44b8 | 171 | previousValueCounterLeft = valueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 172 | previousValueCounterRight = valueCounterRight; |
cittecla | 34:40d8d29b44b8 | 173 | actualSpeedLeft = speedLeftFilter.filter((float)countsInPastPeriodLeft /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 174 | actualSpeedRight = speedRightFilter.filter((float)countsInPastPeriodRight /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 175 | |
cittecla | 34:40d8d29b44b8 | 176 | // Berechnen der Motorspannungen Uout |
cittecla | 34:40d8d29b44b8 | 177 | float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN; |
cittecla | 34:40d8d29b44b8 | 178 | float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN; |
cittecla | 34:40d8d29b44b8 | 179 | |
cittecla | 34:40d8d29b44b8 | 180 | // Berechnen, Limitieren und Setzen der Duty-Cycle |
cittecla | 34:40d8d29b44b8 | 181 | float dutyCycleLeft = 0.5f+0.5f*voltageLeft/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 182 | if (dutyCycleLeft < MIN_DUTY_CYCLE) dutyCycleLeft = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 183 | else if (dutyCycleLeft > MAX_DUTY_CYCLE) dutyCycleLeft = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 184 | pwmLeft = dutyCycleLeft; |
cittecla | 94:0381e8b1beda | 185 | |
cittecla | 34:40d8d29b44b8 | 186 | float dutyCycleRight = 0.5f+0.5f*voltageRight/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 187 | if (dutyCycleRight < MIN_DUTY_CYCLE) dutyCycleRight = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 188 | else if (dutyCycleRight > MAX_DUTY_CYCLE) dutyCycleRight = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 189 | pwmRight = dutyCycleRight; |
cittecla | 34:40d8d29b44b8 | 190 | } |
cittecla | 34:40d8d29b44b8 | 191 | |
cittecla | 34:40d8d29b44b8 | 192 | //Sensors |
cittecla | 34:40d8d29b44b8 | 193 | void Sensor_init() |
cittecla | 34:40d8d29b44b8 | 194 | { |
cittecla | 34:40d8d29b44b8 | 195 | for( int ii = 0; ii<6; ++ii) |
cittecla | 72:4e8a151d804e | 196 | sensors[ii].init(&distance,&distance2, &bit0, &bit1, &bit2, ii); |
cittecla | 80:92b9d083322d | 197 | IRenable = 1; |
cittecla | 94:0381e8b1beda | 198 | } |
cittecla | 94:0381e8b1beda | 199 | |
cittecla | 117:66d64dbd1b36 | 200 | |
cittecla | 34:40d8d29b44b8 | 201 | |
cittecla | 34:40d8d29b44b8 | 202 | float getDistanceIR(int number) |
cittecla | 34:40d8d29b44b8 | 203 | { |
cittecla | 34:40d8d29b44b8 | 204 | return sensors[number]; |
cittecla | 34:40d8d29b44b8 | 205 | } |
cittecla | 34:40d8d29b44b8 | 206 | |
aeschsim | 55:a1e6fb87d648 | 207 | //Color |
cittecla | 61:628f8a4e857c | 208 | bool get_color() |
cittecla | 61:628f8a4e857c | 209 | { |
aeschsim | 81:956f65714207 | 210 | if (red == 1 && green == 0) { //Active - LOW |
aeschsim | 55:a1e6fb87d648 | 211 | return 1; |
aeschsim | 55:a1e6fb87d648 | 212 | } else { |
aeschsim | 55:a1e6fb87d648 | 213 | return 0; |
aeschsim | 55:a1e6fb87d648 | 214 | } |
aeschsim | 55:a1e6fb87d648 | 215 | } |
aeschsim | 55:a1e6fb87d648 | 216 | |
cittecla | 34:40d8d29b44b8 | 217 | //IMU |
cittecla | 34:40d8d29b44b8 | 218 | float read_acc_x() |
cittecla | 34:40d8d29b44b8 | 219 | { |
cittecla | 34:40d8d29b44b8 | 220 | return imu.readAccelerationX(); |
cittecla | 34:40d8d29b44b8 | 221 | } |
cittecla | 34:40d8d29b44b8 | 222 | |
cittecla | 34:40d8d29b44b8 | 223 | float read_acc_y() |
cittecla | 34:40d8d29b44b8 | 224 | { |
cittecla | 34:40d8d29b44b8 | 225 | return imu.readAccelerationY(); |
cittecla | 34:40d8d29b44b8 | 226 | } |
cittecla | 34:40d8d29b44b8 | 227 | |
cittecla | 34:40d8d29b44b8 | 228 | float read_acc_z() |
cittecla | 34:40d8d29b44b8 | 229 | { |
cittecla | 34:40d8d29b44b8 | 230 | return imu.readAccelerationZ(); |
cittecla | 34:40d8d29b44b8 | 231 | } |
cittecla | 34:40d8d29b44b8 | 232 | |
cittecla | 34:40d8d29b44b8 | 233 | float read_gyr_x() |
cittecla | 34:40d8d29b44b8 | 234 | { |
cittecla | 34:40d8d29b44b8 | 235 | return imu.readGyroX(); |
cittecla | 34:40d8d29b44b8 | 236 | } |
cittecla | 34:40d8d29b44b8 | 237 | |
cittecla | 34:40d8d29b44b8 | 238 | float read_gyr_y() |
cittecla | 34:40d8d29b44b8 | 239 | { |
cittecla | 34:40d8d29b44b8 | 240 | return imu.readGyroY(); |
cittecla | 34:40d8d29b44b8 | 241 | } |
cittecla | 34:40d8d29b44b8 | 242 | |
cittecla | 34:40d8d29b44b8 | 243 | float read_gyr_z() |
cittecla | 34:40d8d29b44b8 | 244 | { |
cittecla | 34:40d8d29b44b8 | 245 | return imu.readGyroZ(); |
cittecla | 34:40d8d29b44b8 | 246 | } |
cittecla | 34:40d8d29b44b8 | 247 | |
cittecla | 34:40d8d29b44b8 | 248 | float read_heading() |
cittecla | 34:40d8d29b44b8 | 249 | { |
cittecla | 38:3526c36e4c73 | 250 | double deg = (double)(180.0f/(double)M_PI*imu.readHeading()); |
cittecla | 38:3526c36e4c73 | 251 | return deg; |
cittecla | 34:40d8d29b44b8 | 252 | } |
cittecla | 34:40d8d29b44b8 | 253 | |
cittecla | 34:40d8d29b44b8 | 254 | |
cittecla | 34:40d8d29b44b8 | 255 | // Servo I2C |
aeschsim | 55:a1e6fb87d648 | 256 | |
aeschsim | 55:a1e6fb87d648 | 257 | |
aeschsim | 55:a1e6fb87d648 | 258 | void init_servo(int freq) |
aeschsim | 55:a1e6fb87d648 | 259 | { |
aeschsim | 55:a1e6fb87d648 | 260 | char data[2]; |
aeschsim | 55:a1e6fb87d648 | 261 | //Reset |
aeschsim | 55:a1e6fb87d648 | 262 | data[0] = (char) MODE1; |
cittecla | 61:628f8a4e857c | 263 | data[1] = (char) SLEEP; |
aeschsim | 55:a1e6fb87d648 | 264 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 265 | |
aeschsim | 55:a1e6fb87d648 | 266 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 267 | |
aeschsim | 55:a1e6fb87d648 | 268 | float prescaleval = 25000000.0; //25MHz |
aeschsim | 55:a1e6fb87d648 | 269 | prescaleval /= 4096.0f; //12-Bit |
aeschsim | 42:08b3aea29254 | 270 | prescaleval /= (float)freq; |
aeschsim | 43:9f291a496db8 | 271 | prescaleval -= 1.0f; |
aeschsim | 55:a1e6fb87d648 | 272 | char prescale = (char)(prescaleval); //0x64 bei 50Hz |
cittecla | 61:628f8a4e857c | 273 | |
aeschsim | 55:a1e6fb87d648 | 274 | data[0] = (char) PRESCALE; |
aeschsim | 55:a1e6fb87d648 | 275 | data[1] = prescale; |
aeschsim | 55:a1e6fb87d648 | 276 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 277 | |
aeschsim | 55:a1e6fb87d648 | 278 | data[0] = (char) MODE1; |
aeschsim | 55:a1e6fb87d648 | 279 | data[1] = 0x00; //wake up |
aeschsim | 55:a1e6fb87d648 | 280 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 281 | |
aeschsim | 55:a1e6fb87d648 | 282 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 283 | |
aeschsim | 55:a1e6fb87d648 | 284 | data[0] = (char) MODE1; |
cittecla | 61:628f8a4e857c | 285 | data[1] = 0x80; //do restart |
aeschsim | 55:a1e6fb87d648 | 286 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 287 | |
aeschsim | 55:a1e6fb87d648 | 288 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 289 | |
aeschsim | 55:a1e6fb87d648 | 290 | data[0] = (char) MODE2; |
aeschsim | 55:a1e6fb87d648 | 291 | data[1] = (char) 0x04; |
aeschsim | 55:a1e6fb87d648 | 292 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 293 | |
aeschsim | 55:a1e6fb87d648 | 294 | wait_ms(1); |
aeschsim | 55:a1e6fb87d648 | 295 | |
aeschsim | 55:a1e6fb87d648 | 296 | printf("init done...\r\n"); |
aeschsim | 55:a1e6fb87d648 | 297 | } |
aeschsim | 42:08b3aea29254 | 298 | |
aeschsim | 55:a1e6fb87d648 | 299 | void set_servo_position(int servo, int deg) |
aeschsim | 55:a1e6fb87d648 | 300 | { |
cittecla | 62:c2fcf3b349e9 | 301 | // Servo 0 = IR_left |
cittecla | 62:c2fcf3b349e9 | 302 | // Servo 2 = IR_right |
cittecla | 62:c2fcf3b349e9 | 303 | // Servo 4 = Arm_1 |
cittecla | 62:c2fcf3b349e9 | 304 | // Servo 6 = Arm_2 |
cittecla | 62:c2fcf3b349e9 | 305 | // Servo 8 = Grabber |
aeschsim | 73:f7657ddb7827 | 306 | if (servo == 0) { |
cittecla | 120:cdf7a6751f9e | 307 | deg = servo0center - 3.25*deg; |
aeschsim | 73:f7657ddb7827 | 308 | } |
aeschsim | 73:f7657ddb7827 | 309 | if (servo == 2) { |
cittecla | 120:cdf7a6751f9e | 310 | deg = (int)((float)servo2center - 3.75f*(float)deg); |
aeschsim | 73:f7657ddb7827 | 311 | } |
cittecla | 94:0381e8b1beda | 312 | |
aeschsim | 73:f7657ddb7827 | 313 | //only for tests with putty |
aeschsim | 55:a1e6fb87d648 | 314 | if (deg < 0 || deg > 4095) { |
aeschsim | 55:a1e6fb87d648 | 315 | deg = 300; |
cittecla | 94:0381e8b1beda | 316 | } |
aeschsim | 55:a1e6fb87d648 | 317 | char data[2]; |
aeschsim | 55:a1e6fb87d648 | 318 | int16_t wert1 = (deg>>8); |
aeschsim | 55:a1e6fb87d648 | 319 | int16_t wert2 = (deg & 0xFF); |
cittecla | 120:cdf7a6751f9e | 320 | //printf("%x %x servo deg\r\n",wert1,wert2); |
cittecla | 61:628f8a4e857c | 321 | |
aeschsim | 55:a1e6fb87d648 | 322 | data[0] = (char)LED0_ON_L+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 323 | data[1] = 0x00; |
aeschsim | 55:a1e6fb87d648 | 324 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 325 | |
aeschsim | 55:a1e6fb87d648 | 326 | data[0] = (char)LED0_ON_H+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 327 | data[1] = 0x00; |
aeschsim | 55:a1e6fb87d648 | 328 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 329 | |
aeschsim | 55:a1e6fb87d648 | 330 | data[0] = (char)LED0_OFF_L+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 331 | data[1] = (char)wert2; |
aeschsim | 55:a1e6fb87d648 | 332 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 333 | |
aeschsim | 55:a1e6fb87d648 | 334 | data[0] = (char)LED0_OFF_H+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 335 | data[1] = (char)wert1; |
cittecla | 61:628f8a4e857c | 336 | i2c.write((ADRESS<<1), data,2); |
aeschsim | 55:a1e6fb87d648 | 337 | } |
aeschsim | 55:a1e6fb87d648 | 338 | |
cittecla | 61:628f8a4e857c | 339 | int getPWM(uint8_t servo) |
aeschsim | 55:a1e6fb87d648 | 340 | { |
aeschsim | 55:a1e6fb87d648 | 341 | char read = 255; |
aeschsim | 55:a1e6fb87d648 | 342 | char secondread = 255; |
aeschsim | 55:a1e6fb87d648 | 343 | char data; |
aeschsim | 55:a1e6fb87d648 | 344 | data = (char)(LED0_ON_L+(4*servo)); |
aeschsim | 55:a1e6fb87d648 | 345 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 346 | i2c.read((ADRESS<<1), &read, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 347 | data = (char)(LED0_ON_H+(4*servo)); |
aeschsim | 55:a1e6fb87d648 | 348 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 349 | i2c.read((ADRESS<<1), &secondread, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 350 | printf("LED %d ON_L: %x ON_H: %x\r\n",servo, read, secondread); |
aeschsim | 55:a1e6fb87d648 | 351 | return (int)read; |
aeschsim | 55:a1e6fb87d648 | 352 | } |
aeschsim | 55:a1e6fb87d648 | 353 | |
cittecla | 61:628f8a4e857c | 354 | int getPRESCALE(void) |
aeschsim | 55:a1e6fb87d648 | 355 | { |
aeschsim | 55:a1e6fb87d648 | 356 | char read = 255; |
aeschsim | 55:a1e6fb87d648 | 357 | char data; |
aeschsim | 55:a1e6fb87d648 | 358 | data = (char)(PRESCALE); |
aeschsim | 55:a1e6fb87d648 | 359 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 360 | i2c.read((ADRESS<<1), &read, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 361 | printf("read Prescale value: >>%x<<\r\n",read); |
aeschsim | 55:a1e6fb87d648 | 362 | return (int)read; |
aeschsim | 55:a1e6fb87d648 | 363 | } |
cittecla | 61:628f8a4e857c | 364 | |
cittecla | 61:628f8a4e857c | 365 | void enable_servos() |
cittecla | 61:628f8a4e857c | 366 | { |
aeschsim | 55:a1e6fb87d648 | 367 | Servo_enable = 0; |
cittecla | 61:628f8a4e857c | 368 | } |
cittecla | 61:628f8a4e857c | 369 | |
cittecla | 61:628f8a4e857c | 370 | void disable_servos() |
cittecla | 61:628f8a4e857c | 371 | { |
aeschsim | 55:a1e6fb87d648 | 372 | Servo_enable = 1; |
cittecla | 61:628f8a4e857c | 373 | } |