France
Asked — Edited

Script Using Multi Color For Personality Generator

edited with the full code

Hi,

Here is a small script I have written, using the new Multi Color tracking feature.

The script tracks Multi Colors (here red, green and blue) and each time a color is tracked, the script update a textfile corresponding to the color and increases by 1 the number inside the file. So, the more a color is seen, higher the number is.

Then the script compares the number of the color tracked to other colors and if the color is the most seen, the bot says he doesn't like it. At contrary, if the color tracked is the less seen, the bot says he does like it.

This way, the bot "learns" during time which color he prefers and which color he dislikes... and say it!


# This script is for Personality Generator:
# It tracks Multi Colors (here red, green and blue) and each time a color is tracked, the script update a file corresponding to the color
# and increases by 1 the number inside the file. So, the more a color is seen, higher the number is.
# Then the script compares the number of the color tracked to other colors and if the color is the most seen, the bot says he doesn't like it.
# At contrary, if the color tracked is the less seen, the bot says he does like it.
# This way, the bot "learns" during time which color he prefers and which color he dislike... and say it!
# There is a countdown in the script to limit color tracking to 30s.


# Close the "color" text files, to make them accessible.
FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Red.txt")
FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Green.txt")
FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Blue.txt")

# Enable Multi Color tracking
ControlCommand("camera", CameraMultiColorTrackingEnable)

# Obtain a number from the "color" text files, representing how many time each color has been seen, and use it as variable
$red = FileReadAll("C:\Users\fredebec\Documents\EZ-Builder\Red.txt")
$green = FileReadAll("C:\Users\fredebec\Documents\EZ-Builder\Green.txt")
$blue = FileReadAll("C:\Users\fredebec\Documents\EZ-Builder\Blue.txt")

# variables for Object color and for a countdown
$CameraObjectColor = ""
$countdown = 30

# show in the debug window each color value
Print("Red:" $red)
Print("Green:" $green)
Print("Blue:" $blue)

# Script

:LOOP
# RED
IF ($CameraObjectColor = "Red")
  IF ($red > $green AND $red > $blue)
    Say ("Oh, red! I don't like red!")
    Sleep (1000)
  ELSEIF ($red < $green AND $red < $blue )
    Say ("Oh, red! I like red!")
    Sleep (1000)
  ELSE 
    Say ("It is red")
    Sleep (1000)
  ENDIF 
  $red = $red + 1
  FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Red.txt")
  FileDelete("C:\Users\fredebec\Documents\EZ-Builder\Red.txt")
  FileWrite("C:\Users\fredebec\Documents\EZ-Builder\Red.txt", $red)
  ControlCommand("camera", CameraMultiColorTrackingDisable)
  CC("Script Manager", scriptstart, "Neutral position")
  halt()
  
# GREEN
ELSEIF ($CameraObjectColor = "Green")
  IF ($green > $red AND $green > $blue)
    Say ("Oh, green! I don't like green!")
    Sleep (1000)
  ELSEIF ($green < $red AND $green < $blue)
    Say ("Oh, green! I like green!")
    Sleep (1000)
  ELSE 
    Say ("It is green")
    Sleep(1000)
  ENDIF 
  $green = $green + 1
  FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Green.txt")
  FileDelete("C:\Users\fredebec\Documents\EZ-Builder\Green.txt")
  FileWrite("C:\Users\fredebec\Documents\EZ-Builder\Green.txt", $green)
  ControlCommand("camera", CameraMultiColorTrackingDisable)
  CC("Script Manager", scriptstart, "Neutral position")
  halt()

# BLUE 
ELSEIF ($CameraObjectColor = "Blue")
  IF ($blue > $red AND $blue > $green)
    Say ("Oh, blue! I don't like blue!")
    Sleep (1000)
  ELSEIF ($blue < $red AND $blue < $green)
    Say ("Oh, blue! I like blue!")
    Sleep (1000)
  ELSE
    Say ("It is blue")
    Sleep (1000)
  ENDIF 
  $blue = $blue + 1
  FileReadClose("C:\Users\fredebec\Documents\EZ-Builder\Blue.txt")
  FileDelete("C:\Users\fredebec\Documents\EZ-Builder\Blue.txt")
  FileWrite("C:\Users\fredebec\Documents\EZ-Builder\Blue.txt", $blue)
  ControlCommand("camera", CameraMultiColorTrackingDisable)
  CC("Script Manager", scriptstart, "Neutral position")
  halt()
ENDIF 
$countdown = $countdown-1
print($countdown)
IF ($countdown = 0)
  ControlCommand("camera", CameraMultiColorTrackingDisable)
  halt()
ENDIF 
Sleep(1000)
Goto(Loop)

Some things I don't find very elegant in my script are:

  1. I have to have a different file for each color
  2. By using FileRead and FileWrite, I have to close, delete, and then re-write the file each time, in order to increase by 1 the number inside.

So the script is certainly perfectible...


ARC Pro

Upgrade to ARC Pro

Don't limit your robot's potential – subscribe to ARC Pro and transform it into a dynamic, intelligent machine.

United Kingdom
#1  

FileWrite and FileWriteLine are both on my list of commands to play with although I personally prefer to keep everything inside ARC and only use variables. Not much you can do about it if you want it to remember the counts after a reboot though.

Not strictly all within ARC but you could use the HTTPGET command to read and write the counts to a database via a simple PHP page. But that would require a database setup, web server etc. etc. etc. (I have all that anyway so is no problem for me but I assume many don't).

Is some of the script missing or been altered by the forum formatting? Parts of it don't seem to look right to me (and also it finds red and adds to the $blue value).

#2  

Oops! It seems that the copy/paste procedure has eaten half of the script... I have edited the first post with the full code blush

Indeed, using a database certainly would be more elegant... but sadly, I have no skills in in these things.