4WD Chassis

Very nice! I like how this project looks like.

App controls should work the same as for “4WD car”. On Android Joystick widget seems broken when trying to mix X and Y axis and driving motors… It came from old days and haven’t been tested properly.

You can try different approach:

  1. Connect App with X4.
  2. Create joystick widget and add functionA for X and functionB for Y axis.
  3. Intercept these values inside X4.
  4. Compute drivie direction accoding to Joystick position.
  5. Update motors.

App configuration:

Arduino code:

// Variables holding joystick position
int x, y;
bool updated;
// Forward declaration of motor direction calculation function
void updateMotors(int drive, int turn);
// Function called when joystick is moved in X direction
void eventFunctionA() {
  x = X4.functionA.get(); // Value sent to functionA (Joystick X)
  updated = true;
}
// Function called when joystick is moved in Y direction
void eventFunctionB() {
  y = X4.functionB.get(); // Value sent to functionB (Joystick Y)
  updated = true;
}
void setup() {
  // Start printing for Serial Monitor
  Serial.begin(9600);
  // Register event handlers for functions
  X4.functionA.addEvent(eventFunctionA);
  X4.functionB.addEvent(eventFunctionB);
}
void loop() {
  // Check if new value received from Joystick
  if (!updated) return;
  // Mark that value is read and wait for next event
  updated = false;
  // Update motors
  updateMotors(y, x);
  // Print updated joystick position
  Serial.printf("X: % 2d, Y: % 2d\n", x, y);
}
// Constant how much torque to apply when turning
int turnIntensity = 100;
// Calcuate and update motors power
void updateMotors(int drive, int turn) {
    // Validate input values range
    turn = constrain(turn, -100, 100);
    drive = constrain(drive, -100, 100);
    // Calculate turn intensity
    int intensity = turnIntensity + ((100-turnIntensity)*abs(drive)/100);
    turn = (turn*intensity)/100;
    // Calculate motors power
    int A = constrain(drive+turn, -100, 100);
    int B = constrain(drive-turn, -100, 100);
    int C = A;
    int D = B;
    // Update motors
    X4.dc.power(A, B, C, D);
}

Of course configure motor wiring and spin direction according to your robot.

Also, see your motors has encoders. Unfortunately board v.1.0 has limited GPIO functionality. If using v.1.1 revision board, you could hook encoders to GPIO and with additional coding - control exact motors speed (not only power).

2 Likes