Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions MotorPair.cpp
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);
}
12 changes: 12 additions & 0 deletions MotorPair.h
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);
};
5 changes: 5 additions & 0 deletions desktop.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[.ShellClassInfo]
Copy link
Member

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

InfoTip=This folder is shared online.
IconFile=C:\Program Files (x86)\Google\Drive\googledrivesync.exe
IconIndex=16

13 changes: 13 additions & 0 deletions jcj1zxha.xx0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

Copy link
Member

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

# 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
#

13 changes: 9 additions & 4 deletions line-follower.ino
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
}
15 changes: 12 additions & 3 deletions line-follower.vcxproj

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions motor_class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "motor_class.h"



118 changes: 118 additions & 0 deletions 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent capitalization.
Either all class names should be capitalized or none of them should.

{

private:
float speed;
int FORWARD_PIN;
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Copy link
Member

Choose a reason for hiding this comment

The 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).
Asserts or something similar (since it is running on a teensy) are much faster and perform the same function and can easily be disabled in a release build.
Exceptions are meant for when the error is out of the programmers control eg. failed to allocate memory, failed to open file, etc.

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));
Copy link
Member

Choose a reason for hiding this comment

The 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
1 change: 1 addition & 0 deletions the code.gdoc
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"}
Copy link
Member

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