I am new to the world of robotics, and I'm preparing a new project with my students. Our goal is to make a self driving robot that can read colored signs from the road in order to turn right or left a specific angle. For example, if the color sensor 1 detects red color, and at the same time the color sensor 2 detects blue color, the robot must turn 90 degrees right. What's the best programming approach to do this? Consider that the total number of combinations could be up to 49 since each color sensor can read 7 colors.

  • If you've already tried anything, then you should edit your question to explain what you've already tried. Consider sharing your work-in-progress code. Mar 6 '19 at 18:15
  • I am still working on this project, i will be glad to share it's code when its done or partially done. Thanks for interest
    – Huss ibra
    Mar 6 '19 at 19:25

There will certainly be some mechanical and robotics questions that you'll need to figure out, such as where on the car to position the sensors, and how to ensure that the car is reliably reading both colors at every intersection. (Which brings up contextual questions like "are there discrete intersections?")

From a programming perspective, 49 possible options is kindof a middle ground.

You could have a systematic representation. For example, each color could map to a digit, and so the pair represents a two digit base-7 number of 2π/49 radians (where 0 means go back the way you came). I don't actually suggest that particular scheme, but something like that could work quite well.

You could just have a lookup table. Or a lookup table of lookup tables. In a C-like language that would look something like

table = [
  'red':  ['red': -90, 'blue': -45],
  'blue': ['red': +90, 'blue': +45]
turn_degree = table[first_color][second_color];

I would call that the "normal" approach, although you're getting toward the upper size of the table one might really want.

You could also treat one color as a modifier for the first:

first_table = ['red': -90, 'blue': +90];
second_table = ['red': -45, 'blue': +45];
turn_degree = first_table[first_color] + second_table[second_color];

This is secretly the same as that first option I gave (base7), but more flexible, easier to read, and easier to explain to kids. And you could use multiplication instead of addition if you wanted.

A robotics consideration: Will the car always correctly sense which color is the "first" color?

If not, then there's actually only 28 (21?) possible combinations. You can still use any of the above methods, but additionally you'll define an ordering of the colors (so for example, you'll just decide that green is "less than" blue). Then your "first" color will always be the lesser of the two colors, regardless of which sensor sensed what.

But seriously, how should you do this in a classroom?

  • If either sensor sees red, the car will turn left.
  • If either sensor sees blue, the car will turn right.
  • If either sensor sees yellow, the car will turn 15°.
  • If either sensor sees green, the car will turn 45°.
  • If either sensor sees orange, the car will turn 60°.
  • If either sensor sees purple, the car will turn 90°.
  • If either sensor sees black, the car will turn 135°.
  • If the car doesn't see two colors, or if the colors don't clearly say what to do, then the car just keeps going forward.
first = get_sensor_one();
second = get_sensor_two();
directions = ['red': -1, 'blue': 1];
degrees = ['yellow': 15, 'green': 45, 'orange': 60, 'purple': 90, 'black': 135];
if(contains(directions, first)){
  direction_color = first;
  degree_color = second;
} else {
  direction_color = second;
  degree_color = first;
if(!contains(directions, direction_color) || !contains(degrees, degree_color)){
} else {
  turn(degrees[degree_color] * direction[direction_color]);
  • Thanks alot for your professional approach of my case, for me i was thinking in a way that the two sensors will see different colors at the same time, like there is a line cutting the road which is made up of two distinct colors, for example brown at one end and red on the other. My question is that how to tell the robot to do a certain task when he detects this combination, in my project the task is that the robot must turn right 120 degrees for brown and red.
    – Huss ibra
    Mar 6 '19 at 17:46
  • I was thinking in a way that if cs1 = brown , and cs2 =red, go 120 right.
    – Huss ibra
    Mar 6 '19 at 17:47
  • Its going to be nested if conditions i think, what do you think?
    – Huss ibra
    Mar 6 '19 at 17:48
  • Nesting ifs is fine, but if you find yourself nesting similar if statements then that suggest there may be a better way. Sequential ifs, elses, and else ifs will at least keep your code flat. switch statements usually aren't worth the bother, but some people like them. If you can avoid ifs etc ("branching") altogether, then you probably should. Mar 6 '19 at 18:14

Your Answer

