Alright.
So i have been playing with my arduino uno r3 from time to time for quite some time now and manage to get basic things done with it (nothing fancy). Lately i wanted to do something a bit more advanced but can't seem to quite get it done. I have a GY-521 accelerometer and a L298N H-Bridge. Hardware-wise everything is connected and operational. I also found an awesome script that detects angle and accelerometer changes and applies the kalman filter for a better result. My question is how can use that filtered result to control a motor's speed and direction? like if (xangle > 0) {digitalWrite (IN1, LOW); digitalWrite (IN2, HIGH); analogWrite (ENA, i);} etc...
I thought that the variable containing the filtered value i was looking for was kalAngleX but i can't seem to use it. Is that even what i am looking for?
Anyway thank's in advance for your time.
cyberlord64:
My question is how can use that filtered result to control a motor's speed and direction? like if (xangle > 0) {digitalWrite (IN1, LOW); digitalWrite (IN2, HIGH); analogWrite (ENA, i);} etc...
You need to understand what relationship you need between the input signal from the sensor, and the output signal you need to supply to the motor driver. The relationship would be determined by what your solution is intended to achieve - which you haven't told us.
The result i am trying to achieve is to control the speed of the motor based on the angle change. For example if the angle is 0 the motor stays still. if the angle becomes 45 degrees the motor rotates at half speed. at 90 degrees full speed etc.
You need to understand what relationship you need between the input signal from the sensor, and the output signal you need to supply to the motor driver.
I don't think i follow you here... what do you mean?
Putting the motor to one side (in your mind, at least) have you a variable that contains the angle from the accelerometer after all the filtering and stuff has been done. Can you use Serial.print() to show the content of that variable and does it show the correct values.
Once that part works properly, you have another mini project to think of. Now put the accelerometer to one side and think how to cause the motor to respond to the value in your variable. For test purposes you could just hard-code a value in the variable.
If it was my project my loop() would probably look like this
cyberlord64:
well the lines that eventually print the final result are these
Serial.print(F("#KAL:")); //Kalman Filtered angle
pad(kalAngleX);
The result is indeed accurate. (there is a margin of error of course within 0.5 - 1 degree)
Can i use kalAngleX directly? Is the Serial.print(F()); and pad(); of any significance?
The Serial.print line just produces the text "#KAL:" and I have no idea what pad() does but I guess it has something to do with making the printing look nice.
See what happens with Serial.println(kalAngleX);
The beauty of the Arduino is that it is so easy to try things to see what happens.
cyberlord64:
well the lines that eventually print the final result are these
Serial.print(F("#KAL:")); //Kalman Filtered angle
pad(kalAngleX);
The result is indeed accurate. (there is a margin of error of course within 0.5 - 1 degree)
Can i use kalAngleX directly? Is the Serial.print(F()); and pad(); of any significance?
Well assuming you are going to use the analogWrite() function to drive the motor driver to the desired speed, you just need to scale the angle range (0-90 degrees) to the 0-255 pwm output range. So the basic conversion can be done using the map() function as so:
int outputSpeed;
int angle;
outputSpeed = map(angle,0,90,0,255);
analogWrite(pin#, outputSpeed);