{"id":339,"date":"2013-05-16T22:12:19","date_gmt":"2013-05-16T22:12:19","guid":{"rendered":"http:\/\/blog.daft-ideas.co.uk\/?p=339"},"modified":"2013-05-17T08:16:36","modified_gmt":"2013-05-17T08:16:36","slug":"and-again-with-the-coding","status":"publish","type":"post","link":"https:\/\/blog.daft-ideas.co.uk\/2013\/05\/16\/and-again-with-the-coding\/","title":{"rendered":"And again with the coding…"},"content":{"rendered":"
Here’s a little Arduino code I’ve cooked up for the purposes of providing a nice crank signal to the FPGA-based ECU, for the purposes of bench testing the output. It’s a simulation of a nicely cleaned up, digital signal generated by a 36-1 toothed crank wheel on your typical Ford engine.<\/p>\n
Essentially, a little throttle position sensor (read, potentiometer) is hooked up to adc pin 5. This is purely to give me something to twiddle to see if the FPGA is reacting quickly enough to the change in frequency (Not that I have any fear of this, really- damn thing is pretty fast- but alas, I must have something to fiddle with while testing, or it’s no fun!).<\/p>\n
The LED flashing part is purely for visual effect; an ‘is it working’ before I had chance to hook it up to an oscilloscope for testing.<\/p>\n
The code is simple enough, my written code is starting to look more user-friendly, and- importantly- it works. Onward with the FPGA testing, then?<\/p>\n
Maybe after my exams; this was put together in my ‘evening off’. (Oh, ninjaedit- it only ‘works’ when debug = 1)<\/p>\n
\r\n\/\/-------------------------------------------------------------------------------\r\n\r\n#include <Wire.h>\r\n\r\n\/\/-----------------------------------\r\n\/\/ Calibration Values for TPS\r\nfloat minimumReading = 48;\r\nfloat maximumReading = 919;\r\n\/\/-----------------------------------\r\n\r\n\/\/Setttings for TPS\r\nint analogPin = 5; \/\/ Throttle Position Sensor Wiper Pin\r\nint throttleRead= 0; \/\/ variable to store the value read\r\nint throttleCorrected= 0; \/\/ variable to store the corrected value\r\n\r\n\/\/LED flashy bit setup\r\nconst int ledPin = 13; \/\/ The number of the onboard LED pin\r\nint ledState = LOW; \/\/ Used to store the LED state\r\n\r\n\/\/Variables for Pulse Generator\r\nlong pulseLength = 0; \/\/ Variable to store the generated pulse length\r\nlong gapLength = 0;\r\nint toggle = 1; \/\/ Variable to act as a toggle\r\nlong lastMicros = 0; \/\/ Variable to act as timer function\r\nint count = 1; \/\/ Variable to store the current tooth count\r\nint calibrate = 92.59259259259; \/\/ Calibration for top speed = appx. 9k rpm\r\n\r\nint rpm1 = 1; \/\/ Variable to store the RPM number\r\nint debug = 1; \/\/ Debug setting- changes output from RPM & pulse length to counter & gap indicator\r\n\r\nvoid setup()\r\n{\r\nSerial.begin(9600); \/\/ Setup serial\r\npinMode(ledPin, OUTPUT);\r\n}\r\n\r\nvoid loop()\r\n{\r\ntpsRead(); \/\/ Run TPS Read loop\r\npulseGenerate();\r\n}\r\n\r\nvoid tpsRead()\r\n{\r\nthrottleRead = analogRead(analogPin); \/\/ Read the input pin\r\nthrottleCorrected = ((throttleRead-minimumReading)\/maximumReading)*100; \/\/ Subtract minimum from TPS. Calculate percentage of max\r\n\r\nif (throttleCorrected<1)\r\n{\r\nthrottleCorrected=1;\r\n}\r\nif (debug == 0)\r\n{\r\nrpm1 = 60000000\/(pulseLength*36);\r\nSerial.print(\"Simulated RPM :\");\r\nSerial.print(rpm1);\r\nSerial.print(\"RPM; Pulse Width :\");\r\nSerial.print(pulseLength);\r\nSerial.println(\"uS\");\r\n}\r\n}\r\nvoid pulseGenerate()\r\n{\r\npulseLength = (throttleCorrected*calibrate); \/\/ Calculate length of 'high' pulse (in microseconds)\r\ngapLength = pulseLength*3;\r\n\r\nif ( (micros() > (lastMicros + pulseLength)) && (toggle==HIGH) && (count <= 35) )\r\n{\r\nlastMicros = micros();\r\ntoggle = LOW;\r\n}\r\nif ( (micros() > (lastMicros + pulseLength)) && (toggle==LOW) && (count < 35) )\r\n{\r\nlastMicros = micros();\r\ntoggle = HIGH;\r\ncount += 1;\r\nif (debug == 1)\r\n{\r\nSerial.print (count);\r\nSerial.print (\",\");\r\n}\r\n}\r\nif ( (micros() > (lastMicros + gapLength)) && (toggle==LOW) && (count == 35) )\r\n{\r\nlastMicros = micros();\r\ntoggle = HIGH;\r\ncount = 1;\r\nif (debug == 1)\r\n{\r\nSerial.println (\"GAP\");\r\nSerial.print(count);\r\nSerial.print (\",\");\r\n}\r\n}\r\ndigitalWrite(ledPin,toggle);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Here’s a little Arduino code I’ve cooked up for the purposes of providing a nice crank signal to the FPGA-based ECU, for the purposes of bench testing the output. It’s a simulation of a nicely cleaned up, digital signal generated by a 36-1 toothed crank wheel on your typical Ford… Continue reading