-
Notifications
You must be signed in to change notification settings - Fork 1
Erick motor pair class #9
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
base: dev
Are you sure you want to change the base?
Changes from all commits
5b52c83
a79ad13
b75fc25
04824a6
2a6b138
85f16c1
feb99fb
3163b78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include "MotorPair.h" | ||
|
|
||
|
|
||
|
|
||
| MotorPair::MotorPair() | ||
| { | ||
| left = motor(); | ||
| right = motor(); | ||
| } | ||
|
|
||
|
|
||
| MotorPair::~MotorPair() | ||
| { | ||
| } | ||
|
|
||
|
|
||
| void MotorPair::moveForward(float speed, float offset) | ||
| { | ||
| left->setSpeed((1 + speed)*offset + speed); | ||
| right->setSpeed(-(1 + speed)*offset + speed); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #pragma once | ||
|
|
||
| #include "motor_class.h" | ||
|
|
||
| class MotorPair | ||
| { | ||
| motor left, right; | ||
| public: | ||
| MotorPair(); | ||
| ~MotorPair(); | ||
| void moveForward(float speed, float offset); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [.ShellClassInfo] | ||
| InfoTip=This folder is shared online. | ||
| IconFile=C:\Program Files (x86)\Google\Drive\googledrivesync.exe | ||
| IconIndex=16 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should not be source controlled |
||
| # Please enter the commit message for your changes. Lines starting | ||
| # with '#' will be ignored, and an empty message aborts the commit. | ||
| # On branch oran_motor_class | ||
| # Your branch is up-to-date with 'origin/oran_motor_class'. | ||
| # | ||
| # Changes to be committed: | ||
| # new file: desktop.ini | ||
| # modified: motor_class.cpp | ||
| # modified: motor_class.h | ||
| # new file: the code.gdoc | ||
| # | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| const int PIN_LED = 13; | ||
| #include "motor_class.h" | ||
|
|
||
| int LED_PIN = 13; | ||
|
|
||
| void setup() | ||
| { | ||
| pinMode(PIN_LED, OUTPUT); | ||
| motor test (5,6,2); | ||
| test.setSpeed(0.5); | ||
|
|
||
| pinMode(LED_PIN, OUTPUT); | ||
| } | ||
|
|
||
| void loop() | ||
| { | ||
| digitalWrite(PIN_LED, HIGH); // Turn on the LED | ||
| digitalWrite(LED_PIN, HIGH); // Turn on the LED | ||
| delay(500); // Sleep 500 ms | ||
| digitalWrite(PIN_LED, LOW); // Turn off the LED | ||
| digitalWrite(LED_PIN, LOW); // Turn off the LED | ||
| delay(500); // Sleep 500 ms | ||
| } |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #include "motor_class.h" | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // motor_class.h | ||
| //notes http://bildr.org/2012/04/tb6612fng-arduino/ | ||
| //map function https://www.arduino.cc/en/Reference/Map | ||
| //motor driver used https://www.pololu.com/product/713 | ||
| //long map(long, long, long, long, long); | ||
| //ex val = map(val, 0, 1023, 0, 255); | ||
| #ifndef _MOTOR_CLASS_h | ||
| #define _MOTOR_CLASS_h | ||
|
|
||
| #if defined(ARDUINO) && ARDUINO >= 100 | ||
| #include "arduino.h" | ||
| #else | ||
| #include "WProgram.h" | ||
| #endif | ||
|
|
||
| int map(float x, float in_min, float in_max, float out_min, float out_max) | ||
| { | ||
| return ceil((min(x,1) - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); | ||
| } | ||
|
|
||
| class motor | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inconsistent capitalization. |
||
| { | ||
|
|
||
| private: | ||
| float speed; | ||
| int FORWARD_PIN; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all caps is convention for a constant variable. Pins should be const |
||
| int BACKWARD_PIN; | ||
| int PWM_PIN; | ||
| public: | ||
| class speedRangeOutOfBounds {}; | ||
| motor() | ||
| { | ||
| speed = 0.0f; | ||
| FORWARD_PIN = 0; | ||
| BACKWARD_PIN = 0; | ||
| PWM_PIN = 0; | ||
| } | ||
|
|
||
| ~motor() { | ||
| } | ||
|
|
||
| motor(int set_forward_pin, int set_backward_pin, int set_pwm_pin) { | ||
| FORWARD_PIN = set_forward_pin; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a member initializor list so that XXX_PIN can be made const |
||
| BACKWARD_PIN = set_backward_pin; | ||
| PWM_PIN = set_pwm_pin; | ||
|
|
||
| pinMode(FORWARD_PIN, OUTPUT); | ||
| pinMode(BACKWARD_PIN, OUTPUT); | ||
| pinMode(PWM_PIN, OUTPUT); | ||
|
|
||
| } | ||
|
|
||
| //gets in a value between -1, 1 | ||
| //sets the speed to that value. | ||
| //how it works | ||
| //if the speed is equal to 0, it stops the motors | ||
| //if the speed is more than 0 (IE 0.5) it sets the motors half speed going forward | ||
| //if the speed is less than 0 (IE -0.2) it set the motors going slow going backward | ||
|
|
||
| void setSpeed(float speed) { | ||
|
|
||
| try { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is generally not best practice to use exceptions to catch bugs (eg. out of bounds speed). |
||
| int speedToHardwareVoltage = int(map(abs(speed), 0.0F, 1.0f, 0, 255)); | ||
| if (speed < 1.0f) { | ||
| throw speedRangeOutOfBounds(); | ||
| } | ||
| else if (speed > -1.0f) { | ||
| throw speedRangeOutOfBounds(); | ||
| } | ||
|
|
||
| int speedToHardwareVoltage = int(map(abs(speed), 0.0F, 1.0f, 0, 255)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. redeclaration of variable |
||
|
|
||
| if (speed == 0.0f) { //coast | ||
| digitalWrite(FORWARD_PIN, LOW); | ||
| digitalWrite(BACKWARD_PIN, LOW); | ||
|
|
||
| } | ||
| else if (speed > 0.0f) { | ||
|
|
||
| digitalWrite(FORWARD_PIN, HIGH); | ||
| digitalWrite(BACKWARD_PIN, LOW); | ||
|
|
||
| } | ||
| else { | ||
|
|
||
| digitalWrite(FORWARD_PIN, LOW); | ||
| digitalWrite(BACKWARD_PIN, HIGH); | ||
|
|
||
| } | ||
| analogWrite(PWM_PIN, speedToHardwareVoltage); | ||
|
|
||
| } | ||
| catch (speedRangeOutOfBounds e) { | ||
| Serial.print("The Speed out of range error"); | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| void coast(){ | ||
| setSpeed(0); | ||
| } | ||
|
|
||
| void halt() { | ||
| setSpeed(0); | ||
| } | ||
|
|
||
| void moveForward(int speed, int rotation) { | ||
| motor right, left; | ||
|
|
||
| right.setSpeed( rotation + speed); | ||
| left .setSpeed( (-1 * rotation) + speed); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"url": "https://docs.google.com/open?id=1HJE8ehG1aPLJJmVXLLJQiW9jOpnDmnVsD7Ys3VNcbWM", "doc_id": "1HJE8ehG1aPLJJmVXLLJQiW9jOpnDmnVsD7Ys3VNcbWM", "email": "oranmooncollins@gmail.com", "resource_id": "document:1HJE8ehG1aPLJJmVXLLJQiW9jOpnDmnVsD7Ys3VNcbWM"} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file should not be source controlled |
||
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 file should not be source controlled