5 years, 1 month ago.

H bridge controlled by Joystick

Hi, I want to build a small car controlled by Joystick.

The problem is with the interpretation of analog inputs. Joystick provide range from 0-256 but I have to interpret them in a specific way: Y (100-150) vehicle doesn't move (thanks to contact vibrations) in other ranges, it must move in all directions, that e.g. when X=200 && Y=200 the vehicle has to turn right and if X=200 && Y=0 car has to rotate in place.

I've created a program that gives you variables from -255 to 255, but I don't know how to go forward.

include the mbed library with this snippet

float map(float Q, float A, float B, float C, float D){
            return ((D-C)/(B-A)*(Q-A)+C);     
        

            //X
if(X0>100 && X0<150){  
            X1=0;
        }else if((X0)<100){
            X1=map(X0,0, 100, -256, 0);               
        }else if((X0)>150){
            X1=map(X0,150, 256, 0, 256);                    
        } 
        X=X1/256;  
        
            // Y
        if(Y0>100 && Y0<150){  
            Y1=0;
        }else if((Y0)<100){
            Y1=map(Y0,0, 100, -256, 0);               
        }else if((Y0)>150){
            Y1=map(Y0,150, 256, 0, 256);                    
        } 
        Y=Y1/256;  

I use Nucleo F103RB

Seems like this may need more work defining the problem more precisely before trying to jump in and write code. So your inputs are an X and Y variable each in the range 0-255. What output do you need? Maybe 2 motors (L and R) and each gets a -255 to +255 signal for the speed so they can go forward and backwards? I would probably try to work it out first without worrying about the deadzones. You could then apply the deadzone mapping over the normal mapping. Stackoverflow suggests the answer may be this simple, where the outputs are clamped to maximum value:

left=y+x; 
right=y-x;
posted by Graham S. 03 Mar 2019

actually, I did exactly that but with X/2. And it works well :-)

posted by Mateusz Kociołek 03 Mar 2019

1 Answer

5 years, 1 month ago.

Joystick unit can be used for controlling motors and it's actually easy to do. Joystick has two main potentiometers one for each axis. Meters are just common resistors with third pin that can slide over resistive stuff. One end of resistive stuff is linked to earth and other to the positive. By sliding the third make contact we can get voltage between earth and positive potential. Joystick is kept in center by two leaps one leaps for each axis. Now if we move expand meters will also change position and voltage will vary on output pins. If VCC is 5V and GND is 0V then we will have output voltage sort from 0V to 5V relating on point of thrive. When joystick is at rest point voltage on both X and Y pins will be 2.5V. It's because meter pin is accurately in center and we will get half of input voltage on output pins. If we start moving thrive to the right then we are altering position of Y axis meter and moving it to the end that is linked to positive and then we are receiving higher and higher voltage on output of Y axis. Moving it in left is the same just in overturn voltage is going down. For X axis if we move up voltage goes up and if we move down voltage it goes down. Related on the useful voltage and the motor itself at lesser speeds the motor is not capable to start moving and it creates a vibrant sound. In this case the motors were not able to move if the rate of the PWM pointer was below 70. So using this two if reports I truly limited to speed range from 70 to 255. At the end we just send the last motor speeds or PWM signal to the allow pins of the L298N device controller. Make use of this website for getting more info.