Touch screen drivers control dashboard for miniature locomotive. Features meters for speed, volts, power. Switches for lights, horns. Drives multiple STM3_ESC brushless motor controllers for complete brushless loco system as used in "The Brute" - www.jons-workshop.com

Dependencies:   TS_DISCO_F746NG mbed Servo LCD_DISCO_F746NG BSP_DISCO_F746NG QSPI_DISCO_F746NG AsyncSerial FastPWM

Committer:
JonFreeman
Date:
Mon Apr 09 07:51:37 2018 +0000
Revision:
4:67478861c670
Child:
5:21a8ac83142c
First edit of code from 2017 for new Twin BLDC controller boards

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 4:67478861c670 1 #include "mbed.h"
JonFreeman 4:67478861c670 2 /* Updated 12 Nov 2017
JonFreeman 4:67478861c670 3 Jon Freeman
JonFreeman 4:67478861c670 4
JonFreeman 4:67478861c670 5 5" and 7.25" gauge Electric Locomotive Controller - ST DISCO-F746NG
JonFreeman 4:67478861c670 6 Uses built in display and touch screen.
JonFreeman 4:67478861c670 7
JonFreeman 4:67478861c670 8 Display shows 'analogue' moving coil meter movements for :
JonFreeman 4:67478861c670 9 Locomotive speed Miles per Hour
JonFreeman 4:67478861c670 10 System voltage (range 20v - 90v or thereabouts)
JonFreeman 4:67478861c670 11 Power Watts delivered to drive motors.
JonFreeman 4:67478861c670 12
JonFreeman 4:67478861c670 13 Touch screen has three 'buttons', these are currently unused, and are where the meter movements show.
JonFreeman 4:67478861c670 14 Idea is to use two for two horns,
JonFreeman 4:67478861c670 15
JonFreeman 4:67478861c670 16 Display has 'slider' touch control. This drives the loco.
JonFreeman 4:67478861c670 17 Control in central position when not driving or drfting.
JonFreeman 4:67478861c670 18 Moving towards bottom of screen applies regenerative braking - move further down applies harder braking.
JonFreeman 4:67478861c670 19 Moving towards top of screen powers drive motors, move further up applies more torque (current controller implemented)
JonFreeman 4:67478861c670 20 Take finger off and control drifts down to central 'neutral' position.
JonFreeman 4:67478861c670 21 */
JonFreeman 4:67478861c670 22
JonFreeman 4:67478861c670 23 //#define SDCARD
JonFreeman 4:67478861c670 24
JonFreeman 4:67478861c670 25 #define QSPI
JonFreeman 4:67478861c670 26
JonFreeman 4:67478861c670 27 //#define CANbus
JonFreeman 4:67478861c670 28
JonFreeman 4:67478861c670 29 #define MAX_TOUCHES 6 // Touch screen can decode up to this many simultaneous finger press positions
JonFreeman 4:67478861c670 30 #define NEUTRAL_VAL 150 // Number of pixels
JonFreeman 4:67478861c670 31
JonFreeman 4:67478861c670 32 #define SLIDERX 418 // slider graphic x position
JonFreeman 4:67478861c670 33 #define SLIDERY 2 // slider graphic y position
JonFreeman 4:67478861c670 34 #define SLIDERW 50 // pixel width of slider
JonFreeman 4:67478861c670 35 #define SLIDERH 268 // pixel height of slider
JonFreeman 4:67478861c670 36
JonFreeman 4:67478861c670 37 // To get speedo reading correctly, need to use correct gear ratio and wheel size info
JonFreeman 4:67478861c670 38 //#define BOGIE_5_INCH
JonFreeman 4:67478861c670 39 #define BOGIE_7_and_a_quarter_INCH
JonFreeman 4:67478861c670 40
JonFreeman 4:67478861c670 41 const int
JonFreeman 4:67478861c670 42
JonFreeman 4:67478861c670 43 NUMBER_OF_MOTORS = 4, // 1 to 6 motors
JonFreeman 4:67478861c670 44
JonFreeman 4:67478861c670 45 BUTTON_RAD = (SLIDERW / 2) - 4, // radius of circular 'knob' in slider control
JonFreeman 4:67478861c670 46 MIN_POS = BUTTON_RAD + 5, // top of screen
JonFreeman 4:67478861c670 47 MAX_POS = SLIDERH - (BUTTON_RAD + 1), // bottom of screen
JonFreeman 4:67478861c670 48 CIRC_CTR = SLIDERX + BUTTON_RAD + 4;
JonFreeman 4:67478861c670 49
JonFreeman 4:67478861c670 50 static const double
JonFreeman 4:67478861c670 51 #ifdef BOGIE_7_and_a_quarter_INCH
JonFreeman 4:67478861c670 52 MOTOR_PINION_T = 17.0, // motor pinion teeth, wheel gear teeth and wheel dia required to calculate speed and distance.
JonFreeman 4:67478861c670 53 WHEEL_GEAR_T = 76.0,
JonFreeman 4:67478861c670 54 WHEEL_DIA_MM = 147.0,
JonFreeman 4:67478861c670 55 #endif
JonFreeman 4:67478861c670 56 #ifdef BOGIE_5_INCH
JonFreeman 4:67478861c670 57 MOTOR_PINION_T = 27.0, // motor pinion teeth, wheel gear teeth and wheel dia required to calculate speed and distance.
JonFreeman 4:67478861c670 58 WHEEL_GEAR_T = 85.0,
JonFreeman 4:67478861c670 59 WHEEL_DIA_MM = 98.0,
JonFreeman 4:67478861c670 60 #endif
JonFreeman 4:67478861c670 61 PI = 4.0 * atan(1.0),
JonFreeman 4:67478861c670 62 WHEEL_CIRCUMFERENCE_METRE = PI * WHEEL_DIA_MM / 1000.0,
JonFreeman 4:67478861c670 63 PULSES_PER_WHEEL_REV = 32.0 * WHEEL_GEAR_T / MOTOR_PINION_T,
JonFreeman 4:67478861c670 64 PULSES_PER_METRE = PULSES_PER_WHEEL_REV / WHEEL_CIRCUMFERENCE_METRE,
JonFreeman 4:67478861c670 65 rpm2mph = 60.0 // = Motor Revs per hour;
JonFreeman 4:67478861c670 66 * (MOTOR_PINION_T / WHEEL_GEAR_T) // = Wheel rev per hour
JonFreeman 4:67478861c670 67 * WHEEL_CIRCUMFERENCE_METRE // = metres per hour
JonFreeman 4:67478861c670 68 * 39.37 // = inches per hour
JonFreeman 4:67478861c670 69 / (1760 * 36) // = miles per hour
JonFreeman 4:67478861c670 70 ;
JonFreeman 4:67478861c670 71
JonFreeman 4:67478861c670 72 const double LOCO_HANDBRAKE_ESCAPE_SPEED = 0.5;
JonFreeman 4:67478861c670 73
JonFreeman 4:67478861c670 74 enum {NO_DPS, ONE_DP};
JonFreeman 4:67478861c670 75 // Assign unique number to every button we may use, and keep count of total number of them
JonFreeman 4:67478861c670 76 enum {
JonFreeman 4:67478861c670 77 ENTER, SLIDER, SPEEDO_BUT, VMETER_BUT, AMETER_BUT,
JonFreeman 4:67478861c670 78 NUMOF_BUTTONS} ; // button names
JonFreeman 4:67478861c670 79 enum {
JonFreeman 4:67478861c670 80 STATES, INACTIVE, RUN, NEUTRAL_DRIFT, REGEN_BRAKE, PARK, HANDBRAKE_SLIPPING};
JonFreeman 4:67478861c670 81
JonFreeman 4:67478861c670 82 struct slide { int position; int oldpos; int state; int direction; bool recalc_run; bool handbrake_slipping;
JonFreeman 4:67478861c670 83 double handbrake_effort; double loco_speed; } ;
JonFreeman 4:67478861c670 84 struct point { int x; int y; } ;
JonFreeman 4:67478861c670 85 //struct rect { struct point a, b; } ;
JonFreeman 4:67478861c670 86 struct key { int keynum; int x; int y; bool pressed; } ;
JonFreeman 4:67478861c670 87 struct ky_bd { int count, slider_y; key ky[MAX_TOUCHES + 1]; bool sli; } ;
JonFreeman 4:67478861c670 88