Statistics
| Branch: | Tag: | Revision:

amiro-os / devices / DiWheelDrive / linefollow2.cpp @ b8085493

History | View | Annotate | Download (7.887 KB)

1
#include "global.hpp"
2
#include "linefollow2.hpp" 
3
#include <cmath>
4

    
5

    
6
void LineFollow::printSensorData(){
7
    chprintf((BaseSequentialStream*) &SD1, "Test!");
8
}
9

    
10
// void LineFollow::followLine(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
11
    
12
//     chprintf((BaseSequentialStream*) &SD1, "SP: %d,\n", SetPoint);
13
    
14
//     // chprintf((BaseSequentialStream*) &SD1, "Proximity: WL:0x%04X FL:0x%04X FR:0x%04X WR:0x%04X\n",
15
//     //                 vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_LEFT],
16
//     //                 vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT],
17
//     //                 vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT],
18
//     //                 vcnl4020Proximity[constants::DiWheelDrive::PROX_WHEEL_RIGHT]);
19
//     // global->motorcontrol.printGains();
20
//     // chprintf((BaseSequentialStream*) &SD1, "Speed -- Left: %d, Right: %d\n", global->motorcontrol.getCurrentRPMLeft(), global->motorcontrol.getCurrentRPMRight());
21

    
22

    
23
//     // float speedL = global->motorcontrol.getCurrentRPMLeft();
24
//     // float speedR = global->motorcontrol.getCurrentRPMRight();
25
//     // chprintf((BaseSequentialStream*) &SD1, "After motor request SP: %f,\n", SetPoint);
26
//     // Process value
27
//     float processV = static_cast< float >((vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] + vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT]));
28
//     // chprintf((BaseSequentialStream*) &SD1, "PV: %f,\n", processV);
29
//     // chprintf((BaseSequentialStream*) &SD1, "After PV SP: %f,\n", SetPoint);
30
//     float error = SetPoint - processV;
31
//     float d_term = old_error - error;
32
//     // chprintf((BaseSequentialStream*) &SD1, "After Error SP: %f,\n", SetPoint);
33
//     // chprintf((BaseSequentialStream*) &SD1, "Error: %f,\n", error);
34
//     acc_sum = 0.5 * acc_sum + error;
35
//     int correctionSpeed = static_cast< int >(Kp * error + Ki*acc_sum + Kd*d_term);
36
//     old_error = error;
37
//     chprintf((BaseSequentialStream*) &SD1, "Error: %f,\n", error);
38
//     chprintf((BaseSequentialStream*) &SD1, "Dterm: %f,\n", d_term);
39
//     chprintf((BaseSequentialStream*) &SD1, "Iterm: %f,\n", acc_sum);
40
//     chprintf((BaseSequentialStream*) &SD1, "New Speed: %d,\n", correctionSpeed);
41
//     // chprintf((BaseSequentialStream*) &SD1, "New Speed: %f, Sum: %f, SP: %f, processV: %f, K_p: %f, K_i: %f \n", correctionSpeed, acc_sum, SetPoint, processV, Kp, Ki);
42

    
43
//     // int forward = 15;
44
//     int speedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL] - correctionSpeed;
45
//     int speedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL] + correctionSpeed;
46

    
47
//     // if (l_speed )
48

    
49
//     rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = speedL;
50
//     rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = speedR;
51

    
52
//     chprintf((BaseSequentialStream*) &SD1, "Speed L: %d, R: %d\n", speedL, speedR);
53

    
54
// }
55

    
56

    
57
void LineFollow::stableFollow(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
58
    int targetSensor = 0x38;
59
    int actualSensorL = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] ;
60
    int actualSensorR = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT] ;
61
    int targetSpeedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL];
62
    int targetSpeedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL];
63

    
64

    
65
    int diff = actualSensorR - actualSensorL; 
66
    int error = targetSensor - (actualSensorL + actualSensorR);
67

    
68
    accSum += error;
69
    int dTerm = error - oldError;
70

    
71
    if (diff > biggestDiff){
72
        biggestDiff = diff;
73
    }
74
    int correctionSpeed = (int) (Kp * error + Ki * accSum + Kd * dTerm);   
75
    chprintf((BaseSequentialStream*) &SD1, "Correction Speed: %d\n", correctionSpeed);
76
    rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = targetSpeedL + correctionSpeed;
77
    rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = targetSpeedR - correctionSpeed;
78

    
79
    chprintf((BaseSequentialStream*) &SD1, "Diff: %d, Biggest: %d\n", correctionSpeed, biggestDiff);
80

    
81
}
82

    
83

    
84
// void LineFollow::followLineSeperateSensors2(int vcnl4020Proximity[4], int (&rpmFuzzyCtrl)[2], Global *global){
85
    
