static int nrfmin_send(netdev_t *dev, const struct iovec *vector, unsigned count) { (void)dev; assert((vector != NULL) && (count > 0) && (state != STATE_OFF)); /* wait for any ongoing transmission to finish and go into idle state */ while (state == STATE_TX) {} go_idle(); /* copy packet data into the transmit buffer */ int pos = 0; for (unsigned i = 0; i < count; i++) { if ((pos + vector[i].iov_len) > NRFMIN_PKT_MAX) { DEBUG("[nrfmin] send: unable to do so, packet is too large!\n"); return -EOVERFLOW; } memcpy(&tx_buf.raw[pos], vector[i].iov_base, vector[i].iov_len); pos += vector[i].iov_len; } /* set output buffer and destination address */ nrfmin_hdr_t *hdr = (nrfmin_hdr_t *)vector[0].iov_base; NRF_RADIO->PACKETPTR = (uint32_t)(&tx_buf); NRF_RADIO->BASE0 = (CONF_ADDR_BASE | hdr->dst_addr); /* trigger the actual transmission */ DEBUG("[nrfmin] send: putting %i byte into the ether\n", (int)hdr->len); state = STATE_TX; NRF_RADIO->TASKS_TXEN = 1; return (int)count; }
void go_forward(void) { int16 obstaculo = 0; #ifdef sensorir obstaculo = verificaObstaculo(); if (obstaculo == 0) { #endif TIMER0_CLOCK_PRESCALER_64(); //frequencia de oscilacao de 1KHz TIMER0_COMPARE_A_CONFIGURE(255); //duty cycle 100% TIMER0_COMPARE_B_CONFIGURE(245); //duty cycle 100% clr_bit(MOTOR_SHIELD_DDR, MOTOR_INA); //seta a direcao como entrada INA clr_bit(MOTOR_SHIELD_DDR, MOTOR_INB); //seta a direcao como entrada INB set_bit(MOTOR_SHIELD_PORT, MOTOR_INA); //set_bit : sentido horario, clr_bit: sentido anti-horario set_bit(MOTOR_SHIELD_PORT, MOTOR_INB); #ifdef DEBUG printf("driving forward...\n"); #endif #ifdef sensorir } else if (obstaculo == 1) { go_idle(); obstaculo = 0; #ifdef DEBUG printf("obstacle, going idle to not crash...\n"); #endif } #endif }
void segue_coordenadas(int8 dados) { //o objetivo é centralizar a bolinha antes de seguir em frente int16 obstaculo = 0; switch (dados) { case CEL_DIREITA: turn_right(); #ifdef DEBUG printf("cellphone turning right...\n"); #endif break; case CEL_ESQUERDA: turn_left(); #ifdef DEBUG printf("cellphone turning left...\n"); #endif break; case CEL_FRENTE: #ifdef sensorir obstaculo = verificaObstaculo(); if (obstaculo == 0) { #endif go_forward(); #ifdef DEBUG printf("no obstacle, go ahead...\n"); #endif #ifdef sensorir } else if (obstaculo == 1) { go_idle(); obstaculo = 0; #ifdef DEBUG printf("obstacle, going idle to not crash...\n"); #endif } #endif default: break; } }
/** * @brief Set radio into the target state as defined by `target_state` * * Trick here is, that the driver can go back to it's previous state after a * send operation, so it can differentiate if the driver was in DISABLED or in * RX mode before the send process had started. */ static void goto_target_state(void) { go_idle(); if ((target_state == STATE_RX) && (rx_buf.pkt.hdr.len == 0)) { /* set receive buffer and our own address */ rx_lock = 1; NRF_RADIO->PACKETPTR = (uint32_t)(&rx_buf); NRF_RADIO->BASE0 = (CONF_ADDR_BASE | my_addr); /* goto RX mode */ NRF_RADIO->TASKS_RXEN = 1; state = STATE_RX; } if (target_state == STATE_OFF) { NRF_RADIO->POWER = 0; state = STATE_OFF; } }
void go_autonomo(int flag) { int8_t dados_tmp[3] = {0, 0, 0}; int8_t dado_correto = 0; if (flag == 1) { #ifdef DEBUG printf("Autonomous MODE: ON...\n"); #endif dados_tmp[0] = getDadosBT(); #ifdef DEBUG printf("tmp0: \n\n%d\n\n", dados_tmp[0]); #endif _delay_ms(20); dados_tmp[1] = getDadosBT(); #ifdef DEBUG printf("tmp1: \n\n%d\n\n", dados_tmp[1]); #endif _delay_ms(20); dados_tmp[2] = getDadosBT(); #ifdef DEBUG printf("tmp2: \n\n%d\n\n", dados_tmp[2]); #endif if ((dados_tmp[0] == dados_tmp[1]) && (dados_tmp[0] == dados_tmp[2])) { dado_correto = dados_tmp[0]; #ifdef DEBUG printf("DADO_CORRETO: \n\n%d\n\n", dado_correto); #endif switch (dado_correto) { case CEL_NULL: #ifdef DEBUG printf("recebi null, vou procurar esta bolinha! %d\n", dado_correto); #endif turn_right(); break; case CEL_FRENTE: #ifdef DEBUG printf("recebi o dado: %d : tenho que ir para frente\n", dado_correto); #endif segue_coordenadas(dado_correto); break; case CEL_DIREITA: #ifdef DEBUG printf("recebi o dado: %d : tenho que ir para direita\n", dado_correto); #endif segue_coordenadas(dado_correto); break; case CEL_ESQUERDA: #ifdef DEBUG printf("recebi o dado: %d : tenho que ir para esquerda\n", dado_correto); #endif segue_coordenadas(dado_correto); break; default: #ifdef DEBUG printf("nao sei o que recebi, vou ficar parado: %d\n", dado_correto); #endif go_idle(); //se nao sabe o que recebe fica parado break; } } else { go_idle(); //se nao sabe o que faz, nao faz nada #ifdef DEBUG printf("estou parado, nao sei aonde esta a bolinha...\n"); #endif } } else { //garante que o carrinho nao se mexe se flag = 0 go_idle(); } dados_tmp[0] = 0; dados_tmp[1] = 0; dados_tmp[2] = 0; dado_correto = 0; }