test

Dependencies:   mbed

Committer:
laik777
Date:
Thu Dec 20 08:29:12 2018 +0000
Revision:
3:63e99a7361ec
Parent:
2:549286fb28d4
matej_slapsak;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
laik777 0:43c630791109 1 #include "mbed.h"
laik777 0:43c630791109 2
laik777 0:43c630791109 3 //indikacija rgb
laik777 0:43c630791109 4 #define OFF 0
laik777 0:43c630791109 5 #define ON 1
laik777 0:43c630791109 6
laik777 0:43c630791109 7 DigitalOut myled_blue(p16);
laik777 0:43c630791109 8 DigitalOut myled_green(p30);
laik777 0:43c630791109 9 DigitalOut myled_red(p15);
laik777 0:43c630791109 10
laik777 0:43c630791109 11 DigitalIn start1(p11);
laik777 0:43c630791109 12 DigitalIn start2(p12);
laik777 0:43c630791109 13 DigitalIn start3(p13);
laik777 0:43c630791109 14 DigitalIn start4(p10);
laik777 0:43c630791109 15
laik777 0:43c630791109 16 //nastavitev pwm signalov
laik777 2:549286fb28d4 17 PwmOut pwm1 (p21);
laik777 0:43c630791109 18 PwmOut pwm2(p22);
laik777 0:43c630791109 19
laik777 0:43c630791109 20 //dodano za merjenje toka in napetosti
laik777 0:43c630791109 21 //def analogno merjenje nap in toka
laik777 0:43c630791109 22 AnalogIn measVol (p17);
laik777 0:43c630791109 23 AnalogIn measCurrG (p18);
laik777 0:43c630791109 24 AnalogIn measCurrH(p19);
laik777 0:43c630791109 25 float voltageMeas = 0.0;
laik777 0:43c630791109 26 float voltageCalc = 0.0;
laik777 0:43c630791109 27 float currentMeasG = 0.0;
laik777 0:43c630791109 28 float currentCalcG = 0.0;
laik777 0:43c630791109 29 float currentMeasH = 0.0;
laik777 0:43c630791109 30 float currentCalcH = 0.0;
laik777 0:43c630791109 31
laik777 3:63e99a7361ec 32 float PWM1 = 0.2f; //20% duty cycle
laik777 3:63e99a7361ec 33 float PWM2 = 0.6f; //60% duty cycle
laik777 3:63e99a7361ec 34
laik777 0:43c630791109 35 //funkcija za nastavljanje baudrate pri serijski komunikaciji na PC
laik777 0:43c630791109 36 void baud(int baudrate)
laik777 0:43c630791109 37 {
laik777 0:43c630791109 38 Serial s(USBTX, USBRX);
laik777 0:43c630791109 39 s.baud(baudrate);
laik777 0:43c630791109 40 }
laik777 0:43c630791109 41
laik777 0:43c630791109 42 void merjenjeTokaInNapetosti() {
laik777 0:43c630791109 43
laik777 0:43c630791109 44 printf ("\r\n");
laik777 0:43c630791109 45 printf ("<------- \r\n");
laik777 0:43c630791109 46 printf ("Measurement: \r\n");
laik777 0:43c630791109 47 DigitalOut pwmGD (p21);
laik777 0:43c630791109 48 pwmGD = 1;
laik777 3:63e99a7361ec 49 //pwmHD = 1;
laik777 0:43c630791109 50
laik777 0:43c630791109 51 wait(0.1);
laik777 0:43c630791109 52
laik777 0:43c630791109 53 voltageMeas = measVol.read();
laik777 0:43c630791109 54 voltageMeas *= 3.3;
laik777 0:43c630791109 55 voltageCalc = voltageMeas * 9.15; //8.2
laik777 0:43c630791109 56
laik777 0:43c630791109 57 currentMeasG = measCurrG.read();
laik777 0:43c630791109 58 currentMeasG *= 3.3;
laik777 0:43c630791109 59 currentCalcG = currentMeasG * 1.259;
laik777 0:43c630791109 60
laik777 0:43c630791109 61 currentMeasH = measCurrH.read();
laik777 0:43c630791109 62 currentMeasH *= 3.3;
laik777 0:43c630791109 63 currentCalcH = currentMeasH * 1.279;
laik777 3:63e99a7361ec 64 wait(3);
laik777 0:43c630791109 65
laik777 0:43c630791109 66 printf("Voltage: %f V\r\n", voltageCalc);
laik777 0:43c630791109 67 printf("Current1: %f A\r\n", currentCalcG);
laik777 3:63e99a7361ec 68 //printf("Current2: %f A\r\n", currentCalcH);
laik777 0:43c630791109 69 printf ("-------> \r\n");
laik777 0:43c630791109 70
laik777 0:43c630791109 71 pwmGD = 0;
laik777 0:43c630791109 72
laik777 0:43c630791109 73 }
laik777 0:43c630791109 74
laik777 0:43c630791109 75 int main()
laik777 3:63e99a7361ec 76 {
laik777 0:43c630791109 77 //hitrost serijske komunikacije
laik777 3:63e99a7361ec 78 baud(9600);
laik777 0:43c630791109 79
laik777 0:43c630791109 80 while(1)
laik777 0:43c630791109 81 {
laik777 0:43c630791109 82 myled_blue = OFF;
laik777 0:43c630791109 83 myled_green = OFF;
laik777 0:43c630791109 84 myled_red = OFF;
laik777 0:43c630791109 85
laik777 1:ceefb922cff9 86 if(start1 == 0)
laik777 1:ceefb922cff9 87 {
laik777 1:ceefb922cff9 88 myled_blue = ON;
laik777 1:ceefb922cff9 89 merjenjeTokaInNapetosti();
laik777 1:ceefb922cff9 90 wait(2);
laik777 1:ceefb922cff9 91 }
laik777 1:ceefb922cff9 92
laik777 1:ceefb922cff9 93 else if(start2 == 0)
laik777 0:43c630791109 94 {
laik777 3:63e99a7361ec 95 //---------------SET
laik777 3:63e99a7361ec 96 //signal duration
laik777 3:63e99a7361ec 97 float t1 = 0.1;
laik777 3:63e99a7361ec 98 float t2 = 0.2;
laik777 3:63e99a7361ec 99 //duty cycle
laik777 3:63e99a7361ec 100 //float PWM1 = 0.2f; //20% duty cycle
laik777 3:63e99a7361ec 101 //float PWM2 = 0.6f; //60% duty cycle
laik777 3:63e99a7361ec 102 //---------------SET
laik777 3:63e99a7361ec 103
laik777 0:43c630791109 104 myled_green = ON;
laik777 2:549286fb28d4 105 PwmOut pwm1 (p21);
laik777 2:549286fb28d4 106 PwmOut pwm2(p22);
laik777 0:43c630791109 107 printf ("\r\n");
laik777 0:43c630791109 108 printf ("<------- \r\n");
laik777 0:43c630791109 109
laik777 0:43c630791109 110 //nastavitev pwm period
laik777 2:549286fb28d4 111 pwm1.period(0.001f);
laik777 2:549286fb28d4 112 pwm2.period(0.001f);
laik777 0:43c630791109 113
laik777 0:43c630791109 114 pwm1=0;
laik777 0:43c630791109 115 pwm2=0;
laik777 0:43c630791109 116
laik777 0:43c630791109 117 printf ("Start \r\n");
laik777 0:43c630791109 118 printf("pwm1 read: %f\n\r",pwm1.read());
laik777 3:63e99a7361ec 119 //printf("pwm2 read: %f\n\r",pwm2.read());
laik777 0:43c630791109 120
laik777 0:43c630791109 121 //printf ("\r\n");
laik777 0:43c630791109 122
laik777 0:43c630791109 123 //printf("pwm1 read: %f\n\r",pwm1.read()*100);
laik777 0:43c630791109 124
laik777 0:43c630791109 125 //izvajanje krmilja
laik777 0:43c630791109 126 printf ("PWM set \r\n");
laik777 0:43c630791109 127
laik777 3:63e99a7361ec 128 pwm1.write(PWM1);
laik777 0:43c630791109 129
laik777 0:43c630791109 130 printf("pwm1 read: %f\n\r",pwm1.read());
laik777 3:63e99a7361ec 131 //printf("pwm2 read: %f\n\r",pwm2.read());
laik777 3:63e99a7361ec 132 wait(t1);
laik777 0:43c630791109 133
laik777 0:43c630791109 134 printf ("PWM set \r\n");
laik777 3:63e99a7361ec 135 pwm1.write(PWM2);
laik777 0:43c630791109 136
laik777 3:63e99a7361ec 137 printf("pwm2 read: %f\n\r",pwm1.read());
laik777 3:63e99a7361ec 138 //printf("pwm2 read: %f\n\r",pwm2.read());
laik777 3:63e99a7361ec 139 wait(t2);
laik777 0:43c630791109 140 printf ("PWM off \r\n");
laik777 0:43c630791109 141 pwm1 = 0;
laik777 0:43c630791109 142 pwm2 = 0;
laik777 0:43c630791109 143
laik777 0:43c630791109 144
laik777 0:43c630791109 145 printf("pwm1 read: %f\n\r",pwm1.read());
laik777 3:63e99a7361ec 146 //printf("pwm2 read: %f\n\r",pwm2.read());
laik777 0:43c630791109 147 printf ("End \r\n");
laik777 0:43c630791109 148 printf ("-------> \r\n");
laik777 0:43c630791109 149 wait(2);
laik777 0:43c630791109 150 myled_green = OFF;
laik777 0:43c630791109 151 }
laik777 0:43c630791109 152
laik777 0:43c630791109 153 else if(start3 == 0)
laik777 0:43c630791109 154 {
laik777 3:63e99a7361ec 155
laik777 3:63e99a7361ec 156 if(PWM1 >= 1.0f)
laik777 3:63e99a7361ec 157 {
laik777 3:63e99a7361ec 158 PWM1 = 0.0f;
laik777 3:63e99a7361ec 159 }
laik777 3:63e99a7361ec 160
laik777 3:63e99a7361ec 161 else
laik777 3:63e99a7361ec 162 {
laik777 3:63e99a7361ec 163 PWM1 = PWM1 + 0.1f;
laik777 3:63e99a7361ec 164 }
laik777 3:63e99a7361ec 165
laik777 3:63e99a7361ec 166 printf("\n\r");
laik777 3:63e99a7361ec 167 printf("PWM1: %f\n\r",PWM1);
laik777 3:63e99a7361ec 168
laik777 0:43c630791109 169 myled_red = ON;
laik777 3:63e99a7361ec 170 wait(0.2);
laik777 0:43c630791109 171 }
laik777 0:43c630791109 172
laik777 0:43c630791109 173 else if(start4 == 0)
laik777 0:43c630791109 174 {
laik777 3:63e99a7361ec 175 if(PWM2 >= 1.0f)
laik777 3:63e99a7361ec 176 {
laik777 3:63e99a7361ec 177 PWM2 = 0.0f;
laik777 3:63e99a7361ec 178 }
laik777 3:63e99a7361ec 179
laik777 3:63e99a7361ec 180 else
laik777 3:63e99a7361ec 181 {
laik777 3:63e99a7361ec 182 PWM2 = PWM2 + 0.1f;
laik777 3:63e99a7361ec 183 }
laik777 3:63e99a7361ec 184
laik777 3:63e99a7361ec 185 printf("\n\r");
laik777 3:63e99a7361ec 186 printf("PWM2: %f\n\r",PWM2);
laik777 3:63e99a7361ec 187
laik777 0:43c630791109 188 myled_blue = ON;
laik777 0:43c630791109 189 myled_red = ON;
laik777 0:43c630791109 190 myled_green = ON;
laik777 3:63e99a7361ec 191 wait(0.2);
laik777 0:43c630791109 192 }
laik777 0:43c630791109 193
laik777 0:43c630791109 194 }
laik777 0:43c630791109 195 }