Now that my car is nearly on the road, after more than a year of working on it,
I am looking again at my CougArd Explorer idea. The basic idea is to give some of the functionality of the rtd explorer program in a small embedded platform. Originally I was going to use a Arduino for this, hence the name. The Arduino has been ditched, but I like the name so am going to keep it. That, and I am too lazy to think of a new name!
So while I am waiting for my
µLCD-32PT(GFX) - 4DGL Display Modules to arrive, I have written some code for it. At this point I want to prove that I can send commands to the controller, receive the data stream and store the values into variables that can be manipulated/displayed. This code should display as text the data from the controller. It compiles o.k., but probably has lots of bugs in it. So here it is, but please note, I am a rubbish programmer. I am however open to lots of suggestions on improving the code.
Code:
#platform "uLCD-32PT_GFX2"
/*---- CougArd Explorer by Greg Fordyce. Version 0.01alpha 05/12/2010 -------
------ 4DGL Workshop 3 Version 3.1.0.4 - Platform uLCD32PT_GFX2 -------
------------------------compiled size 791 bytes of 15360----------------------
**************************** General Information ******************************
This software carries no warranty or guarantee of any kind! Use at
your own risk, and I make no claims as to its suitability for a particular
function. Prospective users must evaluate the system before using it, and no
liability will be entertained by myself in any shape or form whatsoever.
The software has been produced at low cost for the benefit of
the EV & electronic community. The software is available free via the internet.
Users may modify or adapt the system as they see fit. If you are not fully
competent to work on potentially lethal battery systems and high voltages,
then do not experiment with or use this system. Be aware that vehicle
modifications can lead to invalidated insurance and warranty issues. You the
end user remain fully liable for any modifications made to your vehicle.
/****************************** Description ************************************
This software is to capture the real time data stream from the cougar controller
and display it in a graphical interface to the driver. This first version is a
proof of concept and just displays the info as test after capturing and storing the
data to variables. It compiles o.k. but has not yet been tested on the actual
display. It probably has lots of bugs in it at the moment. More info, see links.
http://ecomodder.com/forum/showthread.php/cougard-explorer-13463.html
http://ecomodder.com/wiki/index.php/ReVolt
/************************* Connection warning ***********************************
The cougar controller uses a serial RS232 port if built to the standard design.
The uLSD-32PT_GFX2 uses 3.3v ttl level serial comms (5 volt tolerent). You must use
a serial to ttl level shifter/converter. Direct connection of the display to the
serial port will likely damage the display.
*********************************************************************************/
/////////// Constants ////////////////////////////////////////////////////////////
/////////// Data /////////////////////////////////////////////////////////////////
#DATA byte rtd_pointer 3,10,17,24,31,39,47,53 // Address locations in cougar data string for values
#END
/////////// Variables ////////////////////////////////////////////////////////////
var i, p, n;
var cougTXbuf[10]; // This will allow us to build strings of up to 20 characters to send commands to the controller
var cougRXbuf[33]; // Data string format from controller
//"TR=xxx CR=xxx CF=xxx PW=xxx HS=xxxx RT=xxxx FB=xx BA=xxx AH=xxx.x\r\n"
var coug_rtd[9]; // Data from controller
// [0] TR 'throttle'
// [1] CR 'current reference'
// [2] CF 'current feedback'
// [3] PW 'PWM'
// [4] HS 'heatsink'
// [5] RT 'raw throttle'
// [6] FB 'fault bits'
// [7] BA 'battery amps'
// [8] AH 'amp hours' note: the 1/10 value is ignored
func rtd_capture() // This function parses the rtd string and assigns
// word values of all integer numbers found in the string
n:=str_Find(&p,"TR"); // Check for valid string from controller
if (n > 0 && n < 3) // "TR" should be found at front of string
i:=0;
while(str_GetW(&p, &coug_rtd[i++]) != 0); // parse cougarRXbuf and store values to coug_rtd
com_Init(cougRXbuf,33,0); // reset RX buffer
else
n:=0; // if we get here there is a problem with the string
endif
endfunc
func main()
com_SetBaud("COM0","1920"); // Cougar comms is 19200 8-n-1
gfx_Cls();
gfx_Set(SCREEN_MODE,LANDSCAPE);
txt_Set(FONT_SIZE, FONT2);
com_Init(cougRXbuf,33,0); // set up a interrupt driven ring buffer for comms
com_TXbuffer(cougTXbuf, 10); // set up the TX buffer
//com_TXbufferHold(ON); // probably not required here
to(cougTXbuf); print("rtd-period 500\r\n"); // Cougar sends rtd string every 500ms
//com_TXbufferHold(OFF) // probably not required here
pause(10); // wait for response from controller
while(com_Count()) putch(serin()); // print echo response from controller to screen
p:= str_Ptr(cougRXbuf);
repeat
while(!com_Full()) continue; // wait till com buffer fills
rtd_capture(); // Check rtd data
if (n != 0) // print rtd variables
i := 0 ;
print("Throttle is ", coug_rtd[i++], "\n");
print("Current Ref ", coug_rtd[i++], "\n");
print("Current Fdbk ", coug_rtd[i++], "\n");
print("PWM ", coug_rtd[i++], "\n");
print("Raw Heatsink ", coug_rtd[i++], "\n");
print("Raw Throttle ", coug_rtd[i++], "\n");
print("Fault bits ", coug_rtd[i++], "\n");
print("Battery Amps ", coug_rtd[i++], "\n");
print("Amp Hours ", coug_rtd[i], "\n");
else
print("ERROR: ", [STR] cougRXbuf, "\n"); // print buffer as diagnostic output
com_Init(cougRXbuf,33,0); // reset RX buffer
endif
// endif
forever
endfunc