Tobis Programm forked to not destroy your golden files
Fork of Robocode by
source/Robot.cpp@117:66d64dbd1b36, 2017-05-10 (annotated)
- Committer:
- cittecla
- Date:
- Wed May 10 10:02:53 2017 +0000
- Revision:
- 117:66d64dbd1b36
- Parent:
- 99:78d87027c85b
- Child:
- 120:cdf7a6751f9e
reversed to old IR sensor measurement, search for brick is now working better, still not perfect.
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 | |
aeschsim | 73:f7657ddb7827 | 25 | #define servo0center 300 |
aeschsim | 73:f7657ddb7827 | 26 | #define servo2center 300 |
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 | 34:40d8d29b44b8 | 138 | void set_speed(float left, float right) |
cittecla | 34:40d8d29b44b8 | 139 | { |
cittecla | 94:0381e8b1beda | 140 | enableMotorDriver = 0; |
cittecla | 34:40d8d29b44b8 | 141 | desiredSpeedLeft = left+correction_value; //50 RPM |
cittecla | 34:40d8d29b44b8 | 142 | desiredSpeedRight = -right; //50 RPM |
cittecla | 89:7f9d6e641a01 | 143 | enableMotorDriver = 1; |
cittecla | 88:b89cace9329b | 144 | printf("real speed set as: %f\r\n",desiredSpeedLeft); |
cittecla | 34:40d8d29b44b8 | 145 | } |
cittecla | 34:40d8d29b44b8 | 146 | |
cittecla | 61:628f8a4e857c | 147 | float get_speed_left() |
cittecla | 61:628f8a4e857c | 148 | { |
cittecla | 61:628f8a4e857c | 149 | return actualSpeedLeft; |
cittecla | 61:628f8a4e857c | 150 | } |
cittecla | 61:628f8a4e857c | 151 | |
cittecla | 61:628f8a4e857c | 152 | float get_speed_right() |
cittecla | 61:628f8a4e857c | 153 | { |
cittecla | 61:628f8a4e857c | 154 | return actualSpeedRight; |
cittecla | 61:628f8a4e857c | 155 | } |
cittecla | 61:628f8a4e857c | 156 | |
cittecla | 34:40d8d29b44b8 | 157 | |
cittecla | 34:40d8d29b44b8 | 158 | void speedCtrl() |
cittecla | 34:40d8d29b44b8 | 159 | { |
cittecla | 34:40d8d29b44b8 | 160 | // Berechnen die effektiven Drehzahlen der Motoren in [rpm] |
cittecla | 34:40d8d29b44b8 | 161 | short valueCounterLeft = counterLeft.read(); |
cittecla | 34:40d8d29b44b8 | 162 | short valueCounterRight = counterRight.read(); |
cittecla | 34:40d8d29b44b8 | 163 | short countsInPastPeriodLeft = valueCounterLeft-previousValueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 164 | short countsInPastPeriodRight = valueCounterRight-previousValueCounterRight; |
cittecla | 34:40d8d29b44b8 | 165 | |
cittecla | 34:40d8d29b44b8 | 166 | previousValueCounterLeft = valueCounterLeft; |
cittecla | 34:40d8d29b44b8 | 167 | previousValueCounterRight = valueCounterRight; |
cittecla | 34:40d8d29b44b8 | 168 | actualSpeedLeft = speedLeftFilter.filter((float)countsInPastPeriodLeft /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 169 | actualSpeedRight = speedRightFilter.filter((float)countsInPastPeriodRight /COUNTS_PER_TURN/PERIOD*60.0f); |
cittecla | 34:40d8d29b44b8 | 170 | |
cittecla | 34:40d8d29b44b8 | 171 | // Berechnen der Motorspannungen Uout |
cittecla | 34:40d8d29b44b8 | 172 | float voltageLeft = KP*(desiredSpeedLeft-actualSpeedLeft)+desiredSpeedLeft/KN; |
cittecla | 34:40d8d29b44b8 | 173 | float voltageRight = KP*(desiredSpeedRight-actualSpeedRight)+desiredSpeedRight/KN; |
cittecla | 34:40d8d29b44b8 | 174 | |
cittecla | 34:40d8d29b44b8 | 175 | // Berechnen, Limitieren und Setzen der Duty-Cycle |
cittecla | 34:40d8d29b44b8 | 176 | float dutyCycleLeft = 0.5f+0.5f*voltageLeft/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 177 | if (dutyCycleLeft < MIN_DUTY_CYCLE) dutyCycleLeft = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 178 | else if (dutyCycleLeft > MAX_DUTY_CYCLE) dutyCycleLeft = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 179 | pwmLeft = dutyCycleLeft; |
cittecla | 94:0381e8b1beda | 180 | |
cittecla | 34:40d8d29b44b8 | 181 | float dutyCycleRight = 0.5f+0.5f*voltageRight/MAX_VOLTAGE; |
cittecla | 34:40d8d29b44b8 | 182 | if (dutyCycleRight < MIN_DUTY_CYCLE) dutyCycleRight = MIN_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 183 | else if (dutyCycleRight > MAX_DUTY_CYCLE) dutyCycleRight = MAX_DUTY_CYCLE; |
cittecla | 34:40d8d29b44b8 | 184 | pwmRight = dutyCycleRight; |
cittecla | 34:40d8d29b44b8 | 185 | } |
cittecla | 34:40d8d29b44b8 | 186 | |
cittecla | 34:40d8d29b44b8 | 187 | //Sensors |
cittecla | 34:40d8d29b44b8 | 188 | void Sensor_init() |
cittecla | 34:40d8d29b44b8 | 189 | { |
cittecla | 34:40d8d29b44b8 | 190 | for( int ii = 0; ii<6; ++ii) |
cittecla | 72:4e8a151d804e | 191 | sensors[ii].init(&distance,&distance2, &bit0, &bit1, &bit2, ii); |
cittecla | 80:92b9d083322d | 192 | IRenable = 1; |
cittecla | 94:0381e8b1beda | 193 | } |
cittecla | 94:0381e8b1beda | 194 | |
cittecla | 117:66d64dbd1b36 | 195 | |
cittecla | 34:40d8d29b44b8 | 196 | |
cittecla | 34:40d8d29b44b8 | 197 | float getDistanceIR(int number) |
cittecla | 34:40d8d29b44b8 | 198 | { |
cittecla | 34:40d8d29b44b8 | 199 | return sensors[number]; |
cittecla | 34:40d8d29b44b8 | 200 | } |
cittecla | 34:40d8d29b44b8 | 201 | |
aeschsim | 55:a1e6fb87d648 | 202 | //Color |
cittecla | 61:628f8a4e857c | 203 | bool get_color() |
cittecla | 61:628f8a4e857c | 204 | { |
aeschsim | 81:956f65714207 | 205 | if (red == 1 && green == 0) { //Active - LOW |
aeschsim | 55:a1e6fb87d648 | 206 | return 1; |
aeschsim | 55:a1e6fb87d648 | 207 | } else { |
aeschsim | 55:a1e6fb87d648 | 208 | return 0; |
aeschsim | 55:a1e6fb87d648 | 209 | } |
aeschsim | 55:a1e6fb87d648 | 210 | } |
aeschsim | 55:a1e6fb87d648 | 211 | |
cittecla | 34:40d8d29b44b8 | 212 | //IMU |
cittecla | 34:40d8d29b44b8 | 213 | float read_acc_x() |
cittecla | 34:40d8d29b44b8 | 214 | { |
cittecla | 34:40d8d29b44b8 | 215 | return imu.readAccelerationX(); |
cittecla | 34:40d8d29b44b8 | 216 | } |
cittecla | 34:40d8d29b44b8 | 217 | |
cittecla | 34:40d8d29b44b8 | 218 | float read_acc_y() |
cittecla | 34:40d8d29b44b8 | 219 | { |
cittecla | 34:40d8d29b44b8 | 220 | return imu.readAccelerationY(); |
cittecla | 34:40d8d29b44b8 | 221 | } |
cittecla | 34:40d8d29b44b8 | 222 | |
cittecla | 34:40d8d29b44b8 | 223 | float read_acc_z() |
cittecla | 34:40d8d29b44b8 | 224 | { |
cittecla | 34:40d8d29b44b8 | 225 | return imu.readAccelerationZ(); |
cittecla | 34:40d8d29b44b8 | 226 | } |
cittecla | 34:40d8d29b44b8 | 227 | |
cittecla | 34:40d8d29b44b8 | 228 | float read_gyr_x() |
cittecla | 34:40d8d29b44b8 | 229 | { |
cittecla | 34:40d8d29b44b8 | 230 | return imu.readGyroX(); |
cittecla | 34:40d8d29b44b8 | 231 | } |
cittecla | 34:40d8d29b44b8 | 232 | |
cittecla | 34:40d8d29b44b8 | 233 | float read_gyr_y() |
cittecla | 34:40d8d29b44b8 | 234 | { |
cittecla | 34:40d8d29b44b8 | 235 | return imu.readGyroY(); |
cittecla | 34:40d8d29b44b8 | 236 | } |
cittecla | 34:40d8d29b44b8 | 237 | |
cittecla | 34:40d8d29b44b8 | 238 | float read_gyr_z() |
cittecla | 34:40d8d29b44b8 | 239 | { |
cittecla | 34:40d8d29b44b8 | 240 | return imu.readGyroZ(); |
cittecla | 34:40d8d29b44b8 | 241 | } |
cittecla | 34:40d8d29b44b8 | 242 | |
cittecla | 34:40d8d29b44b8 | 243 | float read_heading() |
cittecla | 34:40d8d29b44b8 | 244 | { |
cittecla | 38:3526c36e4c73 | 245 | double deg = (double)(180.0f/(double)M_PI*imu.readHeading()); |
cittecla | 38:3526c36e4c73 | 246 | return deg; |
cittecla | 34:40d8d29b44b8 | 247 | } |
cittecla | 34:40d8d29b44b8 | 248 | |
cittecla | 34:40d8d29b44b8 | 249 | |
cittecla | 34:40d8d29b44b8 | 250 | // Servo I2C |
aeschsim | 55:a1e6fb87d648 | 251 | |
aeschsim | 55:a1e6fb87d648 | 252 | |
aeschsim | 55:a1e6fb87d648 | 253 | void init_servo(int freq) |
aeschsim | 55:a1e6fb87d648 | 254 | { |
aeschsim | 55:a1e6fb87d648 | 255 | char data[2]; |
aeschsim | 55:a1e6fb87d648 | 256 | //Reset |
aeschsim | 55:a1e6fb87d648 | 257 | data[0] = (char) MODE1; |
cittecla | 61:628f8a4e857c | 258 | data[1] = (char) SLEEP; |
aeschsim | 55:a1e6fb87d648 | 259 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 260 | |
aeschsim | 55:a1e6fb87d648 | 261 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 262 | |
aeschsim | 55:a1e6fb87d648 | 263 | float prescaleval = 25000000.0; //25MHz |
aeschsim | 55:a1e6fb87d648 | 264 | prescaleval /= 4096.0f; //12-Bit |
aeschsim | 42:08b3aea29254 | 265 | prescaleval /= (float)freq; |
aeschsim | 43:9f291a496db8 | 266 | prescaleval -= 1.0f; |
aeschsim | 55:a1e6fb87d648 | 267 | char prescale = (char)(prescaleval); //0x64 bei 50Hz |
cittecla | 61:628f8a4e857c | 268 | |
aeschsim | 55:a1e6fb87d648 | 269 | data[0] = (char) PRESCALE; |
aeschsim | 55:a1e6fb87d648 | 270 | data[1] = prescale; |
aeschsim | 55:a1e6fb87d648 | 271 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 272 | |
aeschsim | 55:a1e6fb87d648 | 273 | data[0] = (char) MODE1; |
aeschsim | 55:a1e6fb87d648 | 274 | data[1] = 0x00; //wake up |
aeschsim | 55:a1e6fb87d648 | 275 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 276 | |
aeschsim | 55:a1e6fb87d648 | 277 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 278 | |
aeschsim | 55:a1e6fb87d648 | 279 | data[0] = (char) MODE1; |
cittecla | 61:628f8a4e857c | 280 | data[1] = 0x80; //do restart |
aeschsim | 55:a1e6fb87d648 | 281 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 282 | |
aeschsim | 55:a1e6fb87d648 | 283 | wait_ms(1); |
cittecla | 61:628f8a4e857c | 284 | |
aeschsim | 55:a1e6fb87d648 | 285 | data[0] = (char) MODE2; |
aeschsim | 55:a1e6fb87d648 | 286 | data[1] = (char) 0x04; |
aeschsim | 55:a1e6fb87d648 | 287 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 288 | |
aeschsim | 55:a1e6fb87d648 | 289 | wait_ms(1); |
aeschsim | 55:a1e6fb87d648 | 290 | |
aeschsim | 55:a1e6fb87d648 | 291 | printf("init done...\r\n"); |
aeschsim | 55:a1e6fb87d648 | 292 | } |
aeschsim | 42:08b3aea29254 | 293 | |
aeschsim | 55:a1e6fb87d648 | 294 | void set_servo_position(int servo, int deg) |
aeschsim | 55:a1e6fb87d648 | 295 | { |
cittecla | 62:c2fcf3b349e9 | 296 | // Servo 0 = IR_left |
cittecla | 62:c2fcf3b349e9 | 297 | // Servo 2 = IR_right |
cittecla | 62:c2fcf3b349e9 | 298 | // Servo 4 = Arm_1 |
cittecla | 62:c2fcf3b349e9 | 299 | // Servo 6 = Arm_2 |
cittecla | 62:c2fcf3b349e9 | 300 | // Servo 8 = Grabber |
aeschsim | 73:f7657ddb7827 | 301 | if (servo == 0) { |
aeschsim | 73:f7657ddb7827 | 302 | deg = servo0center + 3*deg; |
aeschsim | 73:f7657ddb7827 | 303 | } |
aeschsim | 73:f7657ddb7827 | 304 | if (servo == 2) { |
aeschsim | 73:f7657ddb7827 | 305 | deg = servo2center + 3*deg; |
aeschsim | 73:f7657ddb7827 | 306 | } |
cittecla | 94:0381e8b1beda | 307 | |
aeschsim | 73:f7657ddb7827 | 308 | //only for tests with putty |
aeschsim | 55:a1e6fb87d648 | 309 | if (deg < 0 || deg > 4095) { |
aeschsim | 55:a1e6fb87d648 | 310 | deg = 300; |
cittecla | 94:0381e8b1beda | 311 | } |
aeschsim | 55:a1e6fb87d648 | 312 | char data[2]; |
aeschsim | 55:a1e6fb87d648 | 313 | int16_t wert1 = (deg>>8); |
aeschsim | 55:a1e6fb87d648 | 314 | int16_t wert2 = (deg & 0xFF); |
aeschsim | 55:a1e6fb87d648 | 315 | printf("%x %x servo deg\r\n",wert1,wert2); |
cittecla | 61:628f8a4e857c | 316 | |
aeschsim | 55:a1e6fb87d648 | 317 | data[0] = (char)LED0_ON_L+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 318 | data[1] = 0x00; |
aeschsim | 55:a1e6fb87d648 | 319 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 320 | |
aeschsim | 55:a1e6fb87d648 | 321 | data[0] = (char)LED0_ON_H+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 322 | data[1] = 0x00; |
aeschsim | 55:a1e6fb87d648 | 323 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 324 | |
aeschsim | 55:a1e6fb87d648 | 325 | data[0] = (char)LED0_OFF_L+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 326 | data[1] = (char)wert2; |
aeschsim | 55:a1e6fb87d648 | 327 | i2c.write((ADRESS<<1), data,2); |
cittecla | 61:628f8a4e857c | 328 | |
aeschsim | 55:a1e6fb87d648 | 329 | data[0] = (char)LED0_OFF_H+(4*servo); |
aeschsim | 55:a1e6fb87d648 | 330 | data[1] = (char)wert1; |
cittecla | 61:628f8a4e857c | 331 | i2c.write((ADRESS<<1), data,2); |
aeschsim | 55:a1e6fb87d648 | 332 | } |
aeschsim | 55:a1e6fb87d648 | 333 | |
cittecla | 61:628f8a4e857c | 334 | int getPWM(uint8_t servo) |
aeschsim | 55:a1e6fb87d648 | 335 | { |
aeschsim | 55:a1e6fb87d648 | 336 | char read = 255; |
aeschsim | 55:a1e6fb87d648 | 337 | char secondread = 255; |
aeschsim | 55:a1e6fb87d648 | 338 | char data; |
aeschsim | 55:a1e6fb87d648 | 339 | data = (char)(LED0_ON_L+(4*servo)); |
aeschsim | 55:a1e6fb87d648 | 340 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 341 | i2c.read((ADRESS<<1), &read, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 342 | data = (char)(LED0_ON_H+(4*servo)); |
aeschsim | 55:a1e6fb87d648 | 343 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 344 | i2c.read((ADRESS<<1), &secondread, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 345 | printf("LED %d ON_L: %x ON_H: %x\r\n",servo, read, secondread); |
aeschsim | 55:a1e6fb87d648 | 346 | return (int)read; |
aeschsim | 55:a1e6fb87d648 | 347 | } |
aeschsim | 55:a1e6fb87d648 | 348 | |
cittecla | 61:628f8a4e857c | 349 | int getPRESCALE(void) |
aeschsim | 55:a1e6fb87d648 | 350 | { |
aeschsim | 55:a1e6fb87d648 | 351 | char read = 255; |
aeschsim | 55:a1e6fb87d648 | 352 | char data; |
aeschsim | 55:a1e6fb87d648 | 353 | data = (char)(PRESCALE); |
aeschsim | 55:a1e6fb87d648 | 354 | i2c.write((ADRESS<<1), &data, 1, 1); |
aeschsim | 55:a1e6fb87d648 | 355 | i2c.read((ADRESS<<1), &read, 1, 0); |
aeschsim | 55:a1e6fb87d648 | 356 | printf("read Prescale value: >>%x<<\r\n",read); |
aeschsim | 55:a1e6fb87d648 | 357 | return (int)read; |
aeschsim | 55:a1e6fb87d648 | 358 | } |
cittecla | 61:628f8a4e857c | 359 | |
cittecla | 61:628f8a4e857c | 360 | void enable_servos() |
cittecla | 61:628f8a4e857c | 361 | { |
aeschsim | 55:a1e6fb87d648 | 362 | Servo_enable = 0; |
cittecla | 61:628f8a4e857c | 363 | } |
cittecla | 61:628f8a4e857c | 364 | |
cittecla | 61:628f8a4e857c | 365 | void disable_servos() |
cittecla | 61:628f8a4e857c | 366 | { |
aeschsim | 55:a1e6fb87d648 | 367 | Servo_enable = 1; |
cittecla | 61:628f8a4e857c | 368 | } |