Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "arduino.h"
- #include <libmaple/iwdg.h>
- #include <HardwareTimer.h>
- #include <Wire.h>
- #include <STM32ADC.h>
- #include <vl53l0x_class.h>
- TwoWire WIRE1(2, I2C_FAST_MODE);
- VL53L0X sensor_vl53l0x(&WIRE1, PB12, PB13);
- STM32ADC myADC(ADC1);
- STM32ADC myADC2(ADC2);
- #define PWM_OUT PB6
- #define PWM_OUT2 PB7
- #define PWM_OUT_INT PB5
- #define PWM_OUT2_INT PB8
- #define PWM_OUT_EN PB4
- #define PWM_OUT2_EN PB9
- float kp = 14.0f, ki = 0.0, kd = 2.00;
- float errSum, dErr, error, lastErr;
- float output_pid;
- uint16_t data_adc1 = 0;
- uint16_t data_adc2 = 0;
- uint8 pins[] = {PA0};
- const int maxSamples = 1;
- uint16_t dataPoints[maxSamples];
- uint8 pin2 = PA1;
- uint16 dado_adc = 0;
- float output_pid2 = 0;
- uint16_t Alimi = 2000;
- uint32_t coun1 = 0;
- uint32_t coun2 = 0;
- uint32_t timeout_close = 1000;
- uint16_t max_det = 500;
- uint32_t previousMillis = 0;
- uint32_t previousMillis2 = 0;
- uint32_t previousMillis3 = 0;
- uint32_t previousMillis4 = 0;
- int adc111 = 0;
- int st = 0;
- float adc111O = 0;
- float adc111O2 = 10;
- float adc_timeout = 0;
- float Algal = 0;
- float Algal_T = 0;
- //float distance_RW = 0;
- float distance_R = 0;
- uint16_t stack1 = 0;
- uint16_t stack2 = 0;
- uint32_t CT = 0;
- void int_func()
- {
- dado_adc = myADC2.getData();
- }
- void setup()
- {
- Serial.begin(115200);
- pinMode(PC13, OUTPUT);
- pinMode(PWM_OUT_EN, OUTPUT);
- pinMode(PWM_OUT2_EN, OUTPUT);
- pinMode(PA0, INPUT_ANALOG);
- pinMode(PA1, INPUT_ANALOG);
- pinMode(PA2, OUTPUT);
- pinMode(PA3, INPUT_PULLDOWN);
- pinMode(PA4, OUTPUT);
- pinMode(PA5, INPUT_ANALOG);
- digitalWrite(PWM_OUT_EN, HIGH);
- digitalWrite(PWM_OUT2_EN, HIGH);
- pinMode(PWM_OUT_INT, INPUT);
- pinMode(PWM_OUT2_INT, INPUT);
- delay(1000);
- Serial.println("tongST");
- str:
- WIRE1.begin();
- sensor_vl53l0x.VL53L0X_Off();
- int status123;
- status123 = sensor_vl53l0x.InitSensor(0x10);
- //sensor_vl53l0x.VL53L0X_decode_timeout(10);
- if (status123)
- {
- Serial.println("Init sensor_vl53l0x failed...");
- goto str;
- }
- attachInterrupt(PWM_OUT_INT, ADCRE1, FALLING);
- attachInterrupt(PWM_OUT2_INT, ADCRE2, FALLING);
- attachInterrupt(PA3, INPUTStatus, FALLING);
- Timer1.setMode(TIMER_CH3, TIMER_OUTPUTCOMPARE);
- Timer1.setPeriod(1000);
- Timer1.setCompare(TIMER_CH3, 1);
- Timer1.attachInterrupt(TIMER_CH3, handler_led);
- HardwareTimer timer1 = HardwareTimer(4);
- timer1.setPrescaleFactor(1);
- timer1.setOverflow(4499);
- pinMode(PWM_OUT, PWM);
- pinMode(PWM_OUT2, PWM);
- delay(1000);
- myADC.calibrate();
- myADC.setSampleRate(ADC_SMPR_1_5);
- myADC.setScanMode();
- delay(1000);
- iwdg_init(IWDG_PRE_4, 100);
- nvic_irq_set_priority(NVIC_EXTI_15_10, 0);
- pwmWrite(PWM_OUT, 0);
- pwmWrite(PWM_OUT2, 0);
- }
- void loop()
- {
- if ((millis() - previousMillis3) > timeout_close && adc111O2 == 900)
- {
- adc111O2 = 10;
- //previousMillis3 = millis();
- }
- // if (distance_R > max_det && distance_R < max_det + 100 && adc111O > 2500 && adc111O < 2700 && adc111O2 == 2600)
- // {
- // adc111O2 = 1400;
- // }
- if (adc111O > 1350 && adc111O < 1550)
- {
- stack1 = 0;
- digitalWrite(PA2, HIGH);
- }
- else
- {
- digitalWrite(PA2, LOW);
- }
- if (adc111O > 2500 && adc111O < 2700)
- {
- digitalWrite(PA4, HIGH);
- }
- else
- {
- digitalWrite(PA4, LOW);
- }
- // if (Serial.available() > 0)
- // {
- // char incomingByte = Serial.read();
- // if (incomingByte == 'A')
- // {
- // adc111O2 = 900;
- // previousMillis3 = millis();
- // }
- // if (incomingByte == 'B')
- // {
- // //adc111O2 = 2600;
- // }
- // }
- if (millis() - previousMillis4 > 10 && adc111O > 2500 && adc111O < 2700)
- {
- uint32_t distance;
- int status;
- status = sensor_vl53l0x.GetDistance(&distance);
- if (status == VL53L0X_ERROR_NONE)
- {
- distance_R = distance;
- //smooth(0.6f, distance, distance_R);
- //distance;
- }
- if (distance_R > 200 && distance_R < 400 && stack1 == 0)
- {
- stack1 = 1;
- }
- if (distance_R > 500 && distance_R < 700 && stack1 == 1)
- {
- stack1 = 0;
- adc111O2 = 10;
- }
- // if(distance_R>700&&stack1 == 2){
- // stack1 = 0;
- // adc111O2 = 1400;
- // }
- previousMillis4 = millis();
- }
- if (millis() - previousMillis > 5)
- {
- Serial.print(data_adc1);
- Serial.print(",");
- Serial.print(data_adc2);
- Serial.print(",");
- Serial.print(Algal);
- Serial.print(",");
- Serial.print(adc111O2);
- Serial.print(",");
- Serial.print(distance_R);
- Serial.print(",");
- Serial.println(stack1);
- previousMillis = millis();
- }
- }
- void ADCRE1()
- {
- data_adc1 = analogRead(PA0);
- coun2++;
- coun1 = 0;
- if (data_adc2 > Alimi && coun2 > 1000)
- {
- data_adc2 = 0;
- coun2 = 0;
- }
- }
- void ADCRE2()
- {
- data_adc2 = analogRead(PA0);
- coun1++;
- coun2 = 0;
- if (data_adc1 > Alimi && coun1 > 1000)
- {
- data_adc1 = 0;
- coun1 = 0;
- }
- }
- void INPUTStatus()
- {
- adc111O2 = 900;
- previousMillis3 = millis();
- }
- void handler_led(void)
- {
- iwdg_feed();
- adc111O = smooth(0.9f, analogRead(PA1), adc111O);
- Algal = map(adc111O, 1450, 2600, 0, 900);
- adc_timeout = smooth(0.1f, analogRead(PA5), adc_timeout);
- timeout_close = ((((uint16_t)adc_timeout) >> 8) * 500) + 1000;
- myPID();
- //output_pid2 = output_pid * ((1.00f - (abs(error) / 1300)) + 0.3f);
- }
- float limit(float input, int min_limit, int max_limit)
- {
- if (input > max_limit)
- input = max_limit;
- if (input < min_limit)
- input = min_limit;
- return input;
- }
- float smooth(float alfa, float new_data, float old_data)
- {
- return (old_data + alfa * (new_data - old_data));
- }
- void myPID()
- {
- lastErr = error;
- if (Algal_T > adc111O2)
- {
- if (Algal_T - adc111O2 < 100)
- {
- Algal_T = Algal_T - 0.5f;
- }
- else
- {
- Algal_T = Algal_T - 1.5f;
- }
- }
- if (Algal_T < adc111O2)
- {
- if (adc111O2 - Algal_T < 100)
- {
- Algal_T = Algal_T + 0.5f;
- }
- else
- {
- Algal_T = Algal_T + 1.5f;
- }
- }
- error = Algal - Algal_T;
- dErr = smooth(0.8f,((error - lastErr) * 10.0f),dErr);
- if (abs(error) <= 5)
- {
- error = 0;
- dErr = 0;
- }
- errSum = limit(errSum + (error * 0.01f), -200, 200);
- output_pid = (kp * error) + (ki * errSum) + (kd * dErr);
- MOTOR(limit(output_pid, -4000.00, 4000.00));
- }
- void MOTOR(int y)
- {
- static float ramp;
- if (ramp < 1)
- {
- ramp += 0.0003f;
- }
- int x = y * ramp;
- if (x < -1)
- {
- digitalWrite(PC13, HIGH);
- if (data_adc1 > Alimi)
- {
- pwmWrite(PWM_OUT, 10); //
- pwmWrite(PWM_OUT2, 0);
- }
- else
- {
- pwmWrite(PWM_OUT, abs(x)); //
- pwmWrite(PWM_OUT2, 0);
- }
- }
- else if (x > 1)
- {
- digitalWrite(PC13, HIGH);
- if (data_adc2 > Alimi)
- {
- pwmWrite(PWM_OUT, 0);
- pwmWrite(PWM_OUT2, 10);
- }
- else
- {
- pwmWrite(PWM_OUT, 0);
- pwmWrite(PWM_OUT2, abs(x));
- }
- }
- else
- {
- digitalWrite(PC13, LOW);
- pwmWrite(PWM_OUT, 0);
- pwmWrite(PWM_OUT2, 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement