Updated version of RenBuggy Servo that can accept instructions based on time or distance.

Dependencies:   PinDetect mbed

Fork of RenBuggyServo by Renishaw

Car.cpp

Committer:
Markatron
Date:
2014-03-10
Revision:
1:3e1290de9c8d
Parent:
0:d388aed56112
Child:
2:287a808baad7

File content as of revision 1:3e1290de9c8d:

/*******************************************************************************
* 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"

/*
** Constructs the car with PwmOut objects for servo and motor.
** @params servoPin: This is the pin used for pwm output for driving the servo.
** @params motorPin: This is the pin used for pwm output for driving the motor.
*/
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;
}

/*
** Deconstructs the car.
*/
Car::~Car() {
}

/* 
**
*/
void Car::setSpeed(int speed_us) {
    m_speed = speed_us;
}

/*
** This function is for use in conjuction with
** an encoder, and makes the car move a specified
** distance. 
** @params distance: The distance the car should 
** move, in cm.
*/
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();
    
}   

/*
** Start the car moving with a default speed.
*/ 
void Car::forwards() {
    m_motor.pulsewidth_us(m_speed);
}

/*
** Stops the motor.
*/
void Car::stop() {
    m_motor.pulsewidth_us(0);
}

/*
** Set the direction the car is facing.
** @params degrees: The degrees of the angle, where -45 is full 
**                  left, 0 is centre and +45 is full right.
*/
void Car::setDirection(int degrees) {
    float angleOffset = m_servoRange * (m_servoDegrees / degrees);
    m_servo.pulsewidth_us(1500 + angleOffset);
}

/*
** Configures the pulsewidth and perion for the servon, in microseconds.
** @params pulsewidth_us: The pwm pulsewidth for the servo, in mircoseconds.
** @params period_ms: The pwm period for the servo, in mircoseconds.
** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms).
** @params degrees: The angle to full right/left turn of the servo from centre (0).
*/
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;
}

/* 
** Configures the pulsewidth and period for the servo, in milliseconds.
** @params pulsewidth_ms: The pwm pulsewidth for the servo, in milliseconds.
** @params period_ms: The pwm period for the servo, in milliseconds.
** @params range: The pulsewidth range to full left/right turn of the servo from centre (1.5ms)
** @params degrees: The angle to full right/left turn of the servo from centre (0).
*/
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;
}

/*
** Configures the pulsewidth and period for the motor, in microseconds.
** @params pulsewidth_us: The pwm pulsewidth for the motor, in mircoseconds.
** @params period_us: The pwm period for the motor, in microseconds.
*/
void Car::configureMotor_us(int pulsewidth_us, int period_us) {
    m_motor.pulsewidth_us(pulsewidth_us);
    m_motor.period_us(period_us);
}

/*
** Configures the pulsewidth and period for the motor, in milliseconds.
** @params pulsewidth_ms: The pwm pulsewidth for the motor, in milliseconds.
** @params period_ms: The pwm period for the motor, in milliseconds.
*/
void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) {
    m_motor.pulsewidth_ms(pulsewidth_ms);
    m_motor.period_ms(period_ms);
}

/*
** Provides information required to make use of an encoder for 
** specifying distance.
** @params countsPerRevolution: The number of counts the encoder
** makes in one full cycle of the wheel.
** @params wheelCircumference: The circumference of the wheel being
** read by the encoder.
*/
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