SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Hardwares/Motor.cpp

Committer:
Bobymicjohn
Date:
2017-02-07
Revision:
11:676ea42afd56
Parent:
9:b72e18f80f49
Child:
13:7dcb1642ef99

File content as of revision 11:676ea42afd56:

#include "Motor.h"
#include "mbed.h"

#include "Core.h"

#include "PinAssignment.h"

Motor::Motor(SW::Core& core) :
    m_core(core),
    m_dirL(DigitalOut(MC_DIR_L, 1)),
    m_dirR(DigitalOut(MC_DIR_R, 1)),
    m_pwmL(PwmOut(MC_SPEED_L)),
    m_pwmR(PwmOut(MC_SPEED_R))
    
{
    
}


void Motor::Update(float deltaTime)
{
    
}

void Motor::setLeftSpeed(float speed)
{
    if(speed < -1.0f || speed > 1.0f)
        return;
    
    m_pwmL.period_us(60);
    
    setLeftDirection(speed < 0 ? MDIR_Backward : MDIR_Forward);
    speed = speed < 0 ? -speed : speed;

    m_pwmL = speed;
}

void Motor::setRightSpeed(float speed)
{
    if(speed < -1.0f || speed > 1.0f)
        return;
    
    m_pwmR.period_us(60);
    
    setRightDirection(speed < 0 ? MDIR_Backward : MDIR_Forward);
    speed = speed < 0 ? -speed : speed;

    m_pwmR = speed;
}    

void Motor::setSpeeds(float leftSpeed, float rightSpeed)
{
    setLeftSpeed(leftSpeed);
    setRightSpeed(rightSpeed);    
}

    
void Motor::setLeftDirection(MotorDir dir)
{
    m_dirL = static_cast<int>(dir);
}
    
void Motor::setRightDirection(MotorDir dir)
{
    m_dirR = static_cast<int>(dir);
}
    
void Motor::setDirections(MotorDir dirL, MotorDir dirR)
{
    setLeftDirection(dirL);
    setRightDirection(dirR);
}