Dezibot 4
motion_correction.ino
Go to the documentation of this file.
1 #include "Dezibot.h"
2 Dezibot dezibot = Dezibot();
3 #define maxDuty 6000
4 #define minDuty 3500
5 #define baseDuty 3900
6 void setup() {
7  // put your setup code here, to run once:
8  dezibot.begin();
9  Serial.begin(115200);
10  //dezibot.motion.move();
11  //analogWrite(11, 2000);
12  //analogWrite(12, 2000);
13 }
14 uint16_t leftDuty= baseDuty;
15 uint16_t rightDuty = baseDuty;
16 unsigned long previous = millis();
17 unsigned long current = millis();
18 long averageZRotation = 0;
19 long averageXAccel = 0;
20 int left=0;
21 int right=0;
22 int difference = 0;
23 int changerate = 0;
24 int interval = 40;
25 int threshold = 150;
26 int resultcounter = 0;
27 void loop() {
28  // put your main code here, to run repeatedly:
29  current = millis();
30  IMUResult result = dezibot.motion.detection.getRotation();
31  averageXAccel += dezibot.motion.detection.getAcceleration().x;
32  averageZRotation += result.z;
33  //Serial.println(result.z);
34  if (result.z > threshold){
35  right++;
36  } else if(result.z < -threshold) {
37  left++;
38  }
39  resultcounter++;
40  if ((current - previous) > interval){
41  //averageZRotation = averageZRotation / resultcounter;
42  difference = abs(left-right);
43  if (difference>25){
44  changerate = 100;
45  } else if(difference>20){
46  changerate = 50;
47  } else if(difference >15){
48  changerate = 30;
49  } else if(difference > 10){
50  changerate = 10;
51  } else{
52  changerate = 5;
53  }
54  //Serial.print("Z:");
55  //Serial.println(averageZRotation);
56  dezibot.display.clearDisplay();
57  dezibot.display.setCursor(0, 0);
58  dezibot.display.setTextColor(WHITE);
59  dezibot.display.setTextSize(2); // Draw 2X-scale text
60  dezibot.display.println(averageZRotation);
61  //dezibot.display.println(resultcounter);
62  dezibot.display.print(left);
63  dezibot.display.print(" ");
64  dezibot.display.println(right);
65  if(left>right){ //rotates anticlock
66  leftDuty+=changerate;
67  rightDuty-=changerate;
68  } else if(left<right){
69  leftDuty-=changerate;
70  rightDuty+=changerate;
71  }
72  dezibot.motion.left.setSpeed(leftDuty);
73  dezibot.motion.right.setSpeed(rightDuty);
74  dezibot.display.println(leftDuty);
75  dezibot.display.println(rightDuty);
76  dezibot.display.display();
77 
78 
79  averageZRotation = 0;
80  averageXAccel = 0;
81  resultcounter = 0;
82  left = 0;
83  right = 0;
84  previous = current;
85  }
86 }