void AI_Machine::init(void) { const Json::Value &all_ai_json = CONFIG_INSTANCE->config_json()["ai"]["ai_behavior"]; if (all_ai_json == Json::Value::null) { LOG_ABORT("configure file error."); } for (Json::Value::iterator iter = all_ai_json.begin(); iter != all_ai_json.end(); ++iter) { int ai_id = atoi(iter.key().asCString()); AI_Data *ai_data = new AI_Data; Composite_Node *ai_behavior = new CompositeNode_Selector(); ai_data->ai_behavior = ai_behavior; behavior_init(*iter, ai_behavior); ai_data_map_.insert(std::make_pair(ai_id, ai_data)); } { Composite_Node* pselect_idle = new CompositeNode_Selector(); pselect_idle->add_child(CREATE_TERMINATE_NODE(Be_Idle)); blockhead_ai_ = pselect_idle; } { Composite_Node* psequence_ai_behavior = new CompositeNode_Sequence(); psequence_ai_behavior->add_child(CREATE_TERMINATE_NODE(Co_Idle_Status)); psequence_ai_behavior->add_child(CREATE_TERMINATE_NODE(Be_Idle)); //psequence_ai_behavior->add_child(CREATE_TERMINATE_NODE(Co_Die_Status)); //psequence_ai_behavior->add_child(CREATE_TERMINATE_NODE(Be_NPC_Recover)); normal_ai_ = psequence_ai_behavior; } }
int main(void) { int test_mode; int vm_present; int i; unsigned int seed; // Needs to be called ASAP as rf need a looooooong time to wake up. // This function is just sending a pulse over the SCL line. rf_wakeup(); clock_set_speed(16000000UL,16); setup_pps(); setup_io(); leds_init(); CHARGE_500MA = 0; // Switch back to 100mA charge. // Switch on one led to say we are powered on leds_set(LED_BATTERY_0, 32); // Enable the poweroff softirq. _INT3IF = 0; _INT3IP = 1; _INT3IE = 1; // Sound must be enabled before analog, as // The analog interrupt callback into sound processing ... // But must be initialised _after_ leds as it use one led IO for enabling amp. sound_init(); tone_init(); // Init tone generator pwm_motor_init(); pid_motor_init(); // We need the settings for the horizontal prox. load_settings_from_flash(); for (i = 0; i < 2; i++) { // Settings is definitely wrong.... if(settings.mot256[i] <= 0) settings.mot256[i] = 256; // 1024 (AD resolution is 10 bits) * 256 / 9 fits in signed 16 bits. if (settings.mot256[i] < 9) settings.mot256[i] = 9; } // This is the horizontal prox. Vertical one are handled by the ADC // but ADC sync the motor mesurment with the prox, so we don't pullute it with noise ... timer_init(TIMER_IR_COMM, 0,-1); // The period will be changed later. prox_init(PRIO_SENSORS); // Same priority as analog (maybe should be at 7 ...) // Warning: We cannot use the SD before the analog init as some pin are on the analog port. analog_init(TIMER_ANALOG, PRIO_SENSORS); wait_valid_vbat(); log_init(); // We will need to read vbat to be sure we can flash. ntc_init(ntc_callback, PRIO_1KHZ); // i2c_init(I2C_3); i2c_init_master(I2C_3, 400000, PRIO_I2C); I2C3CON = 0x9000; mma7660_init(I2C_3, MMA7660_DEFAULT_ADDRESS, acc_cb, 0); mma7660_set_mode(MMA7660_120HZ, 1); rc5_init(TIMER_RC5, rc5_callback, PRIO_RC5); sd_init(); timer_init(TIMER_1KHZ, 1000, 6); timer_enable_interrupt(TIMER_1KHZ, timer_1khz, PRIO_1KHZ); rf_init(I2C_3); timer_enable(TIMER_1KHZ); sd_log_file(); vm_present = init_aseba_and_fifo(); if(vm_present) log_analyse_bytecode(); vmVariables.fwversion[0] = FW_VERSION; vmVariables.fwversion[1] = FW_VARIANT; // SD file is more important than internal flash if(!sd_load_aseba_code()) { log_set_flag(LOG_FLAG_VMCODESD); vm_present = 1; log_analyse_bytecode(); } // Behavior is on INT4 (softirq trigged by 1khz timer). behavior_init(PRIO_BEHAVIOR); test_mode = sd_test_file_present(); if(!test_mode) mode_init(vm_present); // Enable the LVD interrupt _LVDIE = 1; play_sound(SOUND_POWERON); if(test_mode) { test_mode_start(); while(1) idle_without_aseba(); } while(behavior_enabled(B_MODE)) idle_without_aseba(); // If usb did not put us out of behavior mode, then start the rf link if(!usb_uart_serial_port_open() && (rf_get_status() & RF_PRESENT)) { rf_set_link(RF_UP); } // get the random seed seed = 0; for(i = 0; i < 5; i++) { seed += vmVariables.buttons_mean[i]; seed += vmVariables.buttons_noise[i]; } seed += vmVariables.vbat[0]; seed += vmVariables.vbat[1]; for(i = 0; i < 3; i++) seed += vmVariables.acc[i]; AsebaSetRandomSeed(seed); for(i = 0; i < 3; i++) AsebaGetRandom(); // Give full control to aseba. No way out (except reset). run_aseba_main_loop(); }
int AI_Machine::behavior_init(Json::Value &node_json, Composite_Node* parent_node) { const Json::Value &ai_action_pool = CONFIG_INSTANCE->config_json()["ai"]["ai_pool"]; if (ai_action_pool == Json::Value::null) { MSG_USER("configure file error."); } for (Json::Value::iterator iter = node_json.begin(); iter != node_json.end(); ++iter) { std::string node = (*iter)["node"].asString(); std::string type = (*iter)["type"].asString(); Composite_Node* this_node = NULL; if (type == "Selector") { this_node = new CompositeNode_Selector(); } else if (type == "Sequence") { this_node = new CompositeNode_Sequence(); } else if (type == "Parallel") { this_node = new CompositeNode_Parallel(); } else if (type == "Condition") { if (ai_action_pool[node].asString() == "Co_Die_Status") parent_node->add_child(CREATE_TERMINATE_NODE(Co_Die_Status)); // else if (ai_action_pool[node].asString() == "Co_Dormancy_Status") // parent_node->add_child(CREATE_TERMINATE_NODE(Co_Dormancy_Status)); else if (ai_action_pool[node].asString() == "Co_Back_Status") parent_node->add_child(CREATE_TERMINATE_NODE(Co_Back_Status)); else if (ai_action_pool[node].asString() == "Co_Hatred_List_Not_Null") parent_node->add_child(CREATE_TERMINATE_NODE(Co_Hatred_List_Not_Null)); // else if (ai_action_pool[node].asString() == "Co_HP_Not_Full") // parent_node->add_child(CREATE_TERMINATE_NODE(Co_HP_Not_Full)); // else if (ai_action_pool[node].asString() == "Co_HP_Is_Empty") // parent_node->add_child(CREATE_TERMINATE_NODE(Co_HP_Is_Empty)); // else if (ai_action_pool[node].asString() == "Co_Has_Move_Target") // parent_node->add_child(CREATE_TERMINATE_NODE(Co_Has_Move_Target)); // else if (ai_action_pool[node].asString() == "Co_Hatred_List_Is_Null") // parent_node->add_child(CREATE_TERMINATE_NODE(Co_Hatred_List_Is_Null)); else MSG_USER("AI:no condition:%s", node.c_str()); } else if (type == "Behavior") { if (ai_action_pool[node].asString() == "Be_Idle") parent_node->add_child(CREATE_TERMINATE_NODE(Be_Idle)); // else if (ai_action_pool[node].asString() == "Be_NPC_Recover") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_NPC_Recover)); // else if (ai_action_pool[node].asString() == "Be_Battle_Mode_A") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Battle_Mode_A)); // else if (ai_action_pool[node].asString() == "Be_Battle_Mode_B") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Battle_Mode_B)); else if (ai_action_pool[node].asString() == "Be_Patrol_Mode_A") parent_node->add_child(CREATE_TERMINATE_NODE(Be_Patrol_Mode_A)); else if (ai_action_pool[node].asString() == "Be_Chase_Mode") parent_node->add_child(CREATE_TERMINATE_NODE(Be_Chase_Mode)); else if (ai_action_pool[node].asString() == "Be_Back_Mode") parent_node->add_child(CREATE_TERMINATE_NODE(Be_Back_Mode)); // else if (ai_action_pool[node].asString() == "Be_Pet_Mode") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Pet_Mode)); // else if (ai_action_pool[node].asString() == "Be_Hp_Full") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Hp_Full)); // else if (ai_action_pool[node].asString() == "Be_Clean_Hatred_List") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Clean_Hatred_List)); // else if (ai_action_pool[node].asString() == "Be_Clean_Walk_Path") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Clean_Walk_Path)); // else if (ai_action_pool[node].asString() == "Be_Dormancy_Breaking_And_Full_State") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Dormancy_Breaking_And_Full_State)); // else if (ai_action_pool[node].asString() == "Be_Set_Dormancy_Status") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Set_Dormancy_Status)); else if (ai_action_pool[node].asString() == "Be_NPC_Die") parent_node->add_child(CREATE_TERMINATE_NODE(Be_NPC_Die)); // else if (ai_action_pool[node].asString() == "Be_Delay_Until_Next_Resurrection_Time_Resurrection") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Delay_Until_Next_Resurrection_Time_Resurrection)); // else if (ai_action_pool[node].asString() == "Be_Material_Mode") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Material_Mode)); // else if (ai_action_pool[node].asString() == "Be_Move_Mode_A") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Move_Mode_A)); else if (ai_action_pool[node].asString() == "Be_Move_Mode_B") parent_node->add_child(CREATE_TERMINATE_NODE(Be_Move_Mode_B)); // else if (ai_action_pool[node].asString() == "Be_Move_Mode_D") // parent_node->add_child(CREATE_TERMINATE_NODE(Be_Move_Mode_D)); else MSG_USER("AI:no behavior:%s", node.c_str()); } if (this_node != NULL) { parent_node->add_child(this_node); } if ((*iter)["child_node"] != Json::Value::null) behavior_init((*iter)["child_node"], this_node); } return 0; }