Updated version of RenBuggy Servo that can accept instructions based on time or distance.
Fork of RenBuggyServo by
Car.cpp
- Committer:
- Markatron
- Date:
- 2014-03-10
- Revision:
- 2:287a808baad7
- Parent:
- 1:3e1290de9c8d
- Child:
- 3:01bc89d7ae8e
File content as of revision 2:287a808baad7:
/******************************************************************************* * RenBED Car used to drive RenBuggy with servo, motor and encoder(optional) * * Copyright (c) 2014 Mark Jones * * * * Permission is hereby granted, free of charge, to any person obtaining a copy * * of this software and associated documentation files (the "Software"), to deal* * in the Software without restriction, including without limitation the rights * * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * * copies of the Software, and to permit persons to whom the Software is * * furnished to do so, subject to the following conditions: * * * * The above copyright notice and this permission notice shall be included in * * all copies or substantial portions of the Software. * * * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,* * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * * THE SOFTWARE. * * * * Car.cpp * * * * V1.0 05/03/2014 Mark Jones * *******************************************************************************/ #ifndef CAR_C #define CAR_C #include "Car.h" #include "mbed.h" Car::Car(PinName servoPin, PinName motorPin) : m_servo(servoPin), m_motor(motorPin) { m_speed = 15000; } Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference) : m_servo(servoPin), m_motor(motorPin) { configureEncoder(countsPerRevolution, wheelCircumference); m_speed = 15000; } Car::~Car() { } void Car::setSpeed(int speed_us) { m_speed = speed_us; } void Car::forwards(float distance) { int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference)); // Tell encoder to keep reading, and have motor keep going forward // until the specified number of counts (countsForward) has been read // (e.g. countsForward = 10 // while(encoderValue <= 10) // { // m_motor.pulsewidth(m_speed); // } // stop(); } void Car::forwards() { m_motor.pulsewidth_us(m_speed); } void Car::stop() { m_motor.pulsewidth_us(0); } void Car::setDirection(int degrees) { float angleOffset = m_servoRange * (m_servoDegrees / degrees); m_servo.pulsewidth_us(1500 + angleOffset); } void Car::configureServo_us(int pulsewidth_us, int period_us, int range, float degrees) { m_servo.pulsewidth_us(pulsewidth_us); m_servo.period_us(period_us); m_servoRange = range; m_servoDegrees = degrees; } void Car::configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees) { m_servo.pulsewidth_ms(pulsewidth_ms); m_servo.period_ms(period_ms); m_servoRange = range; m_servoDegrees = degrees; } void Car::configureMotor_us(int pulsewidth_us, int period_us) { m_motor.pulsewidth_us(pulsewidth_us); m_motor.period_us(period_us); } void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) { m_motor.pulsewidth_ms(pulsewidth_ms); m_motor.period_ms(period_ms); } void Car::configureEncoder(int countsPerRevolution, float wheelCircumference) { m_countsPerRevolution = countsPerRevolution; m_wheelCircumference = wheelCircumference; } /* ** Takes a distance specified by user, and calculates how far will ** be travelled in 1 second. It then calculates the time that will ** be required to travel the specified distance from this result. ** @params distance: The distance that needs to be converted into ** a time value. */ /* float Car::distanceToTimeConverter(float distance) { float singleMovement = sqrt(distance); // sqaure root of distance. float time = 1; time = time + (distance / singleMovement); return time; } */ #endif // CAR_C