Asked
Resolved Resolved by DJ Sures!

Movement Panel With Servo Control

I have modified my 3-wheel robot by replacing the two 360-degree servos with a motor equipped with a differential. This motor is controlled by a dual H-bridge using PWM signals. While I'm not entirely sure if this is the optimal setup, I'm still learning and experimenting with various possibilities. Currently, the single motor controls both rear wheels, allowing for synchronized forward and reverse movement, which is functioning well.

For steering, I have installed a servo at the front to control the wheel's left-right movement. However, I'm encountering an issue where the joystick correctly manages the forward and reverse motion but fails to control the steering. I would like to have a single control interface or "tool" that can manage both forward/reverse and left/right steering using the servo. If achieving this with a joystick is not feasible, could you suggest an alternative solution?


Related Hardware AdventureBot
Related Control Custom Movement Panel

ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

#25  

DJ mentioned that you tweak the values for the Utility map() to suit your needs. We need to see your code to see what has changed from DJ's example code to better help you.

PRO
Synthiam
#26  

Try this.... (I removed the Utility.map() and did the math inline to have better control of what it does)

Forward Script

// Set the motor direction to forward
Digital.set(d0, true); // Enable forward direction on motor
Digital.set(d1, false); // Disable reverse direction on motor

// Calculate the speed for the PWM
// Inline mapping the max specified speed (0-255) to a range for the PWM (0-100)
var topSpeed = Math.max(Movement.getSpeedLeft(), Movement.getSpeedRight());
var speed = (topSpeed - 0) * (100 - 0) / (255 - 0) + 0; // Equivalent to Utility.map(topSpeed, 0, 255, 0, 100)
PWM.set(d2, speed);

// Calculate the speed difference between left and right speeds from the movement request
var diffSpeed = Movement.getSpeedLeft() - Movement.getSpeedRight();

// Inline mapping the speed difference to a servo steering angle
// Convert the difference to a range suitable for servo positioning (1-180)
var steeringAngle = (diffSpeed - (-255)) * (180 - 1) / (255 - (-255)) + 1; 

// Set the servo position based on the calculated steering angle
Servo.setPosition(d3, steeringAngle);

Reverse Script

// Set the motor direction to reverse
Digital.set(d0, false); // Disable forward direction on motor
Digital.set(d1, true);  // Enable reverse direction on motor

// Calculate the speed for the PWM
// Inline mapping of the max specified speed (0-255) to a range for the PWM (0-100)
var topSpeed = Math.max(Movement.getSpeedLeft(), Movement.getSpeedRight());
var speed = (topSpeed - 0) * (100 - 0) / (255 - 0) + 0; // Equivalent to Utility.map(topSpeed, 0, 255, 0, 100)
PWM.set(d2, speed);

// Calculate the speed difference between left and right speeds from the movement request
var diffSpeed = Movement.getSpeedLeft() - Movement.getSpeedRight();

// Inline mapping of the speed difference to a servo steering angle
// Convert the difference to a range suitable for servo positioning (1-180)
var steeringAngle = (diffSpeed - (-255)) * (180 - 1) / (255 - (-255)) + 1; 

// Set the servo position based on the calculated steering angle
Servo.setPosition(d3, steeringAngle);
PRO
Germany
#27  

@dj sures, Significant improvement. It no longer springs to the right or left as he did before. I modified the max speed in the joystick configuration. That way,i can steer correctly. Very good, thanks. I'll try with a wireless joystick.Not sure it will better but id like to try the skill Joystick (Direct Input).

PRO
Synthiam
#28  

Check to ensure your joystick is a Direct Input or XInput type. You'll know if one doesn't work, try the other. Newer joysticks are usually XInput. Some have a switch that will say D or X on it. Old joysticks are Direct Input.

@Athena explain the difference between Direct Input and XInput for joysticks

PRO
Canada
#29  

Certainly! Here's a brief explanation of the differences between Direct Input and XInput for joysticks:

Direct Input:

  • Legacy API: Direct Input is an older API developed by Microsoft for handling input from a variety of devices, including joysticks, gamepads, and other peripherals.
  • Compatibility: It supports a wide range of devices and is compatible with older hardware.
  • Customization: Direct Input allows for more customization and supports a broader range of input devices, including those with more than the standard number of buttons and axes.
  • Complexity: It can be more complex to set up and use, as it requires more manual configuration.

XInput:

  • Modern API: XInput is a newer API also developed by Microsoft, specifically designed for Xbox controllers and compatible devices.
  • Standardization: It provides a standardized interface for Xbox controllers, making it easier to use with modern games and applications.
  • Limited Support: XInput supports a limited number of buttons and axes, typically those found on an Xbox controller.
  • Ease of Use: It is generally easier to set up and use, as it requires less manual configuration and is often automatically recognized by modern games and applications.

Choosing Between Them:

  • Device Type: If you have a newer Xbox-style controller, XInput is likely the best choice.
  • Older Devices: For older or non-standard devices, Direct Input may be necessary.
  • Game/Application Requirements: Some games and applications may only support one of the two, so it's essential to check compatibility.

If you have any more questions or need further assistance, feel free to ask!


I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.