ADD: support for chinese nunchuck

This commit is contained in:
Niklas Fauth
2018-05-06 20:11:53 +02:00
parent 5326e558ae
commit 7337a8ae7e
5 changed files with 424 additions and 424 deletions

View File

@@ -175,7 +175,7 @@ void DMA1_Channel1_IRQHandler() {
if(ABS((adc_buffer.dcl - offsetdcl) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > 50 || enable == 0) {
if(ABS((adc_buffer.dcl - offsetdcl) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) {
LEFT_TIM->BDTR &= ~TIM_BDTR_MOE;
//HAL_GPIO_WritePin(LED_PORT, LED_PIN, 1);
} else {
@@ -183,7 +183,7 @@ void DMA1_Channel1_IRQHandler() {
//HAL_GPIO_WritePin(LED_PORT, LED_PIN, 0);
}
if(ABS((adc_buffer.dcr - offsetdcr) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > 50 || enable == 0) {
if(ABS((adc_buffer.dcr - offsetdcr) * MOTOR_AMP_CONV_DC_AMP) > DC_CUR_LIMIT || timeout > TIMEOUT || enable == 0) {
RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE;
} else {
RIGHT_TIM->BDTR |= TIM_BDTR_MOE;

View File

@@ -9,7 +9,7 @@ uint8_t ppm_count = 0;
uint32_t timeout = 100;
uint8_t nunchuck_data[6] = {0};
uint8_t ai2cBuffer[6];
uint8_t i2cBuffer[2];
extern I2C_HandleTypeDef hi2c2;
DMA_HandleTypeDef hdma_i2c2_rx;
@@ -60,27 +60,29 @@ void PPM_Init() {
void Nunchuck_Init() {
//-- START -- init WiiNunchuck
ai2cBuffer[0] = 0xF0;
ai2cBuffer[1] = 0x55;
//Originale
ai2cBuffer[0] = 0x40;
ai2cBuffer[1] = 0x00;
//HAL_I2C_Master_Transmit_DMA(&hi2c2, 0xA4, (uint8_t*)ai2cBuffer, 2);
//while(wii_JOYdati.I2CTxDone ==0);
//wii_JOYdati.I2CTxDone = 0;
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)ai2cBuffer, 2, 100);
i2cBuffer[0] = 0xF0;
i2cBuffer[1] = 0x55;
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 2, 100);
HAL_Delay(10);
i2cBuffer[0] = 0xFB;
i2cBuffer[1] = 0x00;
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 2, 100);
HAL_Delay(10);
//wii_JOYdati.done = 0;
}
void Nunchuck_Read() {
ai2cBuffer[0] = 0x00;
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)ai2cBuffer, 1, 100);
i2cBuffer[0] = 0x00;
HAL_I2C_Master_Transmit(&hi2c2,0xA4,(uint8_t*)i2cBuffer, 1, 100);
HAL_Delay(2);
HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)ai2cBuffer, 6, 100);
for (int i = 0; i < 6; i++) {
nunchuck_data[i] = (ai2cBuffer[i] ^ 0x17) + 0x17;
if (HAL_I2C_Master_Receive(&hi2c2,0xA4,(uint8_t*)nunchuck_data, 6, 100) == HAL_OK) {
timeout = 0;
} else {
timeout++;
}
//setScopeChannel(0, (int)nunchuck_data[0]);
//setScopeChannel(1, (int)nunchuck_data[1]);
//setScopeChannel(2, (int)nunchuck_data[5] & 1);

View File

@@ -123,13 +123,11 @@ int main(void) {
#ifdef CONTROL_NUNCHUCK
Nunchuck_Read();
cmd1 = CLAMP((nunchuck_data[0] - 127) * 10, -1000, 1000); // y - axis. Nunchuck joystick readings range 30 - 230
cmd2 = CLAMP((nunchuck_data[1] - 127) * 10, -1000, 1000); // x - axis
cmd1 = CLAMP((nunchuck_data[0] - 127) * 8, -1000, 1000); // x - axis. Nunchuck joystick readings range 30 - 230
cmd2 = CLAMP((nunchuck_data[1] - 128) * 8, -1000, 1000); // y - axis
//uint8_t button1 = (uint8_t)nunchuck_data[5] & 1;
//uint8_t button2 = (uint8_t)(nunchuck_data[5] >> 1) & 1;
timeout = 0;
#endif
#ifdef CONTROL_PPM
@@ -145,8 +143,8 @@ int main(void) {
#endif
// ####### LOW-PASS FILTER #######
speed = speed * (1.0 - FILTER) + cmd1 * FILTER;
steer = steer * (1.0 - FILTER) + cmd2 * FILTER;
steer = steer * (1.0 - FILTER) + cmd1 * FILTER;
speed = speed * (1.0 - FILTER) + cmd2 * FILTER;
setScopeChannel(0, (int)speed);
setScopeChannel(1, (int)steer);
@@ -156,7 +154,7 @@ int main(void) {
speedL = CLAMP(speed * SPEED_COEFFICIENT + steer * STEER_COEFFICIENT, -1000, 1000);
setScopeChannel(2, (int)speedR);
setScopeChannel(3, (int)speedL);
setScopeChannel(3, (int)timeout);
// ####### ADDITIONAL CODE #######
#ifdef ADDITIONAL_CODE
@@ -164,7 +162,7 @@ int main(void) {
#endif
// ####### SET OUTPUTS #######
if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50) && timeout < 50) {
if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50) && timeout < TIMEOUT) {
pwmr = speedR;
pwml = -speedL;
}
@@ -175,8 +173,6 @@ int main(void) {
// ####### LOG TO CONSOLE #######
consoleScope();
timeout=0;
if (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) {
enable = 0;
while (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) {}