Sunday, November 6, 2011

PC application - Some code to start

For those who want to develop a PC application to configure the gyro.
Remember: This is only possible with the bootloader running!

/* Configuration */
typedef struct _config_
{
uint8_t version;
uint8_t mark[3];
uint16_t servo_freq; //not used
uint16_t hi_endpoint; // [1600..2100]
uint16_t low_endpoint; //[900..1400]
uint16_t midpoint; //not used
uint8_t reverse; // [0..1]
uint8_t rudder_deadband;// [1..100]
uint8_t rudder_speed; // [1..8]
uint8_t delay_cw; // [1..8]
uint8_t delay_ccw; // [1..8]
uint8_t rudder_delay; // [1..8]
uint8_t servo_speed; // [1..140] ms
uint8_t kP; // [1..128]
uint8_t kI; // [1..128]
uint8_t kD; // [1..128]
} config_t;

int enter_gyro(void)
{
SerialWrite(0x1B);
delay_ms(500);
SerialFlush();
SerialWrite('7');
if(SerialRead(200) == '?')
return 1;
return 0;
}

int read_gyro( uint16_t address, uint8_t *buf, uint16_t size)
{
if(!enter_gyro())
return 0;
SerialFlush();
SerialWrite('P'); // Enter Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
SerialWrite('A'); // Set Address
_delay_ms(1);
SerialWrite(address>>8);
_delay_ms(1);
SerialWrite(address&0xFF);
if(SerialRead(1000) != 0x0D) return 0;
while(size--)
{
SerialWrite('d'); // Read Data Memory
*buf++ = SerialRead(1000);
}
SerialWrite('L'); // Leave Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
return 1;
}

int write_gyro( uint16_t address, uint8_t *buf, uint16_t size)
{
SerialFlush();
SerialWrite('P'); // Enter Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
SerialWrite('A'); // Set Address
_delay_ms(1);
SerialWrite(address>>8);
_delay_ms(1);
SerialWrite(address&0xFF);
if(SerialRead(1000) != 0x0D) return 0;
while(size--)
{
SerialWrite('D'); // Write Data Memory
_delay_ms(1);
SerialWrite(*buf++);
if(SerialRead(1000) != 0x0D) return 0;
}
SerialWrite('L'); // Leave Prog Mode
if(SerialRead(1000) != 0x0D) return 0;
return 1;
}

void main(void)
{
read_gyro( 6, (uint8_t *) &gyro_config, sizeof(config_t));
................
write_gyro( 6, (uint8_t *) &gyro_config, sizeof(config_t));

}

The 0.04 Beta Application

Here is one more version!
You can place the gyro face up or down (the delay cw and the delay ccw will be swapped).
Increase the RudderDelay only if you get bounce with maximum values on Delay CW and CCW.
The servo frequency is fixed to 250Hz but you can change it on the code and compile again.
Please let me know your results here or on the discussion thread .

Parameters you can now adjust with transmitter:
Direction - LED - 1 blue, [1..2] red
EndPoint1- LED - 2 blue, 1 red
EndPoint2- LED - 3 blue, 1 red
RudderSpeed - LED - 4 blue, [1..8] red
Delay CW- LED - 5 blue, [1..8] red
Delay CCW- LED - 6 blue, [1..8] red
RudderDelay - LED - 7 blue, [1..8] red

Parameters possible to adjust with PC program or Program Box (to be done):
Rudder Deadband, Servo Speed, kP, kI and kD.


Download it!