#include <TLE5012-ino.hpp>
using namespace tle5012;
Tle5012Ino Tle5012Sensor = Tle5012Ino(40,Tle5012b::TLE5012B_S0 );
#define PWD 19
#define PWS 22
unsigned long CO2;
//#define PI (double) 3.14159265359
#define PHASE_DELAY_1 (double) 2.094395102 //120°
#define PHASE_DELAY_2 (double) 4.188790205 //240°
const int U = 24;
const int V = 23;
const int W = 9;
const int EN_U = 6;
const int EN_V = 5;
const int EN_W = 3;
int i=0;
int duty = 100;
int pwmOne = 0;
int pwmTwo = 0;
int pwmThree = 0;
double amplitudeOne;
double amplitudeTwo;
double amplitudeThree;
double angle = 0.0;
double last_angle = 0.0;
double sollWinkel = 0.0;
float angle_rad = 0.0;
double speed;
int KP = 20;
int KD = -50;
double former_millis = 0;
int myPWM_U_values[1800] = {-14, -11, -8, -4, -1, 2, 5, 9, 12, 15, 18, 22, 24, 27, 30, 33, 36, 38, 41, 43, 45, 48, 50, 52, 54, 56, 57, 59, 61, 63, 65, 66, 68, 69, 71, 73, 75, 76, 78, 80, 82, 83, 84, 86, 87, 88, 89, 91, 92, 92, 93, 94, 95, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 89, 87, 86, 84, 83, 81, 79, 77, 75, 74, 72, 70, 68, 66, 65, 63, 61, 60, 58, 57, 55, 54, 52, 51, 49, 47, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 23, 21, 18, 14, 11, 8, 4, 2, -1, -4, -8, -11, -15, -17, -21, -24, -26, -29, -32, -34, -37, -40, -42, -44, -47, -49, -51, -53, -55, -57, -59, -61, -63, -65, -67, -69, -71, -73, -75, -76, -78, -80, -82, -84, -85, -86, -88, -89, -90, -91, -92, -93, -94, -94, -95, -95, -96, -96, -97, -97, -98, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -91, -90, -89, -87, -86, -84, -83, -81, -79, -78, -76, -74, -73, -71, -69, -68, -66, -65, -63, -62, -60, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -39, -37, -35, -33, -32, -30, -27, -25, -23, -21, -18, -16, -13, -10, -7, -3, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 37, 40, 42, 45, 47, 50, 52, 54, 56, 58, 60, 62, 64, 66, 67, 69, 71, 73, 74, 76, 78, 80, 81, 83, 84, 86, 87, 88, 89, 91, 91, 92, 93, 94, 95, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 96, 95, 94, 93, 93, 92, 91, 89, 88, 87, 85, 83, 82, 80, 78, 76, 75, 73, 72, 70, 68, 67, 65, 63, 62, 60, 59, 57, 55, 54, 52, 50, 48, 47, 45, 43, 41, 39, 38, 36, 34, 32, 30, 28, 26, 24, 21, 18, 15, 11, 8, 4, 1, -1, -5, -9, -12, -16, -19, -22, -25, -28, -31, -33, -37, -39, -42, -44, -46, -49, -51, -53, -55, -57, -59, -61, -63, -65, -67, -69, -71, -73, -75, -77, -79, -81, -83, -84, -86, -87, -89, -90, -91, -92, -93, -94, -95, -95, -96, -97, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -96, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -86, -84, -82, -81, -79, -78, -76, -75, -73, -72, -70, -69, -68, -66, -65, -64, -62, -61, -60, -58, -57, -55, -54, -52, -51, -49, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -23, -20, -17, -14, -10, -7, -4, 0, 2, 6, 9, 13, 16, 19, 22, 25, 28, 31, 33, 36, 38, 40, 42, 45, 47, 50, 52, 54, 56, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 82, 84, 85, 86, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 96, 95, 94, 93, 92, 91, 90, 89, 88, 86, 85, 83, 81, 79, 78, 76, 75, 73, 71, 69, 67, 66, 64, 62, 61, 59, 58, 56, 55, 53, 52, 51, 49, 47, 46, 44, 42, 41, 39, 37, 36, 34, 32, 30, 29, 27, 25, 23, 21, 19, 16, 12, 9, 6, 3, 0, -3, -6, -10, -13, -17, -19, -22, -26, -29, -32, -34, -37, -40, -42, -44, -46, -49, -51, -53, -55, -57, -60, -62, -64, -66, -68, -70, -72, -74, -76, -77, -79, -81, -82, -84, -85, -87, -88, -89, -91, -92, -93, -94, -94, -95, -96, -96, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -90, -89, -88, -86, -85, -83, -81, -80, -78, -76, -75, -73, -72, -70, -68, -67, -65, -64, -62, -61, -59, -58, -56, -55, -53, -52, -50, -48, -47, -45, -44, -42, -40, -39, -37, -35, -33, -31, -29, -27, -25, -23, -21, -18, -15, -12, -8, -5, -2, 0, 3, 6, 10, 13, 16, 18, 21, 24, 27, 30, 32, 34, 36, 38, 41, 43, 45, 47, 49, 50, 53, 55, 57, 58, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 82, 84, 85, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 96, 97, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 95, 94, 93, 93, 91, 90, 89, 88, 86, 85, 83, 81, 80, 78, 76, 74, 73, 71, 69, 67, 66, 64, 62, 60, 58, 57, 55, 53, 51, 49, 48, 46, 44, 43, 41, 39, 37, 36, 34, 32, 30, 28, 26, 25, 23, 20, 18, 15, 12, 9, 5, 2, -1, -4, -7, -10, -14, -17, -20, -23, -26, -29, -32, -35, -38, -40, -42, -45, -47, -49, -51, -53, -55, -57, -59, -61, -63, -65, -67, -68, -70, -72, -74, -76, -77, -79, -81, -82, -83, -85, -86, -87, -88, -89, -90, -91, -92, -93, -94, -94, -95, -96, -96, -97, -97, -98, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -91, -90, -88, -87, -85, -84, -82, -80, -78, -77, -75, -73, -72, -70, -68, -67, -65, -63, -62, -60, -59, -57, -56, -54, -53, -51, -49, -48, -46, -44, -42, -40, -38, -36, -34, -32, -30, -28, -26, -23, -21, -18, -15, -12, -9, -6, -3, 0, 3, 7, 10, 13, 16, 20, 23, 26, 29, 32, 34, 37, 39, 41, 43, 45, 47, 49, 52, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 80, 82, 83, 85, 86, 87, 88, 90, 91, 92, 93, 94, 94, 95, 96, 96, 96, 97, 97, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 89, 87, 86, 84, 82, 81, 79, 77, 76, 74, 72, 71, 69, 67, 66, 64, 62, 61, 59, 58, 56, 54, 53, 51, 49, 47, 45, 43, 41, 39, 37, 36, 34, 32, 30, 28, 26, 24, 22, 19, 16, 13, 10, 6, 3, 0, -3, -7, -10, -13, -17, -20, -23, -26, -29, -32, -36, -39, -41, -44, -46, -49, -51, -53, -55, -57, -59, -61, -63, -65, -67, -69, -71, -72, -74, -76, -78, -79, -81, -82, -84, -85, -87, -88, -89, -90, -91, -92, -93, -94, -95, -95, -96, -96, -97, -97, -98, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -97, -96, -95, -95, -94, -93, -92, -92, -91, -89, -88, -87, -86, -84, -83, -81, -79, -78, -76, -75, -73, -71, -70, -68, -66, -65, -63, -61, -60, -58, -57, -55, -53, -51, -50, -48, -46, -44, -42, -41, -39, -37, -35, -33, -31, -29, -27, -24, -22, -19, -16, -13, -10, -7, -3, 0, 3, 6, 10, 13, 16, 19, 22, 25, 28, 31, 34, 36, 39, 41, 44, 46, 48, 50, 52, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 86, 87, 89, 90, 91, 92, 93, 94, 95, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 95, 94, 94, 92, 92, 90, 89, 88, 86, 85, 83, 82, 80, 78, 77, 75, 73, 72, 70, 69, 67, 66, 64, 63, 61, 60, 59, 57, 56, 54, 53, 52, 50, 48, 46, 45, 43, 41, 39, 37, 35, 33, 31, 29, 26, 24, 22, 18, 15, 11, 8, 5, 2, 0, -4, -8, -11, -15, -17, -20, -23, -26, -30, -33, -35, -38, -41, -43, -46, -48, -50, -53, -55, -57, -59, -61, -63, -65, -67, -69, -71, -73, -75, -77, -78, -80, -82, -83, -85, -86, -88, -89, -90, -91, -92, -93, -94, -95, -96, -96, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -96, -95, -94, -94, -93, -92, -91, -89, -88, -87, -86, -85, -83, -81, -80, -78, -76, -75, -73, -71, -69, -68, -66, -64, -62, -61, -59, -58, -56, -55, -53, -52, -50, -49, -47, -45, -44, -42, -41, -39, -38, -36, -34, -33, -31, -29, -27, -25, -24, -21, -19, -17, -14, -11, -8, -4, -1, 1, 5, 9, 12, 15, 18, 22, 25, 28, 31, 33, 36, 38, 41, 43, 46, 48, 50, 52, 54, 57, 59, 60, 63, 65, 66, 69, 71, 73, 74, 76, 78, 80, 82, 84, 85, 87, 88, 90, 91, 92, 93, 93, 94, 95, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 96, 95, 94, 93, 92, 90, 89, 88, 87, 85, 83, 81, 80, 78, 76, 75, 73, 71, 69, 68, 66, 64, 63, 61, 60, 58, 56, 55, 54, 52, 51, 49, 48, 47, 45, 43, 41, 39, 38, 36, 34, 32, 30, 28, 26, 23, 21, 18, 15, 12, 9, 6, 3, 0, -2, -6, -9, -12, -15, -18, -20, -23, -26, -28, -31, -33, -35, -38, -40, -42, -45, -47, -48, -51, -53, -55, -57, -59, -61, -63, -65, -67, -69, -71, -73, -75, -77, -79, -81, -83, -84, -86, -87, -88, -90, -91, -92, -93, -93, -94, -95, -96, -96, -97, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -95, -95, -94, -93, -92, -91, -90, -89, -87, -86, -84, -83, -81, -79, -77, -75, -74, -72, -70, -68, -67, -65, -63, -61, -60, -58, -56, -55, -53, -51, -49, -48, -46, -44, -42, -41, -39, -37, -36, -34, -32, -30, -29, -27, -25, -23, -21, -19, -17};
int myPWM_V_values[1800] = {92, 91, 90, 89, 87, 85, 84, 81, 79, 77, 75, 73, 71, 68, 66, 64, 62, 60, 57, 56, 53, 51, 49, 48, 45, 44, 42, 40, 37, 35, 33, 30, 28, 26, 23, 21, 19, 16, 13, 11, 8, 6, 3, 0, -1, -4, -6, -9, -11, -13, -16, -18, -20, -22, -24, -26, -28, -30, -32, -34, -36, -37, -39, -41, -42, -44, -46, -47, -49, -51, -52, -54, -56, -58, -60, -61, -63, -65, -67, -69, -71, -73, -74, -77, -78, -80, -82, -84, -85, -87, -88, -90, -91, -92, -93, -94, -95, -96, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -95, -95, -94, -92, -91, -90, -88, -87, -85, -83, -81, -79, -77, -75, -73, -71, -69, -67, -65, -62, -60, -58, -56, -54, -52, -50, -48, -46, -44, -41, -39, -36, -34, -31, -29, -26, -24, -21, -18, -16, -12, -10, -7, -4, -1, 1, 3, 6, 9, 11, 14, 16, 18, 20, 22, 24, 26, 28, 29, 31, 32, 34, 35, 36, 38, 39, 41, 42, 43, 45, 46, 47, 49, 50, 52, 54, 55, 57, 59, 61, 62, 64, 66, 68, 70, 72, 74, 75, 77, 79, 81, 82, 84, 85, 87, 88, 90, 91, 92, 93, 94, 95, 95, 96, 97, 97, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 96, 95, 95, 94, 93, 92, 90, 89, 87, 86, 84, 82, 80, 78, 76, 74, 71, 69, 66, 64, 62, 60, 58, 55, 53, 51, 49, 46, 44, 42, 39, 37, 35, 33, 31, 28, 26, 24, 21, 19, 16, 13, 11, 8, 5, 2, 0, -2, -4, -7, -10, -12, -15, -17, -19, -22, -24, -26, -27, -30, -31, -33, -35, -37, -39, -40, -42, -43, -45, -46, -48, -49, -50, -52, -53, -54, -56, -57, -59, -60, -62, -64, -65, -67, -68, -70, -72, -73, -75, -77, -79, -80, -82, -83, -85, -86, -88, -89, -90, -92, -93, -93, -94, -95, -96, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -95, -95, -93, -92, -91, -89, -88, -86, -84, -82, -80, -78, -76, -74, -72, -69, -67, -65, -62, -60, -58, -56, -54, -52, -50, -47, -45, -43, -41, -38, -36, -34, -31, -29, -26, -23, -20, -17, -14, -11, -8, -5, -2, 0, 3, 7, 10, 12, 15, 17, 19, 22, 24, 26, 28, 30, 32, 34, 35, 37, 38, 40, 41, 43, 44, 45, 47, 48, 49, 51, 52, 54, 56, 57, 59, 61, 63, 64, 66, 68, 70, 72, 73, 75, 77, 78, 80, 82, 83, 84, 86, 87, 88, 89, 91, 92, 93, 94, 94, 95, 96, 96, 97, 97, 98, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 89, 88, 86, 84, 82, 80, 78, 76, 74, 72, 70, 68, 66, 64, 62, 60, 58, 56, 54, 51, 49, 46, 44, 41, 39, 36, 34, 31, 29, 26, 23, 20, 18, 15, 12, 9, 6, 4, 1, -1, -3, -6, -9, -11, -14, -16, -18, -20, -22, -24, -26, -28, -30, -32, -34, -36, -38, -40, -42, -43, -45, -47, -49, -50, -52, -54, -55, -57, -59, -60, -62, -63, -65, -67, -69, -70, -72, -73, -75, -77, -78, -80, -82, -83, -85, -86, -88, -89, -91, -92, -93, -94, -94, -95, -96, -96, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -96, -95, -95, -94, -93, -92, -90, -89, -87, -86, -84, -82, -80, -78, -77, -74, -72, -70, -68, -66, -63, -61, -58, -56, -54, -52, -50, -48, -46, -43, -41, -38, -36, -34, -31, -28, -26, -23, -21, -18, -15, -12, -9, -7, -4, -1, 1, 3, 6, 9, 12, 14, 17, 19, 22, 24, 26, 28, 30, 32, 34, 36, 37, 39, 40, 42, 43, 45, 46, 48, 50, 51, 52, 54, 56, 57, 59, 61, 62, 64, 66, 68, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 87, 89, 90, 91, 92, 93, 94, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 96, 95, 95, 94, 93, 92, 91, 89, 88, 86, 85, 83, 81, 79, 77, 75, 73, 71, 69, 67, 65, 63, 61, 60, 58, 56, 54, 52, 51, 49, 47, 45, 42, 40, 38, 35, 33, 30, 27, 24, 21, 19, 16, 12, 9, 6, 4, 1, 0, -3, -6, -8, -11, -13, -16, -18, -20, -22, -24, -26, -28, -30, -32, -33, -35, -37, -38, -40, -41, -43, -44, -46, -48, -50, -51, -53, -55, -57, -59, -61, -63, -65, -67, -69, -70, -72, -74, -76, -77, -79, -81, -82, -84, -85, -87, -89, -90, -91, -92, -93, -94, -95, -96, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -96, -95, -94, -94, -93, -92, -90, -89, -87, -86, -84, -83, -81, -78, -76, -74, -72, -70, -68, -66, -64, -61, -59, -57, -55, -53, -51, -48, -46, -44, -42, -40, -37, -35, -33, -30, -28, -26, -23, -20, -18, -15, -13, -10, -7, -5, -2, 0, 1, 3, 6, 8, 10, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 38, 40, 42, 43, 45, 47, 48, 50, 52, 54, 55, 57, 59, 61, 62, 64, 66, 68, 69, 71, 73, 75, 76, 78, 80, 82, 83, 85, 86, 88, 90, 91, 92, 93, 94, 95, 95, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 96, 95, 94, 93, 92, 91, 89, 88, 86, 84, 82, 80, 79, 77, 75, 73, 70, 68, 66, 64, 62, 60, 58, 56, 54, 52, 49, 47, 46, 43, 41, 39, 37, 35, 33, 30, 27, 25, 22, 19, 16, 13, 11, 8, 5, 2, 0, -2, -4, -7, -10, -12, -15, -17, -19, -21, -23, -25, -27, -28, -30, -32, -33, -35, -37, -38, -40, -41, -42, -43, -45, -46, -47, -48, -50, -52, -53, -55, -56, -58, -60, -62, -64, -66, -68, -70, -72, -74, -76, -78, -80, -82, -83, -85, -86, -88, -89, -91, -92, -93, -94, -94, -95, -96, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -91, -90, -88, -87, -85, -83, -81, -79, -77, -75, -72, -70, -67, -65, -62, -60, -58, -55, -53, -51, -49, -46, -44, -42, -40, -38, -35, -33, -31, -28, -26, -23, -20, -18, -15, -12, -9, -6, -4, -1, 0, 3, 5, 8, 11, 13, 15, 17, 19, 22, 24, 26, 28, 30, 32, 33, 35, 36, 38, 40, 41, 43, 45, 46, 47, 49, 51, 52, 54, 55, 57, 59, 60, 62, 64, 65, 67, 68, 70, 72, 73, 75, 76, 78, 79, 81, 82, 83, 85, 86, 87, 88, 90, 92, 93, 93, 94, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 95, 95, 93, 92, 91, 90, 88, 87, 85, 83, 81, 79, 77, 75, 73, 71, 69, 66, 64, 62, 60, 58, 56, 54, 52, 49, 47, 45, 42, 40, 38, 36, 33, 31, 28, 25, 22, 19, 16, 12, 9, 6, 3, 0, -2, -5, -8, -10, -13, -15, -18, -20, -22, -24, -26, -28, -30, -32, -33, -35, -37, -39, -40, -42, -43, -45, -46, -47, -49, -50, -51, -53, -54, -56, -57, -59, -61, -62, -64, -66, -68, -70, -71, -73, -75, -77, -79, -81, -82, -84, -85, -87, -88, -90, -91, -92, -93, -94, -94, -95, -96, -96, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -95, -94, -93, -92, -91, -89, -88, -86, -85, -83, -81, -78, -76, -75, -73, -70, -68, -66, -64, -61, -59, -56, -54, -52, -50, -48, -45, -43, -41, -38, -36, -33, -31, -28, -26, -23, -20, -18, -14, -12, -9, -6, -3, 0, 2, 5, 7, 10, 13, 15, 18, 20, 22, 25, 27, 29, 31, 33, 35, 36, 39, 40, 42, 44, 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 61, 62, 64, 66, 67, 69, 70, 72, 74, 75, 76, 78, 80, 81, 83, 84, 85, 87, 88, 90, 91, 92, 93, 94, 95, 96, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 98, 97, 97, 96, 96, 95, 94, 94, 93, 92, 90, 89, 87, 86, 84, 82, 80, 78, 76, 74, 72, 69, 67, 65, 63, 61, 59, 56, 54, 52, 50, 48, 46, 43, 41, 38, 36, 34, 31, 28, 26, 23, 21, 17, 14, 11, 8, 5, 2, 0, -3, -6, -9, -11, -13, -16, -18, -20, -22, -24, -26, -28, -30, -32, -33, -35, -36, -38, -40, -41, -43, -45, -46, -48, -49, -51, -53, -55, -56, -58, -60, -62, -63, -65, -67, -69, -71, -73, -75, -77, -79, -80, -82, -84, -85, -87, -88, -90, -91, -92, -93, -94, -95, -96, -96, -97, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -91, -90, -88, -87, -85, -84, -82, -80, -78, -77, -75, -73, -71, -69, -67, -65, -63, -61, -59, -57, -55, -53, -51, -49, -47, -44, -42, -40, -38, -35, -33, -30, -27, -25, -22, -18, -16, -13, -10, -7, -4, -1, 1, 3, 6, 9, 12, 14, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 36, 38, 39, 41, 43, 44, 46, 48, 50, 51, 53, 55, 56, 58, 59, 61, 63, 65, 66, 68, 70, 72, 73, 75, 77, 79, 80, 82, 83, 85, 86, 88, 89, 91, 92, 93, 94, 94, 95, 96, 97, 97, 98, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 97, 96, 95, 95, 94, 93};
int myPWM_W_values[1800] = {-78, -80, -82, -83, -85, -87, -89, -90, -92, -93, -94, -95, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -85, -84, -82, -81, -80, -78, -77, -75, -74, -72, -71, -70, -68, -67, -65, -64, -62, -61, -59, -58, -56, -55, -53, -52, -50, -49, -47, -45, -43, -41, -39, -37, -34, -32, -29, -27, -25, -22, -19, -16, -13, -10, -7, -4, -1, 1, 4, 7, 10, 13, 16, 19, 21, 23, 26, 28, 31, 33, 35, 37, 39, 41, 42, 44, 46, 47, 49, 50, 52, 54, 55, 57, 59, 60, 62, 64, 65, 67, 69, 70, 72, 74, 75, 78, 80, 81, 83, 85, 87, 88, 90, 91, 93, 94, 95, 96, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 88, 87, 86, 84, 83, 81, 80, 78, 77, 76, 75, 73, 72, 70, 69, 68, 67, 65, 64, 63, 62, 60, 59, 58, 57, 56, 54, 53, 52, 50, 49, 47, 46, 44, 42, 40, 38, 36, 34, 31, 29, 26, 23, 21, 18, 16, 13, 11, 8, 5, 2, 0, -3, -7, -10, -13, -15, -17, -20, -22, -25, -27, -29, -31, -33, -35, -37, -39, -40, -42, -44, -45, -47, -49, -51, -52, -54, -56, -58, -59, -61, -62, -64, -66, -67, -68, -70, -72, -73, -75, -77, -78, -80, -82, -84, -86, -87, -89, -91, -92, -93, -94, -95, -96, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -96, -96, -95, -94, -93, -92, -91, -90, -89, -88, -87, -85, -84, -83, -81, -80, -78, -77, -75, -74, -72, -71, -69, -68, -66, -65, -63, -62, -60, -59, -57, -56, -55, -53, -52, -51, -49, -48, -46, -45, -43, -41, -40, -38, -36, -34, -32, -30, -28, -26, -24, -22, -19, -17, -14, -11, -9, -6, -3, 0, 2, 5, 8, 11, 14, 16, 19, 21, 24, 26, 28, 30, 32, 34, 36, 38, 40, 41, 43, 45, 47, 49, 50, 52, 54, 56, 57, 59, 60, 62, 63, 65, 67, 68, 70, 71, 73, 76, 78, 80, 82, 84, 86, 87, 89, 91, 92, 93, 94, 95, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 96, 96, 95, 94, 93, 92, 90, 89, 88, 86, 84, 83, 81, 79, 78, 76, 75, 73, 71, 70, 69, 67, 66, 65, 63, 62, 61, 59, 58, 57, 55, 54, 53, 51, 50, 49, 47, 45, 43, 42, 40, 38, 36, 34, 31, 29, 26, 24, 21, 19, 16, 13, 11, 8, 6, 3, 1, -1, -4, -6, -9, -12, -14, -17, -19, -21, -24, -26, -28, -29, -31, -33, -34, -36, -38, -39, -41, -42, -44, -45, -47, -49, -50, -52, -53, -55, -57, -58, -60, -62, -63, -65, -67, -69, -70, -73, -75, -77, -79, -81, -83, -84, -86, -88, -89, -91, -92, -93, -94, -95, -96, -97, -98, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -95, -95, -94, -93, -91, -90, -89, -88, -87, -85, -84, -83, -81, -80, -78, -77, -75, -74, -72, -71, -69, -68, -66, -65, -63, -62, -60, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -30, -27, -25, -23, -21, -18, -16, -13, -10, -7, -5, -2, 0, 4, 7, 10, 12, 15, 18, 20, 23, 25, 28, 30, 32, 34, 36, 38, 40, 41, 43, 44, 46, 48, 49, 50, 52, 54, 55, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 73, 74, 76, 78, 80, 81, 83, 85, 87, 89, 90, 92, 93, 94, 95, 96, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 86, 85, 84, 82, 80, 79, 77, 76, 74, 72, 71, 69, 68, 66, 65, 63, 62, 60, 59, 58, 56, 55, 54, 52, 51, 49, 48, 46, 45, 43, 41, 39, 37, 35, 32, 30, 27, 25, 22, 19, 16, 13, 10, 7, 4, 1, 0, -3, -6, -9, -12, -15, -17, -20, -23, -25, -27, -29, -31, -33, -35, -37, -39, -40, -42, -43, -45, -46, -48, -49, -51, -53, -54, -56, -58, -59, -60, -62, -63, -65, -66, -68, -70, -71, -72, -74, -76, -78, -80, -81, -83, -85, -87, -88, -90, -91, -92, -93, -94, -95, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -96, -96, -95, -94, -93, -92, -90, -89, -88, -87, -85, -84, -82, -81, -79, -78, -76, -75, -74, -73, -71, -70, -69, -67, -66, -65, -63, -62, -60, -59, -57, -56, -54, -52, -51, -49, -47, -45, -43, -41, -38, -36, -34, -32, -30, -28, -25, -23, -20, -17, -14, -11, -8, -5, -2, 0, 2, 5, 8, 11, 14, 17, 20, 23, 25, 28, 30, 32, 35, 37, 39, 41, 42, 44, 46, 48, 50, 51, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 79, 81, 83, 85, 86, 88, 90, 91, 92, 93, 94, 95, 96, 97, 97, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 95, 94, 93, 92, 91, 90, 89, 88, 86, 85, 84, 83, 81, 80, 79, 77, 76, 75, 73, 72, 70, 69, 67, 66, 64, 63, 61, 60, 58, 57, 55, 54, 52, 51, 49, 47, 46, 44, 42, 40, 37, 35, 33, 31, 28, 26, 24, 21, 19, 16, 13, 11, 8, 4, 2, 0, -3, -7, -10, -13, -16, -18, -21, -23, -26, -28, -30, -32, -34, -35, -37, -39, -41, -42, -44, -46, -48, -49, -51, -53, -54, -56, -58, -59, -61, -63, -64, -66, -68, -70, -71, -73, -75, -77, -79, -81, -82, -84, -86, -88, -89, -91, -92, -93, -94, -95, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -95, -94, -93, -92, -91, -90, -88, -87, -86, -84, -83, -82, -80, -79, -77, -76, -74, -73, -71, -70, -69, -68, -66, -65, -64, -62, -61, -60, -59, -58, -56, -55, -54, -53, -52, -50, -49, -47, -46, -44, -42, -40, -37, -35, -32, -30, -28, -25, -22, -19, -16, -13, -10, -7, -4, -1, 1, 4, 7, 10, 13, 15, 18, 20, 23, 25, 28, 30, 32, 34, 36, 38, 40, 41, 43, 45, 47, 48, 50, 52, 54, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 75, 77, 79, 81, 83, 85, 86, 88, 90, 91, 93, 94, 95, 96, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 98, 97, 96, 96, 95, 94, 93, 92, 91, 90, 89, 88, 86, 85, 84, 82, 81, 79, 78, 76, 75, 73, 72, 71, 69, 68, 66, 65, 63, 62, 61, 59, 58, 57, 55, 54, 52, 51, 49, 48, 46, 44, 43, 41, 39, 37, 35, 33, 31, 29, 27, 25, 23, 21, 19, 16, 14, 11, 9, 6, 4, 1, 0, -3, -5, -9, -12, -14, -17, -19, -21, -24, -26, -28, -30, -32, -34, -36, -38, -39, -41, -43, -45, -47, -48, -50, -52, -54, -56, -57, -59, -60, -62, -63, -65, -67, -69, -70, -72, -74, -76, -78, -80, -82, -84, -86, -87, -89, -91, -92, -93, -94, -95, -96, -97, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -97, -97, -96, -95, -94, -93, -92, -91, -89, -88, -87, -85, -84, -82, -80, -79, -77, -76, -74, -73, -71, -70, -68, -67, -65, -64, -63, -61, -60, -58, -57, -56, -54, -53, -51, -50, -48, -47, -46, -44, -42, -41, -38, -37, -35, -32, -30, -27, -25, -23, -20, -17, -14, -11, -9, -6, -3, 0, 2, 5, 8, 11, 14, 16, 18, 21, 23, 25, 27, 29, 31, 33, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 51, 52, 54, 55, 57, 59, 61, 62, 64, 66, 68, 69, 71, 73, 75, 77, 79, 81, 83, 84, 86, 88, 90, 91, 92, 93, 94, 95, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 89, 87, 86, 85, 83, 81, 80, 78, 77, 75, 74, 72, 70, 69, 67, 66, 64, 63, 61, 59, 58, 56, 55, 53, 52, 50, 49, 47, 45, 44, 42, 40, 38, 36, 35, 33, 31, 29, 27, 25, 23, 21, 18, 16, 13, 11, 8, 5, 3, 0, -2, -5, -8, -11, -13, -16, -19, -21, -23, -25, -28, -30, -32, -34, -36, -38, -40, -41, -43, -45, -46, -48, -50, -51, -53, -54, -56, -57, -59, -60, -61, -63, -64, -65, -67, -68, -69, -71, -72, -74, -75, -77, -79, -81, -83, -85, -86, -88, -90, -91, -92, -94, -94, -95, -96, -97, -98, -98, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -99, -98, -98, -98, -97, -96, -96, -95, -94, -93, -92, -90, -89, -88, -86, -85, -83, -82, -80, -79, -77, -76, -74, -73, -72, -70, -69, -68, -66, -65, -63, -62, -61, -59, -57, -56, -54, -53, -52, -50, -49, -47, -45, -43, -41, -39, -37, -34, -32, -30, -28, -25, -22, -19, -17, -14, -11, -8, -5, -2, 0, 3, 6, 9, 12, 15, 17, 20, 22, 25, 27, 30, 32, 34, 36, 38, 39, 41, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 58, 59, 61, 63, 64, 66, 67, 69, 71, 72, 74, 76, 78, 80, 81, 83, 85, 86, 88, 89, 91, 92, 93, 94, 95, 96, 96, 97, 98, 98, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 98, 98, 97, 97, 96, 95, 94, 93, 92, 91, 90, 89, 87, 86, 84, 83, 81, 80, 78, 77, 75, 74, 72, 71, 70, 68, 66, 65, 64, 62, 61, 60, 58, 56, 55, 53, 52, 50, 48, 47, 45, 43, 41, 40, 37, 35, 34, 31, 29, 27, 24, 22, 19, 17, 14, 11, 8, 5, 2, 0, -2, -5, -9, -12, -15, -17, -20, -22, -25, -27, -30, -32, -34, -36, -38, -40, -42, -44, -46, -48, -49, -51, -52, -54, -56, -57, -59, -61, -62, -63, -65, -66, -67, -69, -70, -72, -73, -75, -76};
//change these parameters according to your motor
int offset = 125;
int PhaseShift = 75;
extern "C" {
void CCU40_0_IRQHandler(void)
{ //read sensor
Tle5012Sensor.getAngleValue(angle);
//calculate speeds
if(abs(angle-last_angle) < 300){speed = angle-last_angle;}
//Position control (PD)
duty = (sollWinkel-angle)*KP; + speed*-KD;
if(duty > 0){PhaseShift = -75;} //direction
if(duty < 0){PhaseShift = 75;} //direction
duty = constrain(abs(duty),0,127); // only positive dutycycle 0-127
//drive motor according to lookup table
int angle_table = (angle+180)*5+PhaseShift+offset;
if (angle_table >= 1800) {angle_table -=1800;}
if (angle_table < 0000) {angle_table +=1800;}
pwmOne = myPWM_U_values[angle_table];
pwmTwo = myPWM_V_values[angle_table];
pwmThree = myPWM_W_values[angle_table];
analogWrite(U, 127 + duty*pwmOne/100);
analogWrite(V, 127 + duty*pwmTwo/100);
analogWrite(W, 127 + duty*pwmThree/100);
}
}
void setup() {
Serial.begin(115200);
Tle5012Sensor.begin();
pinMode(PWS, OUTPUT); digitalWrite(PWS, LOW); // turn CO2 sensor into PWM mode
pinMode(PWD, INPUT);
pinMode(U, OUTPUT); setAnalogWriteFrequency(U,20000);
pinMode(V, OUTPUT); setAnalogWriteFrequency(V,20000);
pinMode(W, OUTPUT); setAnalogWriteFrequency(W,20000);
pinMode(EN_U, OUTPUT); digitalWrite(EN_U, HIGH);
pinMode(EN_V, OUTPUT); digitalWrite(EN_V, HIGH);
pinMode(EN_W, OUTPUT); digitalWrite(EN_W, HIGH);
//Setup Interrupt settings
XMC_CCU4_SLICE_COMPARE_CONFIG_t pwm_config = {0};
pwm_config.passive_level = XMC_CCU4_SLICE_OUTPUT_PASSIVE_LEVEL_HIGH;
pwm_config.prescaler_initval = XMC_CCU4_SLICE_PRESCALER_128;
//Setup interrupt
XMC_CCU4_Init(CCU40, XMC_CCU4_SLICE_MCMS_ACTION_TRANSFER_PR_CR);
XMC_CCU4_SLICE_CompareInit(CCU40_CC43, &pwm_config);
XMC_CCU4_EnableClock(CCU40, 3);
XMC_CCU4_SLICE_SetTimerPeriodMatch(CCU40_CC43, 200); // Adjust last Value or Prescaler
/* Enable compare match and period match events */
XMC_CCU4_SLICE_EnableEvent(CCU40_CC43, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH);
/* Connect period match event to SR0 */
XMC_CCU4_SLICE_SetInterruptNode(CCU40_CC43, XMC_CCU4_SLICE_IRQ_ID_PERIOD_MATCH, XMC_CCU4_SLICE_SR_ID_0);
/* Configure NVIC */
/* Set priority */
NVIC_SetPriority(CCU40_0_IRQn, 10);
/* Enable IRQ */
NVIC_EnableIRQ(CCU40_0_IRQn);
XMC_CCU4_EnableShadowTransfer(CCU40, (CCU4_GCSS_S0SE_Msk << (4 * 3)));
XMC_CCU4_SLICE_StartTimer(CCU40_CC43);
}
void loop() {
CO2 = pulseIn(PWD, HIGH,70000000);
Serial.print("CO2:"); Serial.println(CO2);
if(CO2 > 1400){//bird dead
sollWinkel = -40.0; //pushing bird down
delay(10);
KP = 0; //no active drive to a specific position
KD = -100; //damping for better look of the fall
}
else {//bird alive
KP = 10; //controller settings to move and hold bird in upright position
KD = -40;
sollWinkel = 0.0;
}
}
Comments