void main(void) { twi_master_init(); mpu6500_init(); mpu6500_stop(); disable_i2c(); simble_init("Motion"); motion_ctx.sampling_period = DEFAULT_SAMPLING_PERIOD; //Set the timer parameters and initialize it. struct rtc_ctx rtc_ctx = { .rtc_x[NOTIF_TIMER_ID] = { .type = PERIODIC, .period = motion_ctx.sampling_period, .enabled = false, .cb = notif_timer_cb, } }; batt_serv_init(&rtc_ctx); rtc_init(&rtc_ctx); ind_init(); motion_init(&motion_ctx); simble_adv_start(); simble_process_event_loop(); }
population_t *pop_init(sat_t *sat, uint32_t size) { population_t *population = NULL; population = calloc(1, sizeof(population_t)); assert(population); population->sat = sat; population->inds_cnt = size; population->inds = calloc(size, sizeof(ind_t)); if (!population->inds) { fprintf(stderr, "calloc individuals\n"); goto error; } for (uint32_t i = 0; size > i; i++) { population->inds[i] = ind_init(sat); assert(population->inds[i]); } return (population); error: // population_free(population); return (NULL); }
void sat_simulated_evolution(sat_t *sat, uint32_t pop_size) { population_t *pop = NULL; population_t *pop_next = NULL; population_t *pop_swap = NULL; pop = pop_init(sat, pop_size); pop_next = pop_init(sat, pop_size); uint32_t ic = sat->vars_cnt; uint32_t favg; /* average fitness */ ind_t *ind_best = ind_init(sat); for (uint32_t gen = 0; gen < 10000; gen++) { for (uint32_t i = 0; i < pop->inds_cnt; i += 2) { memcpy(pop_next->inds[i + 0]->ch, ga_select(pop)->ch, sizeof(bool) * ic); memcpy(pop_next->inds[i + 1]->ch, ga_select(pop)->ch, sizeof(bool) * ic); ga_cross_2p(pop_next->inds[i + 0]->ch, pop_next->inds[i + 1]->ch, ic); ga_mutate(pop_next->inds[i + 0]->ch, ic); ga_mutate(pop_next->inds[i + 1]->ch, ic); ind_compute_fitness(pop_next->inds[i + 0]); ind_compute_fitness(pop_next->inds[i + 1]); } pop_swap = pop; pop = pop_next; pop_next = pop_swap; qsort(pop->inds, pop->inds_cnt, sizeof(ind_t **), ga_fitness_cmp); /* compute average fitness */ favg = 0; for (uint32_t i = 0; i < pop->inds_cnt; i++) { favg += pop->inds[i]->fitness; } favg /= pop->inds_cnt; // fprintf(stdout, "gen=%u avg=%u best ind:", gen, favg); // ind_print(pop->inds[0]); if (ind_best->fitness < pop->inds[0]->fitness) { memcpy(ind_best->ch, pop->inds[0]->ch, sizeof(bool) * ic); } } fprintf(stdout, "total best: %u\n", ind_best->fitness); // ind_print(ind_best); // printf("best ind: "); // ind_print(pop->inds[0]); ind_free(ind_best); population_free(pop); population_free(pop_next); }
int main() { u8 data[ 4 ]; int res; leds_init(); btn_init(); ind_init(); uart_init(); sei(); ledvm_init(); i2cee_init(); pgm_init(); //stdout = &mystdout; nrf_init(); nrf_set_own_addr( lights_addr ); nrf_set_mode( NRF_MODE_RX ); while( 1 ) { // Something to execute? if( pgm_crt != -1 ) { res = ledvm_run(); if( res != LEDVM_ERR_OK ) { if( res == LEDVM_ERR_FINISHED ) // program finished, look for the next one { if( pgm_cycle ) { res = pgm_get_next_index( pgm_crt, 1 ); if( res != -1 ) // if found use the other program, otherwise don't change it pgm_crt = res; } ledvm_init(); } else { ledvm_ll_setrgb( 0, 0, 0 ); pgm_crt = -1; } } } // New data via radio? if( nrf_get_packet( data, 4, NULL ) == 4 ) { // Got data, interpret it if( data[ 0 ] == CMD_START_WRITE ) { pgm_recv_on = 1; pgm_recv_slot = data[ 3 ]; pgm_recv_addr = 0; pgm_recv_buf_idx = 0; if( pgm_recv_slot == pgm_crt ) pgm_crt = -1; } else if( data[ 0 ] == CMD_END_WRITE ) { pgm_recv_on = 0; pgm_recv_to_flash(); pgm_set_program_state( pgm_recv_slot, 1 ); if( pgm_crt == -1 ) pgm_crt = pgm_get_first_index(); } else if( data[ 0 ] == CMD_RUN ) { if( pgm_mask & ( 1 << data[ 3 ] ) ) { pgm_crt = data[ 3 ]; ledvm_init(); } } else if( data[ 0 ] == CMD_RAW ) { pgm_crt = -1; ledvm_ll_setrgb( data[ 1 ], data[ 2 ], data[ 3 ] ); } else if( data[ 0 ] == CMD_DEL ) { pgm_set_program_state( data[ 3 ], 0 ); if( pgm_crt == data[ 3 ] ) { pgm_crt = pgm_get_first_index(); ledvm_init(); } } else if( pgm_recv_on ) // instruction { memcpy( pgm_recv_buffer + pgm_recv_buf_idx, data, 4 ); pgm_recv_buf_idx += 4; if( pgm_recv_buf_idx == PGM_EE_PAGE_SIZE ) pgm_recv_to_flash(); } } // Handle buttons if( pgm_crt != -1 ) { btn_handler(); if( btn_is_pressed( BTN_NEXT_PIN ) ) { res = pgm_get_next_index( pgm_crt, 1 ); if( res != -1 ) pgm_crt = res; ledvm_init(); } else if( btn_is_pressed( BTN_PREV_PIN ) ) { res = pgm_get_next_index( pgm_crt, -1 ); if( res != -1 ) pgm_crt = res; ledvm_init(); } else if( btn_is_pressed( BTN_REPEAT_PIN ) ) ind_set( 1 - pgm_cycle ); } } }