<commit_before>/**
 * @file main.cpp
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */

#include "NaoController.h"

#include <glib.h>
#include <glib-object.h>
#include <signal.h>
//#include <rttools/rtthread.h>


using namespace naoth;

gint framesSinceCognitionLastSeen;

void got_signal(int t)
{

  if(t == SIGTERM) {
    std::cout << "shutdown requested by kill signal" << t << std::endl;
  } else if(t == SIGSEGV) {
    std::cerr << "SEGMENTATION FAULT" << std::endl;
  } else {
    std::cerr << "catched unknown signal " << t << std::endl;
  }

  std::cout << "dumping traces" << std::endl;
  Trace::getInstance().dump();
  StopwatchManager::getInstance().dump("cognition");

  std::cout << "syncing file system..." ;
  sync();
  std::cout << " finished." << std::endl;

  exit(0);
}//end got_signal


// a semaphore for sychronization with the DCM
sem_t* dcm_sem = SEM_FAILED;


/* 
// Just some experiments with the RT-Threads
// not used yet
class TestThread : public RtThread
{
	public:
    TestThread(){}
    ~TestThread(){}
    NaoController* theController;

    virtual void *execute()
    {
      Stopwatch stopwatch;
      while(true)
      {
        theController->callMotion();
    
        if(sem_wait(dcm_sem) == -1)
        {
          cerr << "lock errno: " << errno << endl;
        }

        StopwatchManager::getInstance().notifyStop(stopwatch);
        StopwatchManager::getInstance().notifyStart(stopwatch);
        PLOT("_MotionCycle", stopwatch.lastValue);
      }//end while

      return NULL;
    }
    virtual void postExecute(){}
    virtual void preExecute(){}
} motionRtThread;
*/

void* cognitionThreadCallback(void* ref)
{
  NaoController* theController = static_cast<NaoController*> (ref);

  while(true) {
    theController->runCognition();
    g_atomic_int_set(&framesSinceCognitionLastSeen, 0);

    g_thread_yield();
  }

  return NULL;
}//end cognitionThreadCallback


void* motionThreadCallback(void* ref)
{
  g_atomic_int_set(&framesSinceCognitionLastSeen, 0);

  NaoController* theController = static_cast<NaoController*> (ref);

  Stopwatch stopwatch;
  while(true)
  {
    if(g_atomic_int_get(&framesSinceCognitionLastSeen) > 1000)
    {
      std::cerr << "+==================================+" << std::endl;
      std::cerr << "| NO MORE MESSAGES FROM COGNITION! |" << std::endl;
      std::cerr << "+==================================+" << std::endl;
      std::cerr << "dumping traces" << std::endl;
      Trace::getInstance().dump();
      StopwatchManager::getInstance().dump("cognition");

      #ifndef WIN32
      std::cerr << "syncing file system..." ;
      sync();
      std::cerr << " finished." << std::endl;
      #endif

      ASSERT(false && "Cognition seems to be dead");
    }//end if
    g_atomic_int_inc(&framesSinceCognitionLastSeen);

    theController->runMotion();
    
    if(sem_wait(dcm_sem) == -1) {
      cerr << "lock errno: " << errno << endl;
    }

    stopwatch.stop();
    stopwatch.start();
  }//end while<commit_msg>first cognition call might take longer than 1000 motion frames, so use the old number instead<commit_after>/**
 * @file main.cpp
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */

#include "NaoController.h"

#include <glib.h>
#include <glib-object.h>
#include <signal.h>
//#include <rttools/rtthread.h>


using namespace naoth;

gint framesSinceCognitionLastSeen;

void got_signal(int t)
{

  if(t == SIGTERM) {
    std::cout << "shutdown requested by kill signal" << t << std::endl;
  } else if(t == SIGSEGV) {
    std::cerr << "SEGMENTATION FAULT" << std::endl;
  } else {
    std::cerr << "catched unknown signal " << t << std::endl;
  }

  std::cout << "dumping traces" << std::endl;
  Trace::getInstance().dump();
  StopwatchManager::getInstance().dump("cognition");

  std::cout << "syncing file system..." ;
  sync();
  std::cout << " finished." << std::endl;

  exit(0);
}//end got_signal


// a semaphore for sychronization with the DCM
sem_t* dcm_sem = SEM_FAILED;


/* 
// Just some experiments with the RT-Threads
// not used yet
class TestThread : public RtThread
{
	public:
    TestThread(){}
    ~TestThread(){}
    NaoController* theController;

    virtual void *execute()
    {
      Stopwatch stopwatch;
      while(true)
      {
        theController->callMotion();
    
        if(sem_wait(dcm_sem) == -1)
        {
          cerr << "lock errno: " << errno << endl;
        }

        StopwatchManager::getInstance().notifyStop(stopwatch);
        StopwatchManager::getInstance().notifyStart(stopwatch);
        PLOT("_MotionCycle", stopwatch.lastValue);
      }//end while

      return NULL;
    }
    virtual void postExecute(){}
    virtual void preExecute(){}
} motionRtThread;
*/

void* cognitionThreadCallback(void* ref)
{
  NaoController* theController = static_cast<NaoController*> (ref);

  while(true) {
    theController->runCognition();
    g_atomic_int_set(&framesSinceCognitionLastSeen, 0);

    g_thread_yield();
  }

  return NULL;
}//end cognitionThreadCallback


void* motionThreadCallback(void* ref)
{
  g_atomic_int_set(&framesSinceCognitionLastSeen, 0);

  NaoController* theController = static_cast<NaoController*> (ref);

  Stopwatch stopwatch;
  while(true)
  {
    if(g_atomic_int_get(&framesSinceCognitionLastSeen) > 4000)
    {
      std::cerr << std::endl;
      std::cerr << "+==================================+" << std::endl;
      std::cerr << "| NO MORE MESSAGES FROM COGNITION! |" << std::endl;
      std::cerr << "+==================================+" << std::endl;
      std::cerr << "dumping traces" << std::endl;
      Trace::getInstance().dump();
      StopwatchManager::getInstance().dump("cognition");

      #ifndef WIN32
      std::cerr << "syncing file system..." ;
      sync();
      std::cerr << " finished." << std::endl;
      #endif

      ASSERT(false && "Cognition seems to be dead");
    }//end if
    g_atomic_int_inc(&framesSinceCognitionLastSeen);

    theController->runMotion();
    
    if(sem_wait(dcm_sem) == -1) {
      cerr << "lock errno: " << errno << endl;
    }

    stopwatch.stop();
    stopwatch.start();
  }//end while
{'fraction_non_alphanumeric': 0.18018018018018017, 'fraction_numerical': 0.004182754182754183, 'mean_word_length': 3.3936395759717315, 'pattern_counts': {'":': 0, '<': 153, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '971115', 'n_tokens_mistral': 2123, 'n_tokens_neox': 2011, 'n_words': 424}