Ejemplo n.º 1
0
int main(void)
{
	uint32_t err_code;
	bool erase_bonds;
	
	/* Initialise debugging port */
  debug_init();
  debug_printf("Debug port opened... Initialising...\r\n");
	
	/* Initialise GPIO subsystem */
	err_code = nrf_drv_gpiote_init();
	if (err_code == NRF_SUCCESS)
		debug_printf("GPIOTE initialised!\r\n");
	else
	{
			debug_printf("Ooops.. Something is wrong with initialising GPIOTE..\r\n");
			APP_ERROR_CHECK(err_code);
	}

	led_init();
	adc_init();
	debug_printf("Visual feedback should be active!\r\n");
	
	timers_init();
	ble_stack_init();
	beacon_adv_init();
	device_manager_init(erase_bonds);
	gap_params_init();
	advertising_init();
	services_init();
	conn_params_init();
	
	application_timers_start();
  advertising_start();
	
	led_rgb_set(LED_RED, true);
	led_rgb_set(LED_GREEN, false);
	led_rgb_set(LED_BLUE, false);
	
	debug_printf("Readyyy.. \r\n");
	
  while (true)
  {
		power_manage();
	}
}
Ejemplo n.º 2
0
int
main(int argc, char** argv)
{
    uint32_t led_colors = 0;
    uint8_t at_parm_test[10];
    unsigned once;
    uint32_t i = 0;
    uint8_t canPrescaler = 0;
    uint8_t* uart_tx_packet = 0;
    uint8_t* uart_rx_packet;
    uint32_t old_loadcell_data;
    uint16_t timeStep = 1;

    clock_init();
    pin_init();
    led_init();
    timers_init();
    state_init();
    uart_init();

    // Set up UART2 for 115200 baud. There's no round() on the dsPICs, so we implement our own.
    double brg = (double) 140000000 / 2.0 / 16.0 / 115200.0 - 1.0;
    if (brg - floor(brg) >= 0.5) {
        brg = ceil(brg);
    }
    else {
        brg = floor(brg);
    }
    //  Uart2Init (brg); // Init UART 2 as 115200 baud/s

    loadcell_init();
    loadcell_start();


    led_rgb_off();
    led_rgb_set(50, 0, 100);

    can_state.init_return = RET_UNKNOWN;
    if (can_init()) {
        while (1);
    }

    timer_state.prev_systime = 0;
    timer_state.systime = 0;

#ifdef CONF71
    // Start Reading the int pin on IMU
    mpuData.startData = 0;
    if (IMU_Init(400000, 70000000) == 0) {
        // imu_state.init_return = RET_OK;
        mpuData.startData = 1;
    }
    else {
        //imu_state.init_return = RET_ERROR;
    }
#endif

    for (;;) {
        if (timer_state.systime != timer_state.prev_systime) {
            timer_state.prev_systime = timer_state.systime;
            //everything in here will be executed once every ms
            //make sure that everything in here takes less than 1ms
            //useful for checking state consistency, synchronization, watchdog...

            if(timer_state.systime % 1000 == 1) {
                LED_4 = 1;
            }

            led_update();
            if (timer_state.systime % 10 == 1) {
                IMU_GetQuaternion(quaterion);
                QuaternionToYawPitchRoll(quaterion, ypr);
            }

            if (timer_state.systime % 5 == 1) {
                IMU_normalizeData(&mpuData, &magData, &imuData);
                // Run AHRS algorithm
                IMU_UpdateAHRS (&imuData);

                // Run IMU algorithm (does not use MAG data)
//                IMU_UpdateIMU(&imuData);

                //copy state to CAN dictionary
                IMU_CopyOutput(&imuData, &mpuData, &magData);
            }

            /**
             * CANFestival Loop
             */
            if (can_state.init_return == RET_OK) {
                can_process();

                /**
                 * Sets CANFestival shared variables
                 * specific to Sensor Board
                 */
                can_push_state();
            }

            /**
             * Blinking LED Loop
             */
            if (timer_state.systime % 25 == 0) {
                              LED_1 = !LED_1;
            }


        }
        else {
            //untimed processes in main loop:
            //executed as fast as possible
            //these processes should NOT block the main loop
//            LED_4 = mpuData.accelX > 0;
//            LED_3 = mpuData.accelY > 0;
//            LED_1 = mpuData.accelZ > 0;

//            IMU_GetData();
            IMU_CopyI2CData(&mpuData, &magData);

            if (!T1CONbits.TON) {
                RGB_RED = 0;
                RGB_GREEN = RGB_BLUE = 1;
                while (1);

            }

            if(can_flag){
                TimeDispatch();
                can_flag = 0;
            }

            uart_rx_packet = uart_rx_cur_packet();
            if (uart_rx_packet != 0) {
                uart_rx_packet_consumed();
            }

            /**
             * Handles CAN transmission buffers
             */
            if ((txreq_bitarray & 0b00000001) && !C1TR01CONbits.TXREQ0) {
                C1TR01CONbits.TXREQ0 = 1;
                txreq_bitarray = txreq_bitarray & 0b11111110;
            }
            if ((txreq_bitarray & 0b00000010) && !C1TR01CONbits.TXREQ1) {
                C1TR01CONbits.TXREQ1 = 1;
                txreq_bitarray = txreq_bitarray & 0b11111101;
            }
            if ((txreq_bitarray & 0b00000100) && !C1TR23CONbits.TXREQ2) {
                C1TR23CONbits.TXREQ2 = 1;
                txreq_bitarray = txreq_bitarray & 0b11111011;
            }
            if ((txreq_bitarray & 0b00001000) && !C1TR23CONbits.TXREQ3) {
                C1TR23CONbits.TXREQ3 = 1;
                txreq_bitarray = txreq_bitarray & 0b11110111;
            }
            if ((txreq_bitarray & 0b00010000) && !C1TR45CONbits.TXREQ4) {
                C1TR45CONbits.TXREQ4 = 1;
                txreq_bitarray = txreq_bitarray & 0b11101111;
            }
            if ((txreq_bitarray & 0b00100000) && !C1TR45CONbits.TXREQ5) {
                C1TR45CONbits.TXREQ5 = 1;
                txreq_bitarray = txreq_bitarray & 0b11011111;
            }
            if ((txreq_bitarray & 0b01000000) && !C1TR67CONbits.TXREQ6) {
                C1TR67CONbits.TXREQ6 = 1;
                txreq_bitarray = txreq_bitarray & 0b10111111;
            }
        }
    }
    return (EXIT_SUCCESS);
}
Ejemplo n.º 3
0
void http_process()
{
    xbee_rx_ip_packet_t rx_pkt;
    uint8_t* rf_data_p;
    uint16_t rf_data_len;
    if(http_state.init_return!=RET_OK){
        return;//not running
    }

    //check if top packet in circular buffer is a TCP packet on port 80
    if(CB_ReadMany(&http_state.rx_buffer,&rx_pkt,sizeof(xbee_rx_ip_packet_t))!=SUCCESS){
        return;//empty buffer
    } 

    //we have a packet to handle, handle it now!
    rf_data_p = xbee_ip_rx_rf_data(rx_pkt.raw_packet.raw_data); //actual packet data
    rf_data_len = xbee_ip_rx_rf_data_len(&rx_pkt.raw_packet);
    //parse the packet
    http_parser_init(parser,HTTP_REQUEST);
    http_state.last_url_length = 0;
    http_state.last_url = 0;
    settings->on_url = http_handle_url;
    settings->on_body = http_handle_body;

    http_parser_execute(parser,settings,rf_data_p,(size_t)rf_data_len);

        
    //check if we found a url
    if(http_state.last_url!=0 && http_state.last_url_length>0){//    && rf_data_len>3 && rf_data_p[0]=='G' && rf_data_p[1]=='E'&&rf_data_p[2]=='T'){
        http_state.num_requests++;
        led_rgb_set(50,0,255);
        http_state.last_url[http_state.last_url_length]=0;

        //prepare response
        char* resp =
        "{\"result\": {\"message\":\"req. %lu\",\"url\":\"%s\",\"acc\":[%d,%d,%d]}, \"error\": null, \"id\": 1}\r\n"
        "\r\n";
        sprintf(http_resp_buffer,resp,http_state.num_requests,http_state.last_url,(imu_state.acc_read_data[0]<<8)|imu_state.acc_read_data[1],(imu_state.acc_read_data[2]<<8)|imu_state.acc_read_data[3],(imu_state.acc_read_data[4]<<8)|imu_state.acc_read_data[5]);

        char* header = "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/html\r\n"
        "Content-Length: %u\r\n"
        "\r\n";//application/json-rpc\r\n"

        sprintf(http_header_buffer,header,strlen(http_resp_buffer));


        

        xbee_tx_ip_packet_t resp_pkt;
        //allocate memory for response
        if(allocate_ip_packet(strlen(http_resp_buffer)+strlen(http_header_buffer),&resp_pkt)==RET_OK){
            memcpy(resp_pkt.user_data_location,http_header_buffer,strlen(http_header_buffer));
            memcpy(resp_pkt.user_data_location+strlen(http_header_buffer),http_resp_buffer,strlen(http_resp_buffer));
            //set options
            resp_pkt.options.dest_address[0] = rx_pkt.options.source_addr[0];
            resp_pkt.options.dest_address[1] = rx_pkt.options.source_addr[1];
            resp_pkt.options.dest_address[2] = rx_pkt.options.source_addr[2];
            resp_pkt.options.dest_address[3] = rx_pkt.options.source_addr[3];
            resp_pkt.options.dest_port = rx_pkt.options.source_port;
            resp_pkt.options.source_port = rx_pkt.options.dest_port;
            resp_pkt.options.leave_open = 0;
            resp_pkt.options.protocol = XBEE_NET_IPPROTO_TCP;
            //xbee_free_packet(&resp_pkt.raw_packet);
            if(transmit_ip_packet(&resp_pkt)!=RET_OK){
                //packet could not be transmitted, we need to free the memory
                xbee_free_packet(&resp_pkt.raw_packet);
            } 
        } else {
            //could not allocate response packet.
        }
        
    }
    //free the packet data
    xbee_free_packet(&rx_pkt.raw_packet);
}