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

Dependencies:   PinDetect mbed

Fork of RenBuggyServo by Renishaw

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Car.cpp Source File

Car.cpp

00001 /*******************************************************************************
00002 * RenBED Car used to drive RenBuggy with servo, motor and encoder(optional)    *
00003 * Copyright (c) 2014 Mark Jones                                                *
00004 *                                                                              *
00005 * Permission is hereby granted, free of charge, to any person obtaining a copy *
00006 * of this software and associated documentation files (the "Software"), to deal*
00007 * in the Software without restriction, including without limitation the rights *
00008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell    *
00009 * copies of the Software, and to permit persons to whom the Software is        *
00010 * furnished to do so, subject to the following conditions:                     *
00011 *                                                                              *
00012 * The above copyright notice and this permission notice shall be included in   *
00013 * all copies or substantial portions of the Software.                          *
00014 *                                                                              *
00015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR   *
00016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,     *
00017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  *
00018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER       *
00019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,*
00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN    *
00021 * THE SOFTWARE.                                                                *
00022 *                                                                              *
00023 * Car.cpp                                                                      *
00024 *                                                                              *
00025 * V1.1 31/03/2014                                          Mark Jones          *
00026 *******************************************************************************/
00027 
00028 #ifndef CAR_C
00029 #define CAR_C
00030 
00031 #include "Car.h"
00032 #include "mbed.h"
00033 
00034 const int SERVO_PWM = 1500;             // 1500 = centre.
00035 const int SERVO_PWM_PERIOD = 2000;  
00036 const int SERVO_PWM_RANGE = 500;        // + or - 500 microseconds.
00037 const float SERVO_DEGREES_RANGE = 45.0; // + or - from centre is full right/left.
00038     
00039 const int MOTOR_PWM = 20000;
00040 const int MOTOR_PERIOD = 20000;
00041 
00042 Car::Car(PinName servoPin, PinName motorPin) 
00043  : m_servo(servoPin), m_motor(motorPin) {
00044     
00045     m_speed = 15000;
00046     m_countsPerRevolution = 0;
00047     m_wheelCircumference = 0;
00048     
00049     configureServo_us(SERVO_PWM, SERVO_PWM_PERIOD, 
00050     SERVO_PWM_RANGE, SERVO_DEGREES_RANGE);
00051     
00052     configureMotor_us(MOTOR_PWM, MOTOR_PERIOD);
00053 }
00054 
00055 Car::Car(PinName servoPin, PinName motorPin, int countsPerRevolution, float wheelCircumference, PinName sensorPin)
00056  : m_servo(servoPin), m_motor(motorPin), m_sensor(sensorPin) {
00057     configureEncoder(countsPerRevolution, wheelCircumference);
00058     
00059     m_speed = 15000;
00060     setDirection(0);
00061     
00062     configureServo_us(SERVO_PWM, SERVO_PWM_PERIOD, 
00063     SERVO_PWM_RANGE, SERVO_DEGREES_RANGE);
00064     
00065     configureMotor_us(MOTOR_PWM, MOTOR_PERIOD);
00066     
00067     m_encoderCount = 0;
00068     m_sensor.setSampleFrequency(1000);
00069     m_sensor.setSamplesTillHeld(5);
00070 
00071     Car* basePointer = dynamic_cast<Car*>(this);    
00072     m_sensor.attach_deasserted_held(basePointer, &Car::updateEncoderCount);
00073 }
00074 
00075 Car::~Car() {
00076 }
00077 
00078 void Car::setSpeed(int speed_us) {
00079     m_speed = speed_us;
00080 }
00081 
00082 void Car::updateEncoderCount() {
00083     m_encoderCount++;
00084 }
00085 
00086 void Car::forwards_measured(float distance) {  
00087     
00088     int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference));
00089     
00090     m_encoderCount = 0;
00091     bool isMoving = true;
00092     m_motor.pulsewidth_us(m_speed);
00093     
00094     while(isMoving) {
00095         wait(0.2); // <<-- for some unknown reason, this requires a delay to work :-S
00096         if(countsForward < m_encoderCount)
00097         {
00098             isMoving = false;
00099         }
00100     }
00101     // When it's finished, stop the buggy.
00102     stop();
00103     
00104     return;
00105 }   
00106 
00107 void Car::forwards_timed(float Time) {
00108     m_motor.pulsewidth_us(m_speed);
00109     wait(Time);
00110     stop();
00111 }
00112 
00113 
00114 void Car::forwards(float distance) { //Temporary extra one so that their current instructions still work.
00115     int countsForward = (int)(distance * (m_countsPerRevolution / m_wheelCircumference));
00116     
00117     m_encoderCount = 0;
00118     bool isMoving = true;
00119     m_motor.pulsewidth_us(m_speed);
00120     
00121     while(isMoving) {
00122         wait(0.2); // <<-- for some unknown reason, this requires a delay to work :-S
00123         if(countsForward < m_encoderCount)
00124         {
00125             isMoving = false;
00126         }
00127     }
00128     // When it's finished, stop the buggy.
00129     stop();
00130     
00131     return;
00132 }
00133 
00134 void Car::stop() {
00135     m_motor.pulsewidth_us(0);
00136 }
00137 
00138 void Car::setDirection(int degrees) {
00139     float angleOffset = m_servoRange * (m_servoDegrees / degrees);
00140     m_servo.pulsewidth_us(1500 + angleOffset);
00141 }
00142 
00143 void Car::configureServo_us(int pulsewidth_us, int period_us, int range, float degrees) {
00144     m_servo.pulsewidth_us(pulsewidth_us);
00145     m_servo.period_us(period_us);
00146     m_servoRange = range;
00147     m_servoDegrees = degrees;
00148 }
00149 
00150 void Car::configureServo_ms(int pulsewidth_ms, int period_ms, int range, float degrees) {
00151     m_servo.pulsewidth_ms(pulsewidth_ms);
00152     m_servo.period_ms(period_ms);
00153     m_servoRange = range;
00154     m_servoDegrees = degrees;
00155 }
00156 
00157 void Car::configureMotor_us(int pulsewidth_us, int period_us) {
00158     m_motor.pulsewidth_us(pulsewidth_us);
00159     m_motor.period_us(period_us);
00160 }
00161 
00162 void Car::configureMotor_ms(int pulsewidth_ms, int period_ms) {
00163     m_motor.pulsewidth_ms(pulsewidth_ms);
00164     m_motor.period_ms(period_ms);
00165 }
00166 
00167 void Car::configureEncoder(int countsPerRevolution, float wheelCircumference) {
00168     m_countsPerRevolution = countsPerRevolution;
00169     m_wheelCircumference = wheelCircumference; 
00170 }
00171 
00172 #endif // CAR_C