changes to motor library

Dependents:   pid-car-example

Fork of motor by Lawrence Harlow

Committer:
lh14g13
Date:
Wed Nov 30 10:48:45 2016 +0000
Branch:
motorupdate
Revision:
18:e3fd26490f58
Parent:
16:9c5e9306ae10
Child:
19:69c142d81437
halved

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lh14g13 0:b0476dcfa14c 1 #include "mbed.h"
lh14g13 0:b0476dcfa14c 2 #include "TFC.h"
lh14g13 9:22b119eef1de 3 #include <math.h>
lh14g13 0:b0476dcfa14c 4 DigitalOut myled(LED1);
lh14g13 0:b0476dcfa14c 5
lh14g13 7:9aaa4f73bb32 6
lh14g13 0:b0476dcfa14c 7 void TurnOn();
lh14g13 7:9aaa4f73bb32 8 void runMotor();
lh14g13 0:b0476dcfa14c 9 void DefaultMode();
lh14g13 0:b0476dcfa14c 10
lh14g13 0:b0476dcfa14c 11 //Speed control
lh14g13 15:f40e834d063b 12
lh14g13 15:f40e834d063b 13 float setDutyCycle(float dutyC, int w,int targetW);
lh14g13 0:b0476dcfa14c 14
lh14g13 0:b0476dcfa14c 15 //Corner Control
lh14g13 0:b0476dcfa14c 16
lh14g13 11:4a6f97cc1f1e 17 void dutyCycleCorner( float speed, float angle);
lh14g13 11:4a6f97cc1f1e 18 void corner(float &w1,float &w2,float deltaTheta,int maxspeed);
lh14g13 0:b0476dcfa14c 19
lh14g13 15:f40e834d063b 20
lh14g13 16:9c5e9306ae10 21 //Variables
lh14g13 16:9c5e9306ae10 22 int cornerPwmControl;
lh14g13 0:b0476dcfa14c 23
lh14g13 0:b0476dcfa14c 24
lh14g13 0:b0476dcfa14c 25
lh14g13 0:b0476dcfa14c 26 // uncomment for testing motor functions.
lh14g13 0:b0476dcfa14c 27
lh14g13 0:b0476dcfa14c 28
lh14g13 7:9aaa4f73bb32 29
lh14g13 7:9aaa4f73bb32 30 //need a function for calcu;lating the angle
lh14g13 7:9aaa4f73bb32 31 //need a function for converting w1 to delta. or do i?
lh14g13 0:b0476dcfa14c 32
lh14g13 10:f4fc8ccde4ad 33 //----------------------------------------------------------------------------------------------------------------------------------
lh14g13 10:f4fc8ccde4ad 34 //----------------------------------------------This is for Motor Set up------------------------------------------------------------
lh14g13 10:f4fc8ccde4ad 35 //----------------------------------------------------------------------------------------------------------------------------------
lh14g13 0:b0476dcfa14c 36
lh14g13 7:9aaa4f73bb32 37 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)
lh14g13 0:b0476dcfa14c 38 {
lh14g13 0:b0476dcfa14c 39
lh14g13 7:9aaa4f73bb32 40
lh14g13 7:9aaa4f73bb32 41 TFC_SetMotorPWM(0.4,0.7);
lh14g13 0:b0476dcfa14c 42
lh14g13 0:b0476dcfa14c 43 while(1)
lh14g13 0:b0476dcfa14c 44 {
lh14g13 0:b0476dcfa14c 45 if(TFC_ReadPushButton(0)>0)
lh14g13 0:b0476dcfa14c 46 {
lh14g13 0:b0476dcfa14c 47 TFC_SetMotorPWM(0.0,0.0);
lh14g13 7:9aaa4f73bb32 48
lh14g13 0:b0476dcfa14c 49 DefaultMode();
lh14g13 0:b0476dcfa14c 50
lh14g13 0:b0476dcfa14c 51 }
lh14g13 0:b0476dcfa14c 52 }
lh14g13 0:b0476dcfa14c 53 return;
lh14g13 0:b0476dcfa14c 54 }
lh14g13 0:b0476dcfa14c 55
lh14g13 0:b0476dcfa14c 56 void DefaultMode()
lh14g13 0:b0476dcfa14c 57 {
lh14g13 7:9aaa4f73bb32 58 TFC_Init();
lh14g13 0:b0476dcfa14c 59 while(1)
lh14g13 0:b0476dcfa14c 60 {
lh14g13 7:9aaa4f73bb32 61 TFC_HBRIDGE_ENABLE;
lh14g13 0:b0476dcfa14c 62 if(TFC_ReadPushButton(1)>0)
lh14g13 0:b0476dcfa14c 63 {
lh14g13 7:9aaa4f73bb32 64 runMotor();
lh14g13 0:b0476dcfa14c 65 }
lh14g13 0:b0476dcfa14c 66
lh14g13 0:b0476dcfa14c 67 else if(TFC_ReadPushButton(0)>0)
lh14g13 0:b0476dcfa14c 68 {
lh14g13 0:b0476dcfa14c 69 //this will be a debug mode
lh14g13 0:b0476dcfa14c 70 }
lh14g13 0:b0476dcfa14c 71
lh14g13 0:b0476dcfa14c 72
lh14g13 0:b0476dcfa14c 73 }
lh14g13 7:9aaa4f73bb32 74 TFC_HBRIDGE_DISABLE;
lh14g13 0:b0476dcfa14c 75 return;
lh14g13 0:b0476dcfa14c 76 }
lh14g13 10:f4fc8ccde4ad 77 //-----------------------------------------------------------------------------------------------------
lh14g13 10:f4fc8ccde4ad 78 //------------------------ this is for speed control---------------------------------------------------
lh14g13 10:f4fc8ccde4ad 79 //-----------------------------------------------------------------------------------------------------
lh14g13 10:f4fc8ccde4ad 80
lh14g13 15:f40e834d063b 81 float setDutyCycle(float dutyC, int w,int targetW)
lh14g13 15:f40e834d063b 82 {
lh14g13 0:b0476dcfa14c 83
lh14g13 0:b0476dcfa14c 84
lh14g13 15:f40e834d063b 85 if(true)
lh14g13 0:b0476dcfa14c 86 {
lh14g13 15:f40e834d063b 87 if(w<targetW)
lh14g13 15:f40e834d063b 88 {
lh14g13 15:f40e834d063b 89 dutyC+=0.1;
lh14g13 0:b0476dcfa14c 90
lh14g13 15:f40e834d063b 91 }
lh14g13 0:b0476dcfa14c 92
lh14g13 15:f40e834d063b 93 else
lh14g13 15:f40e834d063b 94 {
lh14g13 15:f40e834d063b 95 dutyC-=0.1;
lh14g13 15:f40e834d063b 96 }
lh14g13 0:b0476dcfa14c 97
lh14g13 0:b0476dcfa14c 98 }
lh14g13 0:b0476dcfa14c 99
lh14g13 0:b0476dcfa14c 100
lh14g13 15:f40e834d063b 101 else if(false)
lh14g13 0:b0476dcfa14c 102 {
lh14g13 0:b0476dcfa14c 103
lh14g13 0:b0476dcfa14c 104 }
lh14g13 0:b0476dcfa14c 105
lh14g13 15:f40e834d063b 106
lh14g13 15:f40e834d063b 107 return dutyC;
lh14g13 9:22b119eef1de 108 }
lh14g13 9:22b119eef1de 109
lh14g13 15:f40e834d063b 110
lh14g13 8:4df2a47ab4ee 111 //----------------------------------------------------------------------------------------------------------------------------
lh14g13 8:4df2a47ab4ee 112 //------------------------------------------------Cornering Control-----------------------------------------------------------
lh14g13 8:4df2a47ab4ee 113 //----------------------------------------------------------------------------------------------------------------------------
lh14g13 2:cc8ddc587af7 114
lh14g13 10:f4fc8ccde4ad 115
lh14g13 10:f4fc8ccde4ad 116
lh14g13 13:c43f157a6bac 117 // tThis is a function which works off of the duty cycle. NO SENSOR REQUIREMENT
lh14g13 13:c43f157a6bac 118 // CLEAN THIS UP AND UPDATE FOR SENSORS VERSION WITH THIS CODE
lh14g13 14:bc77edc4adb0 119 // 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.
lh14g13 15:f40e834d063b 120 void dutyCycleCorner( float speed, float theta)
lh14g13 10:f4fc8ccde4ad 121 {
lh14g13 11:4a6f97cc1f1e 122 bool leftOrRight;
lh14g13 11:4a6f97cc1f1e 123 if(theta<0)
lh14g13 11:4a6f97cc1f1e 124 {
lh14g13 14:bc77edc4adb0 125 leftOrRight=true;
lh14g13 14:bc77edc4adb0 126 theta=theta*-1;
lh14g13 11:4a6f97cc1f1e 127
lh14g13 11:4a6f97cc1f1e 128 }
lh14g13 14:bc77edc4adb0 129
lh14g13 18:e3fd26490f58 130 float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
lh14g13 11:4a6f97cc1f1e 131
lh14g13 14:bc77edc4adb0 132 //TFC_SetMotorPWM(w2,w1);
lh14g13 14:bc77edc4adb0 133 if(leftOrRight){
lh14g13 14:bc77edc4adb0 134 TFC_SetMotorPWM(speed+ deltaW,speed- deltaW);
lh14g13 11:4a6f97cc1f1e 135 }
lh14g13 11:4a6f97cc1f1e 136 else{
lh14g13 14:bc77edc4adb0 137 TFC_SetMotorPWM(speed- deltaW,speed+ deltaW);
lh14g13 14:bc77edc4adb0 138 }
lh14g13 10:f4fc8ccde4ad 139
lh14g13 10:f4fc8ccde4ad 140
lh14g13 10:f4fc8ccde4ad 141
lh14g13 10:f4fc8ccde4ad 142 return;
lh14g13 10:f4fc8ccde4ad 143 }
lh14g13 10:f4fc8ccde4ad 144
lh14g13 2:cc8ddc587af7 145
lh14g13 15:f40e834d063b 146 void sensorCorner(float &w1,float &w2,float theta,int speed)
lh14g13 14:bc77edc4adb0 147 { // when cornering left the left motor slows down more than the right hand side
lh14g13 2:cc8ddc587af7 148 // may just replace with ACC and DECC
lh14g13 14:bc77edc4adb0 149
lh14g13 14:bc77edc4adb0 150 bool leftOrRight;
lh14g13 11:4a6f97cc1f1e 151
lh14g13 14:bc77edc4adb0 152 if(theta<0)
lh14g13 14:bc77edc4adb0 153 {
lh14g13 14:bc77edc4adb0 154 leftOrRight=true;
lh14g13 14:bc77edc4adb0 155 theta=theta*-1;
lh14g13 14:bc77edc4adb0 156
lh14g13 14:bc77edc4adb0 157 }
lh14g13 14:bc77edc4adb0 158
lh14g13 18:e3fd26490f58 159 float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
lh14g13 3:5b5d5af46804 160
lh14g13 15:f40e834d063b 161 w1= speed+ deltaW;
lh14g13 15:f40e834d063b 162 w2= speed -deltaW;
lh14g13 2:cc8ddc587af7 163
lh14g13 2:cc8ddc587af7 164
lh14g13 14:bc77edc4adb0 165 return;
lh14g13 14:bc77edc4adb0 166 }
lh14g13 2:cc8ddc587af7 167
lh14g13 7:9aaa4f73bb32 168
lh14g13 7:9aaa4f73bb32 169
lh14g13 5:c50e40797114 170
lh14g13 14:bc77edc4adb0 171
lh14g13 14:bc77edc4adb0 172
lh14g13 14:bc77edc4adb0 173
lh14g13 14:bc77edc4adb0 174
lh14g13 15:f40e834d063b 175
lh14g13 15:f40e834d063b 176
lh14g13 15:f40e834d063b 177
lh14g13 15:f40e834d063b 178