Masterin Usercode.pde
void userhook_SuperSlowLoop()
{
uint8_t check[6];
// hal.console->print("USERHOOK\n");
if (failsafe.radio || failsafe.radio_counter != 0) {
return;
}
if(g.radio_tuning > 0){
hal.scheduler->delay(1);
_i2c_sem = hal.i2c->get_semaphore();
if (!_i2c_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
// hal.scheduler->panic(PSTR("Failed to get Atmega328p"));
}
if(hal.i2c->read(0x99, (int8_t)6, check) != 0)
{
}else
{
int16_t ch1_value = check[0] << 8 | check[1];
int16_t ch2_value = check[2] << 8 | check[3];
int16_t ch3_value = check[4] << 8 | check[5];
float ch1f_value= (float) ch1_value/ 1000.0f;
float ch2f_value= (float) ch2_value/ 1000.0f;
float ch3f_value= (float) ch3_value/ 1000.0f;
hal.console->print("P");
hal.console->print(ch1f_value);
hal.console->print("I");
hal.console->print(ch2f_value);
hal.console->print("D");
hal.console->print(ch3f_value);
hal.console->print("\n");
g.pid_rate_roll.kP(ch3f_value);
g.pid_rate_pitch.kP(ch3f_value);
g.pid_rateroll.kI(ch3f_value);
g.pid_rate_pitch.kI(ch3f_value);
g.pid_rate_roll.kD(ch3f_value);
g.pid_rate_pitch.kD(ch3f_value);
}
_i2c_sem->give();
}
}
#endif
Slave as Receiver
float low_P_value = 0.15;
float high_P_value = 0.2;
float low_I_value = 0.002;
float high_I_value = 0.010;
float low_D_value = 0.0;
float high_D_value = 0.5;
void setup()
{
Wire.begin(0x99);
Wire.onRequest(requestEvent);
}
void requestEvent()
{
ppm[0] = (int)(fscale( 1100, 1900, low_P_value, high_P_value, ppm[0], 0.0) * 1000);
ppm[1] = (int)(fscale( 1100, 1900, low_I_value, high_I_value, ppm[1], 0.0) * 1000);
ppm[2] = (int)(fscale( 1100, 1900, low_D_value, high_D_value, ppm[2], 0.0) * 1000);
char check[6];
check[0] = (ppm[0] >> 8);
check[1] = (ppm[0] & 255);
check[2] = (ppm[1] >> 8);
check[3] = (ppm[1] & 255);
check[4] = (ppm[2] >> 8);
check[5] = (ppm[2] & 255);
Wire.write(check, 6);
}
Its not completed project.. only the first step of code.