-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotor_control_01.txt
220 lines (192 loc) · 7.5 KB
/
motor_control_01.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
still have to assign pins in arduino
*/
#include <PID_v1.h>
double kp = 0.5, ki = 1, kd = 0; //PID variables and constants NEED TO FIND NEW VALUES(IF NEEDED)
double motorTorque; //torque in motor(PID INPUT)
double torquePedal; //torque supplied by the pedlar (from torque sensor)(PID SETPOINT)
double PWM; //PWM(PID OUTPUT)
int ON,OFF; //ON time and Off time for PWM
double inT=0,finT=0; //holds time for the function
const int pinc=0/*value*/,pins=0/*value*/,pinT=0/*value*/,pinI=0/*value*/; // pins for sine, cosine(speed calculation from torque sensor), torque sensor ,current(current sensor) !! GIVE VALUES
int sine,cosine;
int count;
int ival;
float RPMf(int ival) //function for angular speed
{
sine=digitalRead(pins);
cosine=digitalRead(pinc);
if(sine+cosine!=ival)
{
ival=sine+cosine;
inT=finT;
finT=millis();
//degpsec=(90/8)/(finT-inT);
//RPM=degpsec/360;
return (1/(32*(finT-inT)));
}
else
{
return RPMf(sine+cosine);
}
}
//function for Tp(Pedaller torque)
int Tpf()
{
// read the input on analog pin 1:
int sensorValue = analogRead(pinT);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
return (100*voltage-250);
}
PID myPID(&motorTorque, &PWM, &torquePedal, kp, ki, kd, DIRECT);
float angSpeed; //angSpeed is angular speed of the motor,
float V=48, I; //V and I are avg of qualtities supplied to the motor
float e,esum; //e is instantanious efficiency of motor from efficiency table
const int a_m = 0, b_m = 0, c_m = 0, A_m = 0, B_m = 0, C_m = 0; // ports for mosfet(a,b,c need PWM) !!! GIVE VALUES !!! A,B&C are high side mosfet logic
const int a_h = 0, b_h = 0,c_h = 0; //ports for hallsensor inputs
int a, b, c, A, B, C;
int hall; // holds hall sensor inputs
float r = 2*3.14159265/60; //this is for converting units of angSpeed from RPM to red/s (torque calculation)
// efficiency chart
float Ival[60]={0.667 , 0.716 , 0.774 , 0.838 , 0.932 , 1.054 , 1.200 , 1.379 , 1.563 , 1.775,
2.027 , 2.287 , 2.567 , 2.871 , 3.194 , 3.513 , 3.866 , 4.235 , 4.596 , 4.990,
5.304 , 5.675 , 6.070 , 6.455 , 6.857 , 7.273 , 7.687 , 8.101 , 8.505 , 8.923,
9.355 , 9.766 , 10.190 , 10.610 , 11.050 , 11.480 , 11.900 , 12.330 , 12.760 , 13.160,
13.290 , 13.570 , 13.820 , 13.950 , 14.050 , 14.090 , 14.200 , 14.330 , 14.380 , 14.500,
14.490 , 14.590 , 14.640 , 14.720 , 14.760 , 14.800 , 14.880 , 14.910 , 14.820 , 14.890};
float ASval[60]={243.000 , 243.000 , 243.000 , 242.000 , 242.000 , 241.000 , 240.000 , 240.000 , 239.000 , 238.000,
237.000 , 236.000 , 234.000 , 233.000 , 232.000 , 230.000 , 229.000 , 227.000 , 226.000 , 224.000,
222.000 , 221.000 , 220.000 , 217.000 , 216.000 , 214.000 , 214.000 , 212.000 , 210.000 , 208.000,
207.000 , 204.000 , 203.000 , 202.000 , 200.000 , 198.000 , 197.000 , 195.000 , 193.000 , 191.000,
184.000 , 180.000 , 176.000 , 170.000 , 164.000 , 159.000 , 153.000 , 148.000 , 141.000 , 135.000,
128.000 , 122.000 , 116.000 , 110.000 , 105.000 , 100.000 , 96.000 , 91.000 , 85.000 , 80.000};
float Eval[60]={1.300 , 1.000 , 1.600 , 11.300 , 20.600 , 29.200 , 38.900 , 45.500 , 52.900 , 58.200,
62.900 , 66.600 , 69.700 , 72.400 , 74.400 , 76.700 , 77.900 , 78.600 , 79.800 , 79.800,
81.200 , 81.800 , 82.300 , 82.000 , 82.300 , 82.100 , 82.700 , 82.200 , 82.000 , 81.900,
81.600 , 80.600 , 80.500 , 80.400 , 79.900 , 79.000 , 79.000 , 78.200 , 77.700 , 77.000,
76.200 , 75.500 , 74.600 , 73.500 , 72.600 , 71.900 , 70.600 , 69.300 , 68.000 , 66.700,
65.100 , 63.400 , 61.700 , 59.700 , 58.100 , 56.400 , 55.100 , 53.100 , 50.900 , 48.400};
float Vval[60]={ 48.080 , 48.080 , 48.080 , 48.080 , 48.080 , 48.080, 48.080 , 48.080 , 48.080, 48.080,
48.080, 48.080 , 48.080 , 48.080 , 48.080 , 48.080 , 48.080 , 48.080 , 48.080 , 48.080,
48.100 , 48.100 , 48.100 , 48.100 , 48.100 , 48.100 , 48.100 , 48.110 , 48.110 , 48.110,
48.110 , 48.110 , 48.110 , 48.110 , 48.120 , 48.120 ,48.120 ,48.110 , 48.110 , 48.120,
48.110 , 48.110 , 48.090 , 48.070 , 48.050 , 48.040 , 48.040 , 48.040 , 48.040 , 48.070,
48.110 , 48.110 , 48.110 , 48.110 , 48.120 , 48.110 , 48.110 , 48.110 , 48.120 , 48.090},vsum;
int P[60],k,j; //variables for finding efficiency from chart
void setup()
{
//take angSpeed, V, torquePedal and I as input from appropriate pins
myPID.SetMode(AUTOMATIC); // for using PID library's Compute function
Serial.begin(9600); // just for printing Output (DEBUG only)
pinMode(a_m, OUTPUT); //mosfet output ports
pinMode(b_m, OUTPUT);
pinMode(c_m, OUTPUT);
pinMode(A_m, OUTPUT);
pinMode(B_m, OUTPUT);
pinMode(C_m, OUTPUT);
pinMode(a_h, INPUT); //hallsensor input ports
pinMode(b_h, INPUT);
pinMode(c_h, INPUT);
pinMode(pins, INPUT);
pinMode(pinc, INPUT);
pinMode(pinT, INPUT);
pinMode(pinT, INPUT);
}
void loop()
{
I = analogRead(pinI);
sine=digitalRead(pins);
cosine=digitalRead(pinc);
ival=sine+cosine;
angSpeed=RPMf(ival);
torquePedal= Tpf();
//calculatin motor torque
k=0;
for(j=0;j<60;j++)
{
if((Ival[j]-I<0/*value*/)&&(I-Ival[j]<0/*value*/)) // PUT VALUES
{
P[k]=j;
k++;
}
}
count=k;k=0;esum=0;vsum=0;
for(j=0;j<count;j++)
{
if((ASval[P[j]]-angSpeed<0/*value*/)&&(angSpeed-ASval[P[j]]<0/*value*/)) // PUT VALUES
{
esum = esum + Eval[P[j]];
vsum = vsum + Vval[P[j]];
k++;
}
}
V = vsum/k;
e = esum/k;
motorTorque=V*I*e/(r*angSpeed*100);
myPID.Compute(); // PID
if (torquePedal=0)PWM=0; //takes care of case when pedal_torque=0
hall=digitalRead(a_h); // hall sensor value is stored
hall=10*hall+digitalRead(b_h); // in form of abc
hall=10*hall+digitalRead(c_h);
// switching
ON = (100*PWM)/256;
OFF = (100*(256-PWM))/256;
if(hall==101) //step1
{
a=1;A=0;b=0;B=1;c=0;C=0;
}
if(hall==100) //step2
{
a=1;A=0;b=0;B=0;c=0;C=1;
}
if(hall==110) //step3
{
a=0;A=0;b=1;B=0;c=0;C=1;
}
if(hall==010) //step4
{
a=0;A=1;b=1;B=0;c=0;C=0;
}
if(hall==011) //step5
{
a=0;A=1;b=0;B=0;c=1;C=0;
}
if(hall==001) //step6
{
a=0;A=0;b=0;B=1;c=1;C=0;
}
digitalWrite(a_m, a);
digitalWrite(b_m, b);
digitalWrite(c_m, c);
digitalWrite(A_m, 0);
digitalWrite(B_m, 0);
digitalWrite(C_m, 0);
if (A==1)
{
digitalWrite(a_m, 0);
digitalWrite(A_m, 1);
delayMicroseconds(ON);
digitalWrite(a_m, 1);
digitalWrite(A_m, 0);
delayMicroseconds(OFF);
}
if (B==1)
{
digitalWrite(b_m, 0);
digitalWrite(B_m, 1);
delayMicroseconds(ON);
digitalWrite(b_m, 1);
digitalWrite(B_m, 0);
delayMicroseconds(OFF);
}
if (C==1)
{
digitalWrite(c_m, 0);
digitalWrite(C_m, 1);
delayMicroseconds(ON);
digitalWrite(c_m, 1);
digitalWrite(C_m, 0);
delayMicroseconds(OFF);
}
}