changes to motor library

Dependents:   pid-car-example

Fork of motor by Lawrence Harlow

motor.cpp

Committer:
lh14g13
Date:
2017-01-11
Branch:
motorupdate
Revision:
24:15c6bbdbb0e4
Parent:
23:96c36a38d83b
Child:
25:255d169a45e1

File content as of revision 24:15c6bbdbb0e4:

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

//adam sucks



//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,float speed)
{   // when cornering left the left motor slows down more than the right hand side
   
   
    bool leftOrRight = false;
    float tune = 1.2;
    if(theta<0)
        {
            leftOrRight=true;
            theta=theta*-1;   
            
        }
        
    if(theta>0.5)
    {
     theta = 0.5;   
        
    }
    float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
    
    if(leftOrRight){
          w1= speed+ deltaW * tune;
          w2= speed -deltaW * tune;
          }
    else{
            w1= speed- deltaW*tune;
            w2= speed +deltaW*tune;
        }
    
        
        
    return;   
}