Esempio n. 1
0
void sync_comm(uint32_t* data, uint32_t length)
{
    uint32_t i, j;                   // loop counters
    uint32_t current;                // current word of data
    uint32_t time = get_time()+1000; // start time after initialization
    for(i = 0; i < length; i++) {    // iterate through input data
        current = data[i];           // read next word
        for(j = 0; j < 32; j++) {    // iterate through each bit in word
            time = time + PERIOD/2;
            set_compare(time);
            delay_until();           // wait half period
            gpo_set_0(1);            // posedge clk on pin
            if(current & 1) {        // if bit == 1...
                gpo_set_0(2);        // output bit goes high
            } else {                 // if bit == 0...
                gpo_clear_0(2);      // output bit goes high
            }
            current = current >> 1;  // setup next bit
            time = time + PERIOD/2;
            set_compare(time);
            delay_until();           // wait half period
            gpo_clear_0(1);          // negedge clk on pin
        }
    }
}
Esempio n. 2
0
int main(int args, char **argv)
{
  
  int v;
  int i;
  int first = 1;
  int k = 2;
  
  int period = 10000;

  long x = get_time();
 
  for(;;){

    if(!first){
      delay_until(x);
      mtfd_end();
    }

    mtfd_begin(period);    
    if(!first)
      actuating(k);  
    v = sensing();  
    x += period;    
    k = computation(v);      
    first = 0;
  }

  // The interesting thing is that the final code
  // includes to call to pret_mt

  return k;
}
void producer(void)
 {
   int item;
   if(count==N) 
   delay_until(count<100);
   else
   count++;
 }
 void consumer(void)
 {
   int item;
   if(count==0)
   delay_until(count>0);
   else
   count--;
 }
Esempio n. 5
0
int main(int argc, char *argv[])
{
  int h,l;
  get_time(h,l);
  add_ms(h,l,10);
  delay_until(h,l);

  return 0;
}
Esempio n. 6
0
long cyclic_thread::execute(void *arg)
{
  const float delay_interval = 1.0 / m_frequency;

  struct timespec t1;
  struct timespec t2; 

  // Make GCC happy (-Wextra)
  if (arg) {
    return THREAD_INTERNAL_ERROR;
  }

  // Prepare first run
  if ( clock_gettime(get_clock_id(), &t1) ) {
    return THREAD_TIME_ERROR;
  }
  if ( get_new_time(&t1, delay_interval, &t2) != DELAY_SUCCESS ) {
    return THREAD_TIME_ERROR;
  }
  if ( delay_until(&t2) != DELAY_SUCCESS) {
    return THREAD_TIME_ERROR;
  }

  while ( !is_stopped() ) {

    // Do cyclic work
    if ( cyclic_execute() != THREAD_SUCCESS ) {
      return THREAD_INTERNAL_ERROR;
    }

    // Calculate next interval
    if ( get_new_time(&t2, delay_interval, &t2) != DELAY_SUCCESS ) {
      return THREAD_TIME_ERROR;
    }
    if ( delay_until(&t2) != DELAY_SUCCESS) {
      return THREAD_TIME_ERROR;
    }

    update_exe_cnt();
  }

  return THREAD_SUCCESS;
}
Esempio n. 7
0
int main(void) {

    unsigned int ns_h = 0;
    unsigned int ns_l = 100000;

    unsigned int message;

    // Synchronize.
    delay_until(ns_h, ns_l);

    while(1) {
        message = tohost_id(THREAD, 4); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        taskA4_main();
        message = tohost_id(THREAD, 14); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        add_ms(ns_h, ns_l, 200);
        delay_until(ns_h, ns_l);
    }
    return 0;
}
Esempio n. 8
0
int main(void) {

    unsigned int ns_h = 0;
    unsigned int ns_l = 100000;
    unsigned int i = 0;

    unsigned int message;

    // Synchronize.
    delay_until(ns_h, ns_l);

    while(1) {
        message = tohost_id(THREAD, 1); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        taskB1_main();
        message = tohost_id(THREAD, 11); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        if(i == 0) {
        message = tohost_id(THREAD, 2); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        taskB2_main();
        message = tohost_id(THREAD, 12); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        i++;
        } else {
        message = tohost_id(THREAD, 3); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        taskB3_main();
        message = tohost_id(THREAD, 13); mtpcr(30, message); 
        message = tohost_time(get_time_low()); mtpcr(30, message);
        i = 0;
        }
        add_ms(ns_h, ns_l, 25);
        delay_until(ns_h, ns_l);
    }
    return 0;
}
Esempio n. 9
0
int main(int argc, char *argv[])
{

int h,l;               // High and low 32-bit time values
get_time(h,l);         // Current time in nano seconds
while(1){              // Repeat control loop forever
  add_ms(h,l,10);      // Add 10ms 
  exception_on_expire(h,l,missed_deadline_handler);
                       // Upper timing bound. Exception handler
  compute_task();      // Sense, compute, and actuate 
  delay_until(h,l);    // Delay until start of next period
}

  return 0;
}
Esempio n. 10
0
/*!
 * Delay thread until given time period expires
 * \param t Expiration time
 * \return 0
 */
