Hi, i have a cmucam4,arduino mega and 2 continuous rotation servos,i am working on a red ball tracking robot,but i am stuck at developing the algorithm.I can reach CmuCam4's tracking data structure which gives me midmassx,midmassy,the pixels percentage that i am going to track.I do have a couple of questions:
1-) can i use the map function without using analogread(); ,i do want to use data.mx instead of it(which can have values between 0 and 159) ?
2-)Supposing the redball is at 120th pixels,meaning data.mx gives me 120 input to map,and the percentage of pixels is getting smaller(for example from %30 to %20 comparing to total pixels). How can i develop an algorithm that will control the servos,to move forward and reverse at the same time?
Should i just have the best guess about it compared to servos movement per time,or is there any algorithm i can develop in to?
I am adding the code that i have written so far(will be developing it,its just a model),assuming you may not be familiar with the cmucam4 library.
And here is the line i am asking about in the first question: myservo.write(map(average, 91,159, 130, 179)); Can i use mapping like that?
Thanks a lot for your help.
#include <CMUcam4.h>
#include <CMUcom4.h>
#include <Servo.h>
#define RED_MIN 120
#define RED_MAX 200
#define GREEN_MIN 60
#define GREEN_MAX 80
#define BLUE_MIN 50
#define BLUE_MAX 70
#define LED_BLINK 5 // 5 Hz
#define WAIT_TIME 5000 // 5 seconds
#define LED_DELAY 10
#define DELAY_TIME 5000
CMUcam4 cam(CMUCOM4_SERIAL);
Servo myservo; // create servo object to control a servo
void setup()
{
cam.begin();
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Wait for auto gain and auto white balance to run.
cam.LEDOn(LED_BLINK);
delay(WAIT_TIME);
// Turn auto gain and auto white balance off.
cam.autoGainControl(true);
cam.autoWhiteBalance(true);
cam.LEDOn(LED_DELAY);
delay(DELAY_TIME);
cam.autoGainControl(false);
cam.autoWhiteBalance(false);
cam.noiseFilter(5);
cam.LEDOn(CMUCAM4_LED_ON);
}
void loop()
{
CMUcam4_tracking_data_t data;
cam.trackColor(RED_MIN, RED_MAX, GREEN_MIN, GREEN_MAX, BLUE_MIN, BLUE_MAX);
int average;
int learning_rate = 50; // Try different values for this... between 0 and 100.
average = ((((100 - learning_rate) * data.mx) + (learning_rate * data.mx)) / 100);
for(;;)
{
cam.getTypeTDataPacket(&data); // Get a tracking packet.
if(average>>90)
{
myservo.write(map(average, 91,159, 130, 179)); // sets the servo position according to the scaled value
}
else if(average<<90 && average >> 70)
{
myservo.write(map(average, 70,90, 130,130));
}