-
Notifications
You must be signed in to change notification settings - Fork 25
Added mapping tables for the intake/exhaust valves #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ricallinson
wants to merge
22
commits into
Tamaren1:main
Choose a base branch
from
ricallinson:hf212cc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−72
Open
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9553b3a
spacing cleanup
85035e3
First pass at rewite.
9ec6aa4
Checked in Arduino IDE
0febcb4
Change value offset to allow different top and bottom values.
75e7cb6
Fixed typo
1b84893
Renamed file
9648b5b
Added maps for intake and exhaust
4b06050
Comments and cleanup
26f3bb3
Comments and variable renaming
b4d3282
Added rotation drift check.
494eb9e
Added volatileto ISR variables
d000464
Initialized global variables
85be8bd
Changed failure and index sizing logic.
30341de
Addressed code comments for bad maths.
d32b27d
Reworked the const vars after feedback from comments.
d2b0800
Update to reduce computation time ISR
a20a930
Update to reduce computation time ISR
83cec56
Fixing comments
7a719e0
Updated README
1df7de8
Merge branch 'main' into hf212cc
8757703
Removed timing code and added a second pulse for rotation detection.
0fe906e
Update comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // An Arduino program for controlling valve timing on a 6.5HP Predator engine from Harbor Freight. | ||
| // | ||
| // License goes here? | ||
| // | ||
| // Wesley Kagan, 2020 | ||
| // Ric Allinson, 2020 | ||
|
|
||
| // Pins assignment. | ||
| const int HALL_MAGNET = 2; | ||
| const int EXHAUST_V = 12; | ||
| const int INTAKE_V = 13; | ||
|
|
||
| // Control constants. | ||
| const int DEG_PER_MAGNET = 6; // Number of degrees for per magnet. | ||
| const int ROTATION_TWO = 760/DEG_PER_MAGNET; // Number of interrupts in the full cycle. | ||
| const int ROTATION_ONE = (760/DEG_PER_MAGNET)-1; // Number of interrupts in a half cycle. | ||
|
|
||
| // Runtime mapping variables. | ||
| bool intakeMap[ROTATION_TWO] = {}; // Intake open/close mapping is equal the total number of interrupts for two rotations. | ||
| bool exhaustMap[ROTATION_TWO] = {}; // Exhaust open/close mapping is equal the total number of interrupts for two rotations. | ||
|
|
||
| // ISR variables. | ||
| volatile int hallCounter = 0; // The number of magnets after the last TDC. | ||
| volatile bool secondRotation = false; // "true" if the cam is on its second rotation. | ||
| volatile bool printLog = false; // Used to stop duplicate values from printing in the loop. | ||
| volatile unsigned long timeGap = 0; // Function level time between interrupts. | ||
| volatile unsigned long lastTimeGap = 0; // Global level time between interrupts. | ||
|
|
||
| void setup() { | ||
| Serial.begin(115200); | ||
| pinMode(HALL_MAGNET, INPUT); | ||
| attachInterrupt(0, magnetDetect, RISING); | ||
| pinMode(INTAKE_V, OUTPUT); | ||
| pinMode(EXHAUST_V, OUTPUT); | ||
| // Load mappings here. | ||
| // intakeMap = ? | ||
| // exhaustMap = ? | ||
| } | ||
|
|
||
| void loop() { | ||
| // This may not print all values. Only the most recent from the last interrupt. | ||
| if(printLog){ | ||
| Serial.print(lastTimeGap); | ||
| Serial.print(hallCounter); | ||
| Serial.print(secondRotation); | ||
| printLog = false; | ||
| } | ||
| } | ||
|
|
||
| // This function will be called about every 15.79ms at the maximum RPM of 3800. | ||
ricallinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| void magnetDetect() { | ||
| // Increment the counter to keep track of the position. | ||
| hallCounter++; | ||
| // Get the time gap between this interrupt and the last. | ||
| timeGap = millis() - lastTimeGap; | ||
ricallinson marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Find the missing tooth for Top Dead Center (TDC). | ||
| if (timeGap >= lastTimeGap * 3 / 2) { | ||
| // On the second rotation reset the hallCounter. | ||
| if (secondRotation) { | ||
| hallCounter = 0; | ||
| } else { | ||
| hallCounter = ROTATION_ONE; // Forcing the counter in case of drift over a rotation. | ||
| } | ||
| // Flip the secondRotation. | ||
| secondRotation = !secondRotation; | ||
| } | ||
|
|
||
| // Store the last time difference so we can use it in the next interrupt. | ||
| lastTimeGap = timeGap; | ||
|
|
||
| // If the hallCounter is greater than the mapping index reset it. | ||
| // This would only happen when the missing tooth was NOT detected. | ||
| if (hallCounter >= ROTATION_TWO) { | ||
| hallCounter = 0; | ||
| } | ||
|
|
||
| // Use the intake/exhaust maps to open or close the valves. | ||
| digitalWrite(INTAKE_V, intakeMap[hallCounter]); | ||
| digitalWrite(EXHAUST_V, exhaustMap[hallCounter]); | ||
|
|
||
| // Tells the loop() that values have changed. | ||
| printLog = true; | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be 720 / DEG__PER_MAGNET. However, a 60-2 wheel has 58 trigger edges over 360 degrees. Shouldn't there 58*2 -> 116 interrupts per cycle, not 120?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I totally messed up that maths. Why is it 60-2? One for the missing tooth and one for zero based array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I made the assumption that he had made a standard 60-2 (60 teeth, 2 missing teeth) wheel. Re-watching the video it almost looks like a 60-1 wheel. Either way, it might be easier to define all of these in terms of teeth:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, rather than teeth I've used STEPS incase there is ever a different way to generate position data. It follows the conventions of stepper motors which implement a similar positioning index system.
I didn't use
constexpras I'm not sure it is a standard for the Arduino?