int delay ( time_t *t )
{
	time_t aps;

	ASSERT_ERRNO_AND_RETURN ( t, E_INVALID_ARGUMENT );
	time_get ( &aps );

	aps.sec += t->sec;
	aps.nsec += t->nsec;
	if ( aps.nsec > 1000000000 )
	{
		aps.nsec -= 1000000000;
		aps.sec++;
	}

	return delay_until ( &aps );
}
Esempio n. 11
0
/**
 * Pour requested_amount grams from bottle..
 * Return 0 on success, other values are return values of
 * delay_until (including scale error codes).
 */
errv_t Bottle::pour(int requested_amount, int& measured_amount) {
    // orig_weight is weight including ingredients poured until now
    int orig_weight, ret;
    while (1) {
        // get weight while turning bottle, because ads1231_stable_millis()
        // blocks bottle in pause position too long
        int below_pause = (pos_down + get_pause_pos()) / 2;
        ret = turn_to(below_pause, TURN_DOWN_DELAY, true, &orig_weight);

        // Note that checking weight here is a critical issue, allows hacking
        // the robot. If a heavy weight is placed while measuring and then
        // removed while pouring (results in more alcohol). Stable weight
        // should resolve most problems.
        if (ret == WEIGHT_NOT_STABLE) {
            ret = ads1231_get_stable_grams(orig_weight);
        }
        if (ret != 0 && ret != WHERE_THE_FUCK_IS_THE_CUP) {
            return ret;
        }
        if (ret == WHERE_THE_FUCK_IS_THE_CUP || orig_weight < WEIGHT_EPSILON) {
            // no cup...
            RETURN_IFN_0(wait_for_cup());
        }
        else {
            // everything fine
            break;
        }
    }

    // loop until successfully poured or aborted or other fatal error
    while(1) {
        // petres wants POURING message also after resume...
        // https://github.com/rfjakob/barwin-arduino/issues/10
        MSG(String("POURING ") + String(number) + String(" ") + String(orig_weight));

        DEBUG_MSG_LN("Turn down");
        ret = turn_down(TURN_DOWN_DELAY, true); // enable check_weight

        // wait for requested weight
        // FIXME here we do not want WEIGHT_EPSILON and sharp >
        if (ret == 0) {
            DEBUG_MSG_LN("Waiting");
            ret = delay_until(POURING_TIMEOUT,
                    orig_weight + requested_amount - UPGRIGHT_OFFSET, true);
        }
        if (ret == 0)
            break; // All good

        DEBUG_MSG_LN(String("pour: got err ") + String(ret));

        // Bottle empty
        // Note that this does not work if requested_amount is less than
        // UPGRIGHT_OFFSET!
        if(ret == BOTTLE_EMPTY) {
            ERROR(strerror(BOTTLE_EMPTY) + String(" ") + String(number) );
            // TODO other speed here? it is empty already!
            RETURN_IFN_0(turn_to(pos_up + BOTTLE_EMPTY_POS_OFFSET, TURN_UP_DELAY));
            RETURN_IFN_0(wait_for_resume()); // might return ABORTED
        }
        // Cup was removed early
        else if(ret == WHERE_THE_FUCK_IS_THE_CUP) {
            ERROR(strerror(WHERE_THE_FUCK_IS_THE_CUP));
            RETURN_IFN_0(turn_to_pause_pos(FAST_TURN_UP_DELAY));
            RETURN_IFN_0(wait_for_cup());
        }
        // other error - turn bottle up and return error code
        // includes: scale error, user abort, ...
        else {
            return ret;
        }
    }

    // We turn to pause pos and not completely up so we can crossfade
    RETURN_IFN_0(turn_to_pause_pos(TURN_UP_DELAY));

    RETURN_IFN_0(ads1231_get_grams(measured_amount));
    measured_amount -= orig_weight;

    DEBUG_START();
    DEBUG_MSG("Stats: ");
    DEBUG_VAL(requested_amount);
    DEBUG_VAL(measured_amount);
    DEBUG_END();
    return 0;
}