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