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 Mar 04 17:47:27 2019 +0000
Revision:
14:6bcec5ac21ca
Parent:
12:a25bdf135348
'Brute' Locomotive Touch Screen Controller - Driver's Controls; Always a 'Work In Progress', snapshot March 2019

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 12:a25bdf135348 1 #include "mbed.h"
JonFreeman 12:a25bdf135348 2 #include "Electric_Loco.h"
JonFreeman 12:a25bdf135348 3 #include "AsyncSerial.hpp"
JonFreeman 12:a25bdf135348 4
JonFreeman 12:a25bdf135348 5 extern Serial pc;
JonFreeman 12:a25bdf135348 6 extern AsyncSerial com2escs;
JonFreeman 12:a25bdf135348 7 extern error_handling_Jan_2019 Controller_Error ; // Provides array usable to store error codes.
JonFreeman 12:a25bdf135348 8 extern volatile bool trigger_32ms;
JonFreeman 12:a25bdf135348 9 extern command_line_interpreter_core pcli, ploco; // pcli handles comms with pc, ploco handles comms with STM3_ESC boards
JonFreeman 12:a25bdf135348 10
JonFreeman 12:a25bdf135348 11 void STM3_ESC_Interface::message (int board, char * msg) // Send message to one individual STM3_ESC
JonFreeman 12:a25bdf135348 12 {
JonFreeman 12:a25bdf135348 13 if (!(isdigit(board))) {
JonFreeman 12:a25bdf135348 14 pc.printf ("Error in STM3_ESC_Interface::message, '%c' not valid board ID\r\n");
JonFreeman 12:a25bdf135348 15 Controller_Error.set (FAULT_BOARD_ID_IN_MSG, -1);
JonFreeman 12:a25bdf135348 16 return ;
JonFreeman 12:a25bdf135348 17 }
JonFreeman 12:a25bdf135348 18 com2escs.putc (board);
JonFreeman 12:a25bdf135348 19 message (msg);
JonFreeman 12:a25bdf135348 20 }
JonFreeman 12:a25bdf135348 21
JonFreeman 12:a25bdf135348 22 void STM3_ESC_Interface::message (char * msg) // Broadcast message to all STM3_ESCs
JonFreeman 12:a25bdf135348 23 {
JonFreeman 12:a25bdf135348 24 com2escs.printf (msg);
JonFreeman 12:a25bdf135348 25 }
JonFreeman 12:a25bdf135348 26
JonFreeman 12:a25bdf135348 27 void STM3_ESC_Interface::set_V_limit (double p) // Sets max motor voltage
JonFreeman 12:a25bdf135348 28 {
JonFreeman 12:a25bdf135348 29 if (p < 0.0)
JonFreeman 12:a25bdf135348 30 p = 0.0;
JonFreeman 12:a25bdf135348 31 if (p > 1.0)
JonFreeman 12:a25bdf135348 32 p = 1.0;
JonFreeman 12:a25bdf135348 33 last_V = p;
JonFreeman 12:a25bdf135348 34 com2escs.printf ("v%d\r", (int)(last_V * 99.0));
JonFreeman 12:a25bdf135348 35 }
JonFreeman 12:a25bdf135348 36
JonFreeman 12:a25bdf135348 37 void STM3_ESC_Interface::set_I_limit (double p) // Sets max motor current
JonFreeman 12:a25bdf135348 38 {
JonFreeman 12:a25bdf135348 39 if (p < 0.0)
JonFreeman 12:a25bdf135348 40 p = 0.0;
JonFreeman 12:a25bdf135348 41 if (p > 1.0)
JonFreeman 12:a25bdf135348 42 p = 1.0;
JonFreeman 12:a25bdf135348 43 last_I = p; // New 30/4/2018 ; no use for this yet, included to be consistent with V
JonFreeman 12:a25bdf135348 44 com2escs.printf ("i%d\r", (int)(last_I * 99.0));
JonFreeman 12:a25bdf135348 45 }
JonFreeman 12:a25bdf135348 46
JonFreeman 12:a25bdf135348 47 void STM3_ESC_Interface::get_boards_list (int * dest) {
JonFreeman 12:a25bdf135348 48 for (int i = 0; i < MAX_ESCS; i++)
JonFreeman 12:a25bdf135348 49 dest[i] = board_IDs[i];
JonFreeman 12:a25bdf135348 50 }
JonFreeman 12:a25bdf135348 51
JonFreeman 12:a25bdf135348 52 void STM3_ESC_Interface::search_for_escs () { // Seek out all STM3_ESC boards connected to TS controller
JonFreeman 12:a25bdf135348 53 char whotxt[] = "0who\r\0";
JonFreeman 12:a25bdf135348 54 for (int i = 0; i < MAX_ESCS; i++)
JonFreeman 12:a25bdf135348 55 board_IDs[i] = 0;
JonFreeman 12:a25bdf135348 56 board_count = 0;
JonFreeman 12:a25bdf135348 57 pc.printf ("Searching for connected STM3_ESC boards - ");
JonFreeman 12:a25bdf135348 58 while (!trigger_32ms)
JonFreeman 12:a25bdf135348 59 ploco.sniff (); // Allow any previous STM3_ESC comms opportunity to complete
JonFreeman 12:a25bdf135348 60 while (whotxt[0] <= '9') { // Sniff out system, discover motor controllers connected
JonFreeman 12:a25bdf135348 61 trigger_32ms = false;
JonFreeman 12:a25bdf135348 62 message (whotxt); // Issue '0who' etc
JonFreeman 12:a25bdf135348 63 whotxt[0]++;
JonFreeman 12:a25bdf135348 64 while (!trigger_32ms) { // Give time for STM3_ESC board to respond
JonFreeman 12:a25bdf135348 65 pcli.sniff (); // Check commands from pc also
JonFreeman 12:a25bdf135348 66 ploco.sniff (); // This is where responses to 'who' get picked up and dealt with
JonFreeman 12:a25bdf135348 67 }
JonFreeman 12:a25bdf135348 68 } // Completed quick sniff to identify all connected STM3 ESC boards
JonFreeman 12:a25bdf135348 69 if (board_count) {
JonFreeman 12:a25bdf135348 70 pc.printf ("Found %d boards, IDs ", board_count);
JonFreeman 12:a25bdf135348 71 for (int i = 0; i < board_count; i++)
JonFreeman 12:a25bdf135348 72 pc.printf ("%c ", board_IDs[i]);
JonFreeman 12:a25bdf135348 73 pc.printf ("\r\n");
JonFreeman 12:a25bdf135348 74 }
JonFreeman 12:a25bdf135348 75 else
JonFreeman 12:a25bdf135348 76 pc.printf ("None found\r\n");
JonFreeman 12:a25bdf135348 77 }
JonFreeman 12:a25bdf135348 78
JonFreeman 12:a25bdf135348 79 void STM3_ESC_Interface::set_board_ID (int a) { // called in response to 'whon' coming back from a STM3_ESC
JonFreeman 12:a25bdf135348 80 board_count = 0; // reset and recalculate board_count
JonFreeman 12:a25bdf135348 81 while (board_IDs[board_count]) {
JonFreeman 12:a25bdf135348 82 if (board_IDs[board_count++] == a) {
JonFreeman 12:a25bdf135348 83 // pc.printf ("set_board_ID %c already listed\r\n", a);
JonFreeman 12:a25bdf135348 84 return;
JonFreeman 12:a25bdf135348 85 }
JonFreeman 12:a25bdf135348 86 }
JonFreeman 12:a25bdf135348 87 board_IDs[board_count++] = a;
JonFreeman 12:a25bdf135348 88 }
JonFreeman 12:a25bdf135348 89
JonFreeman 12:a25bdf135348 90 bool STM3_ESC_Interface::request_mph () { // Issue "'n'mph\r" to BLDC board to request RPM 22/06/2018
JonFreeman 12:a25bdf135348 91 if (board_IDs[0] == 0)
JonFreeman 12:a25bdf135348 92 return false; // No boards identified
JonFreeman 12:a25bdf135348 93 if (board_IDs[reqno] == 0)
JonFreeman 12:a25bdf135348 94 reqno = 0;
JonFreeman 12:a25bdf135348 95 message (board_IDs[reqno++], "mph\r");
JonFreeman 12:a25bdf135348 96 return true;
JonFreeman 12:a25bdf135348 97 }
JonFreeman 12:a25bdf135348 98
JonFreeman 12:a25bdf135348 99 //void STM3_ESC_Interface::mph_update(struct parameters & a) { // Puts new readings into mem 22/06/2018
JonFreeman 12:a25bdf135348 100 void STM3_ESC_Interface::mph_update(double mph_reading) { // Puts new readings into mem 22/06/2018
JonFreeman 12:a25bdf135348 101 static int identified_board = 0;
JonFreeman 12:a25bdf135348 102 esc_speeds[identified_board++] = mph_reading; // A single mph reading returned from one STM3ESC. a.dbl[0] gives esc number
JonFreeman 12:a25bdf135348 103 if (identified_board >= board_count)
JonFreeman 12:a25bdf135348 104 identified_board = 0; // Circular buffer for number of STM3ESC boards sniffed and found
JonFreeman 12:a25bdf135348 105 double temp = 0.0;
JonFreeman 12:a25bdf135348 106 for (int j = 0; j < board_count; j++)
JonFreeman 12:a25bdf135348 107 temp += esc_speeds[j];
JonFreeman 12:a25bdf135348 108 if (board_count < 1) // Avoid possible DIV0 error
JonFreeman 12:a25bdf135348 109 mph = 0.0;
JonFreeman 12:a25bdf135348 110 else
JonFreeman 12:a25bdf135348 111 mph = temp / board_count; // Updated average of most recent mph readings recieved
JonFreeman 12:a25bdf135348 112 }
JonFreeman 12:a25bdf135348 113