changes to motor library

Dependents:   pid-car-example

Fork of motor by Lawrence Harlow

motor.cpp

Committer:
lh14g13
Date:
2016-11-30
Branch:
motorupdate
Revision:
18:e3fd26490f58
Parent:
16:9c5e9306ae10
Child:
19:69c142d81437

File content as of revision 18:e3fd26490f58:

#include "mbed.h"
#include "TFC.h"
#include <math.h> 
DigitalOut myled(LED1);


void TurnOn();
void runMotor();
void DefaultMode();

//Speed control

float setDutyCycle(float dutyC, int w,int targetW);

//Corner Control

void dutyCycleCorner( float speed, float angle);
void corner(float &w1,float &w2,float deltaTheta,int maxspeed);


//Variables
int cornerPwmControl;



// uncomment for testing motor functions.



//need a function for calcu;lating the angle
//need a function for converting w1 to delta. or do i?
 
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------This is for Motor Set up------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------  

 void   runMotor() /// putting it into this mode for some reason makes a bit of a whinning noise (this may simply just be the motor running)
    {
        
        
        TFC_SetMotorPWM(0.4,0.7);
        
        while(1)
        {
            if(TFC_ReadPushButton(0)>0)
            {
                TFC_SetMotorPWM(0.0,0.0);
                
                DefaultMode();
                
            }
        }
        return;
    }
    
void DefaultMode()
    {
        TFC_Init();
        while(1)
        {
            TFC_HBRIDGE_ENABLE;
            if(TFC_ReadPushButton(1)>0)
               {
                    runMotor();
                }
            
            else if(TFC_ReadPushButton(0)>0)
               {
                    //this will be a debug mode
                }
            
               
        }
        TFC_HBRIDGE_DISABLE;
    return;
    }
//-----------------------------------------------------------------------------------------------------    
//------------------------ this is for speed control---------------------------------------------------
//-----------------------------------------------------------------------------------------------------

float setDutyCycle(float dutyC, int w,int targetW)
{
    
    
    if(true)
    {
        if(w<targetW)   
        {
            dutyC+=0.1;        
        
        }  
        
        else
        {
            dutyC-=0.1;   
        }
        
    }
    
    
    else if(false)
    {
        
    }
    
 
    return dutyC;
}
    

 //----------------------------------------------------------------------------------------------------------------------------   
 //------------------------------------------------Cornering Control-----------------------------------------------------------   
 //----------------------------------------------------------------------------------------------------------------------------   
    
    
    
// tThis is a function which works off of the duty cycle. NO SENSOR REQUIREMENT
// CLEAN THIS UP AND UPDATE  FOR SENSORS VERSION WITH THIS CODE
// this function works off the actual value rather than the change in angle. therefore need to have a variabe which stores where the value is.
void dutyCycleCorner( float speed, float theta)
{
        bool leftOrRight;
        if(theta<0)
        {
            leftOrRight=true;
            theta=theta*-1;   
            
        }
 
        float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
        
        //TFC_SetMotorPWM(w2,w1);
        if(leftOrRight){
        TFC_SetMotorPWM(speed+ deltaW,speed- deltaW);
        }
        else{
            TFC_SetMotorPWM(speed- deltaW,speed+ deltaW);
        }
        
        
        
 return;       
}
        
        
void sensorCorner(float &w1,float &w2,float theta,int speed)
{   // when cornering left the left motor slows down more than the right hand side
    // may just replace with ACC and DECC
   
    bool leftOrRight;
    
    if(theta<0)
        {
            leftOrRight=true;
            theta=theta*-1;   
            
        }
 
    float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
    
    w1= speed+ deltaW;
    w2= speed -deltaW;
        
        
    return;   
}