86
//     chprintf((BaseSequentialStream*) &SD1, "SP: %d,\n", SetPoint);
87
//     int targetSensorL = 0x10;
88
//     int targetSensorR = 0x28;
89
    
90
//     float actualSpeedL = 20;
91
//     float actualSpeedR = 20;
92

    
93
//     // if(actualSpeedL == 0){
94
//     //     actualSpeedL = 1;
95
//     // }
96
//     // if(actualSpeedR == 0){
97
//     //     actualSpeedR = 1;
98
//     // }
99

    
100
//     // Shift sensor values to prevent overflow in following calculation
101
//     int actualSensorL = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] >> 8;
102
//     int actualSensorR = vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT] >> 8;
103
//     chprintf((BaseSequentialStream*) &SD1, "Sensor L: %d, R: %d\n", actualSensorL, actualSensorR);
104

    
105
//     int targetSpeedL = global->rpmForward[constants::DiWheelDrive::LEFT_WHEEL];
106
//     int targetSpeedR = global->rpmForward[constants::DiWheelDrive::RIGHT_WHEEL];
107

    
108
//     int setPointL = targetSensorL;
109
//     int setPointR = targetSensorR;
110
//     chprintf((BaseSequentialStream*) &SD1, "SetPoint L: %d, R: %d\n",setPointL, setPointR );
111

    
112
//     int processValueL =  actualSensorL;
113
//     int processValueR =  actualSensorR;
114
//     chprintf((BaseSequentialStream*) &SD1, "ProcessValue L: %d, R: %d\n",processValueL, processValueR );
115

    
116
//     int errorL = setPointL - processValueL;
117
//     int errorR = setPointR - processValueR;
118

    
119
//     // This will howfully decrease the overall speed when sensors deviate much 
120
//         // errorL /= targetSensorL+actualSensorL;
121
//         // errorR /= targetSensorR+actualSensorR;
122
//     chprintf((BaseSequentialStream*) &SD1, "Error L: %d, R: %d\n",errorL, errorR);
123

    
124
//     // int newSpeedL =  
125
//     rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = errorL;
126
//     rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = errorR;
127
    
128
//     int correction_speedL = (int) (Kp * errorL);   
129
//     int correction_speedR = (int) (Kp * errorR);   
130
//     chprintf((BaseSequentialStream*) &SD1, "Speed L: %d, R: %d\n",correction_speedL, correction_speedR);
131

    
132
//     // // chprintf((BaseSequentialStream*) &SD1, "After motor request SP: %f,\n", SetPoint);
133
//     // // Process value
134
//     // float processV = static_cast< float >((vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_LEFT] + vcnl4020Proximity[constants::DiWheelDrive::PROX_FRONT_RIGHT]));
135
//     // // chprintf((BaseSequentialStream*) &SD1, "PV: %f,\n", processV);
136
//     // // chprintf((BaseSequentialStream*) &SD1, "After PV SP: %f,\n", SetPoint);
137
//     // float error = SetPoint - processV;
138
//     // float d_term = old_error - error;
139
//     // // chprintf((BaseSequentialStream*) &SD1, "After Error SP: %f,\n", SetPoint);
140
//     // // chprintf((BaseSequentialStream*) &SD1, "Error: %f,\n", error);
141
//     // acc_sum = 0.5 * acc_sum + error;
142
//     // int new_speed = static_cast< int >(Kp * error + Ki*acc_sum + Kd*d_term);
143
//     // old_error = error;
144
//     // chprintf((BaseSequentialStream*) &SD1, "Error: %f,\n", error);
145
//     // chprintf((BaseSequentialStream*) &SD1, "Dterm: %f,\n", d_term);
146
//     // chprintf((BaseSequentialStream*) &SD1, "Iterm: %f,\n", acc_sum);
147
//     // chprintf((BaseSequentialStream*) &SD1, "New Speed: %d,\n", new_speed);
148
//     // // chprintf((BaseSequentialStream*) &SD1, "New Speed: %f, Sum: %f, SP: %f, processV: %f, K_p: %f, K_i: %f \n", new_speed, acc_sum, SetPoint, processV, Kp, Ki);
149

    
150
//     // // int forward = 15;
151
//     // // int l_speed = forward - new_speed;
152
//     // // int r_speed = forward + new_speed;
153

    
154
//     // // if (l_speed )
155

    
156
//     rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL] = targetSpeedL + correction_speedL;
157
//     rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL] = targetSpeedR + correction_speedR;
158

    
159
//     // chprintf((BaseSequentialStream*) &SD1, "Speed L: %d, R: %d\n", rpmFuzzyCtrl[constants::DiWheelDrive::LEFT_WHEEL], rpmFuzzyCtrl[constants::DiWheelDrive::RIGHT_WHEEL]);
160

    
161
// }