Motor Driver control with Display and Joystick helper classes

I find myself constantly copy/pasting my simplay display code and having the re-work my joystick control methods each time I want to reuse them.  Today, I moved all that logic in two helper classes.  The JoystickHelper class is particularly useful and is much better than the built in GHI joystick methods. To demonstrate their usage, I created a simple example using the motor driver module.  The goal is to use the joytick module to control a  small motor.  The engine will spin forward or reverse when the joystick is moved up or down.  Also, the motor will spin faster or slower depending on how far the joystick is moved.  The JoystickHelper is entirely event driven.  Therefore, you don’t have to determine if the joystick was actually moved.  The event will only fire when the position has actually changed. This makes programming the joysitck trivial.

The SimpleDisplayHelper, simply shows the position of the joystick on the screen. It contains all the logic to clear the screen when full and display the next text at the top of the screen.

This full solution can be found on my Gadgeteer GitHub repository.

Direct links to the helpers:

As usual, the setup is easy.

Motor Driver Setup

For assembly, I set my power supply to 7V and a 500mA cutoff. There seems to be some voltage dropout from the motor driver.  I haven’t really investigated it yet. However, if I set my power supply to 3V, the max for my motor, the driver barely give enough power to the motor for it to spin.  7V seems to be equivalent.

Motor Driver Assembly

Here’s a closeup of the assembly.

Motor Driver Assembly Closeup

Notice how the screen is full?  The next time the joystick is moved, the screen will clear and the next data row will appear at the top.  Let’s handle the SimpleDisplayHelper first.

Using the code is very simple. Create a class level instance of the helper with the display and font you want to use. Then, call DisplayText with the text you want to display. All the other logic to calculate screen height and font height are encapsulated in the helper.

            displayHelper = new SimpleDisplayHelper(display_T35,font);
            displayHelper.DisplayText("Program Started");

The JoystickHelper is also very easy to use. Create an instance of the helper and subscribe to the JoystickMoved event.

            joystickHelper = new JoystickHelper(joystick);
            joystickHelper.JoystickMoved += JoystickHelperJoystickMoved;

The JoystickMoved event give you a JoystickEventsArgs

void JoystickHelperJoystickMoved(object sender, JoystickEventsArgs args)

The JoystickEventArgs has the new X and Y joystick positins.

    public class JoystickEventsArgs : EventArgs
        private double xPosition, yPosition;

        public JoystickEventsArgs(double x, double y)
            this.xPosition = x;
            this.yPosition = y;

        public double X
            get { return this.xPosition; }
        public double Y
            get { return this.yPosition; }

Here is my JoystickMoved event handler for the motor driver example.

        void JoystickHelperJoystickMoved(object sender, JoystickEventsArgs args)
            displayHelper.DisplayText("X : "+args.X+ " -- Y : "+args.Y);
            if (args.Y < 0.6 && args.Y > 0.4)
                motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 0, 100);
            if (args.Y == 1.0) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 100, 1000);
            if (args.Y == 1.0) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 100, 1000); return; }
            if (args.Y == -1.0) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, -100, 1000); return; }

            if (args.Y < 1.0 && args.Y >= 0.9) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 75, 500); return; }
            if (args.Y < 0.9 && args.Y >= 0.8) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 50, 500); return; }
            if (args.Y < 0.8 && args.Y >= 0.7) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 25, 500); return; }
            if (args.Y < 0.7 && args.Y >= 0.6) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 10, 500); return; }
            if (args.Y < 0.4 && args.Y >= 0.3) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, -10, 500); return; }
            if (args.Y < 0.3 && args.Y >= 0.2) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, -25, 500); return; }
            if (args.Y < 0.2 && args.Y >= 0.1) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, -50, 500); return; }
            if (args.Y < 0.1 && args.Y >= 0.0) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, -75, 500); return; }

The best part of this code is that I don’t have to determine “if” the joystick was moved. The helper take care of all that for me.

I hope you find this useful. It’s already saved me time and I hope it does for you too.