Tatrabot learns

From RoboWiki
Jump to: navigation, search

We would like to make the Tatrabot learn.


#include <stdlib.h>
#include "tatrabot.h"
#include "demo.h"

int ml[10];
int mr[10];
int mt[10];

void generate_movement(void)
{
  for (int i = 0; i < 10; i++)
  {
    ml[i] = rand() % 20000 - 10000;
    mr[i] = rand() % 20000 - 10000;
    mt[i] = rand() % 900 + 100;
  }
}

void demonstrate_movement(void)
{
  for (int i = 0; i < 10; i++)
  {
    setMotor(1, ml[i]);
    setMotor(2, mr[i]);
    chThdSleepMilliseconds(mt[i]);
  }
  setMotor(3, 0);
}

int mind, oldl, oldr, oldt;

void generate_modification(void)
{
  mind = rand() % 10;
  oldl = ml[mind];
  oldr = mr[mind];
  oldt = mt[mind];
  ml[mind] = rand() % 20000 - 10000;
  mr[mind] = rand() % 20000 - 10000;
  mt[mind] = rand() % 900 + 100;
}

int acquire_feedback(void)
{
  int positive_answer, negative_answer, finish_answer;
  do
  {
    senseTemperatureAndPressure(0);
    positive_answer = (temperature > 280);
    negative_answer = (distance > 1500);
    finish_answer = senseButton(3);

    sound(2000);
    chThdSleepMilliseconds(30);
    nosound();
    chThdSleepMilliseconds(50);
  } while (!positive_answer && !negative_answer && !finish_answer);

  if (positive_answer) return 1;
  if (negative_answer) return 0;
  return -1;
}

void revert_modification(void)
{
  ml[mind] = oldl;
  mr[mind] = oldr;
  mt[mind] = oldt;
}

void beep(void)
{
  sound(440);
  chThdSleepMilliseconds(200);
  nosound();
}

void learn (void)
{
  int feedback;
  generate_movement();
  demonstrate_movement();
  beep();
  chThdSleepMilliseconds(7000);
  do
  {
    generate_modification();
    demonstrate_movement();

    feedback = acquire_feedback();
    if (feedback == 0) revert_modification();
  } while (feedback >= 0);
}

int main(void)
{
  tatrabotInit();
  srand(distance);

  chprintf(PC, " Hello  hello, this is Tatrabot speaking...\n\r");
  chprintf(BT, " Hello Tatrabot on BT!\n");

  demo();
  learn();

  while(true)
  {
    setLED(0, 1);
    chThdSleepMilliseconds(1000);
    setLED(0, 0);
    chThdSleepMilliseconds(1000);
  }
}