Red Ball Tracking Car

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));
    }

FarukBasaran:
myservo.write(map(average, 91,159, 130, 179)); Can i use mapping like that?

If you want permission - you have it.
If you want to know whether it compiles - I don't see why it wouldn't, but you have a compiler so you can find out for yourself.
If you want to know whether it will achieve what you want - it looks like a reasonable thing to do if your average value is in the range 91 - 159 and you want to convert it to a servo position in the range 130 - 179. Whether that is a sensible thing to do in your application, only you can say.

Well,the thing is myservo.write(map(average, 91,159, 130, 179)); 130,179 part is not the position,since i am going to be using continuous rotation servos,they are speed values.Yes it compiles but i am not sure will it be healty to do,and i want to do if is there anyway i can do what that line does

but i am not sure will it be healty to do,and i want to do if is there anyway i can do what that line does

I think you've already answered your own questions. Don't embed function calls like that. Save the results of one function call in a variable that is passed to another function. That way, you can Serial.print() the intermediate value(s) to see whether they are reasonable.

Typically, continuous rotation servos are commanded using writeMicroseconds(), instead of write(). For writeMicroseconds(), the values in the map function are all wrong.

You need to decide what the value in average needs to cause the servos to do. Should an increase in average make the servo turn faster or slower? How much should the speed change for a delta of 1 in average? Only you can answer these questions.

I did use the myservo.write() command because there was an explanation in servo library.

Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with 0 being full-speed in one direction, 180 being full speed in the other, and a value near 90 being no movement).

Thought this would have work to do the job,well assuming it wont do what i want,Can i use writeMicroSeconds(val) like that?
val being between 1500 and 2250 and an equalization between the average value(which is between 90,159) sorts out the changes in val.

Can i use writeMicroSeconds(val) like that?

Yes, and it gives you finer control over the speed of the servo(s).

I see thanks! How about the second question,will i have to measure the movements of servos so how servos behave when data.pixels get smaller and data.mx changes in time(i mean movement in x and z direction).

will i have to measure the movements of servos so how servos behave when data.pixels get smaller and data.mx changes in time(i mean movement in x and z direction).

Yes, you will have to measure and experiment.

Allright,Thanks a lot!!