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