int main(){ signal(SIGINT, &sig_handler); uint16_t rotary_value = 0; float value = 0.0f; mraa_pwm_context pwm1; mraa_aio_context rotary; pwm1 = mraa_pwm_init(5); rotary = mraa_aio_init(0); if (rotary == NULL || pwm1 == NULL) { return 1; printf("Broken\n"); } mraa_pwm_period_ms(pwm1, 20); mraa_pwm_enable(pwm1, 1); while(isrunning){ rotary_value = mraa_aio_read(rotary); //convert to 0.00 to 1.00 scale value = ((float) rotary_value)/102; //convert to 0.025 to 0.1 scale (avoid rattle) value = value/13.33; value = value + 0.025; if (value >= .25){ mraa_pwm_enable(pwm1, 1); printf("%f\n", value); mraa_pwm_write(pwm1, value); } else{ // mraa_pwm_write(pwm1, 0); mraa_pwm_enable(pwm1, 0); } } mraa_pwm_write(pwm1, 0.025f); mraa_pwm_enable(pwm1, 0); return 0; }
void tb6612::diffDrive(float dcA, float dcB) { _dcA=dcA; _dcB=dcB; if (dcA < 0) { revA(); dcA *= -1; } else { fwdA(); } if (dcB < 0) { revB(); dcB *= -1; } else { fwdB(); } mraa_pwm_write(_pwmA, dcA); mraa_pwm_write(_pwmB, dcB); }
void speed_control(mraa_pwm_context in1, mraa_pwm_context in2, float speed) { speed = speed/100; if (speed >= 0) { mraa_pwm_write(in2, 1.0f); mraa_pwm_write(in1, 1.0f - speed); } else if (speed < 0) { mraa_pwm_write(in1, 1.0f); mraa_pwm_write(in2, 1.0f + speed); } }
void right_turn () { float speed1 = mraa_pwm_read(pwm1); float speed2 = mraa_pwm_read(pwm2); if (speed1 == speed2) { mraa_pwm_write(pwm2, speed2/3); } else if (speed2 > speed1) { mraa_pwm_write(pwm1, speed2); mraa_pwm_write(pwm2, speed1); } }
void set_B(float speed) { if (speed > 0 && speed <= 1.0f) { mraa_pwm_write(pwm1, speed); forward_B(); } else if (speed < 0 && speed >= -1.0f) { mraa_pwm_write(pwm1, -speed); reverse_B(); } else { mraa_pwm_write(pwm1, 0.); brake_B(); } }
tb6612::tb6612(): _dcA(0.0), _dcB(0.0) { // _pwmA is pwm channel 0, on pin 20 in mraa _pwmA = mraa_pwm_init(20); mraa_pwm_period_us(_pwmA, 1000); mraa_pwm_enable(_pwmA, 1); // _pwmB is pwm channel 1, on pin 14 in mraa _pwmB = mraa_pwm_init(14); mraa_pwm_period_us(_pwmB, 1000); mraa_pwm_enable(_pwmB, 1); mraa_pwm_write(_pwmA, 0.01); mraa_pwm_write(_pwmB, 0.01); mraa_pwm_write(_pwmA, 0.0); mraa_pwm_write(_pwmB, 0.0); // _A1 and _A2 are on GPIO48 and GPIO47, respectively, which are pins 33 and // 46 in mraa, respectively. _A1 = mraa_gpio_init(33); _A2 = mraa_gpio_init(46); mraa_gpio_dir(_A1, MRAA_GPIO_OUT); mraa_gpio_mode(_A1, MRAA_GPIO_STRONG); mraa_gpio_write(_A1, 1); mraa_gpio_dir(_A2, MRAA_GPIO_OUT); mraa_gpio_mode(_A2, MRAA_GPIO_STRONG); mraa_gpio_write(_A2, 1); // _B1 and _B2 are on GPIO15 and GPIO14, respectively, which are pins 48 and // 36, respectively _B1 = mraa_gpio_init(48); _B2 = mraa_gpio_init(36); mraa_gpio_dir(_B1, MRAA_GPIO_OUT); mraa_gpio_mode(_B1, MRAA_GPIO_STRONG); mraa_gpio_write(_B1, 1); mraa_gpio_dir(_B2, MRAA_GPIO_OUT); mraa_gpio_mode(_B2, MRAA_GPIO_STRONG); mraa_gpio_write(_B2, 1); // _standbyPin is on GPIO49, which is pin 47 in mraa _standbyPin = mraa_gpio_init(47); mraa_gpio_dir(_standbyPin, MRAA_GPIO_OUT); mraa_gpio_mode(_standbyPin, MRAA_GPIO_STRONG); mraa_gpio_write(_standbyPin, 1); }
int rotate_left (int i) { while(i!=0) {mraa_pwm_enable(en1,1); mraa_pwm_enable(en2,1); mraa_pwm_write(en1,1.0f); mraa_pwm_write(en2,1.0f); mraa_gpio_write(in1,0); mraa_gpio_write(in2,1); mraa_gpio_write(in3,0); mraa_gpio_write(in4,1); usleep(( 50000)); ///chagne the value accordingly printf("front"); mraa_gpio_write(in1,0); mraa_gpio_write(in2,0); mraa_gpio_write(in3,0); mraa_gpio_write(in4,0); mraa_pwm_enable(en1,0); mraa_pwm_enable(en2,0); i--; } return 0; }
int main() { mraa_gpio_context in1; mraa_gpio_context in2; mraa_gpio_context in3; mraa_gpio_context in4; in1=mraa_gpio_init(7); in2=mraa_gpio_init(8); in3=mraa_gpio_init(9); in4=mraa_gpio_init(10); mraa_pwm_context en1; en1=mraa_pwm_init(6); mraa_pwm_context en2; en2=mraa_pwm_init(5); mraa_gpio_dir(in1,MRAA_GPIO_OUT); mraa_gpio_dir(in2,MRAA_GPIO_OUT); mraa_pwm_enable(en1,1); mraa_pwm_period_ms(en1,20); mraa_gpio_dir(in3,MRAA_GPIO_OUT); mraa_gpio_dir(in4,MRAA_GPIO_OUT); mraa_pwm_enable(en2,1); mraa_pwm_period_ms(en2,20); mraa_pwm_write(en1,1.0f); mraa_pwm_write(en2,1.0f); mraa_gpio_write(in1,1); mraa_gpio_write(in2,0); mraa_gpio_write(in3,0); mraa_gpio_write(in4,1); printf("right90"); usleep(2300000); mraa_gpio_write(in1,0); mraa_gpio_write(in2,0); mraa_gpio_write(in3,0); mraa_gpio_write(in4,0); mraa_pwm_enable(en1,0); mraa_pwm_enable(en2,0); usleep(2300000); return 0; }
void straight (float speed) { if (speed >= 0 && speed <= 1.0f) { mraa_pwm_write(pwm1, speed); mraa_pwm_write(pwm2, speed); forward_A(); forward_B(); } else if (speed < 0 && speed >= -1.0f) { mraa_pwm_write(pwm1, -speed); mraa_pwm_write(pwm2, -speed); reverse_A(); reverse_B(); } else printf("Wrong speed value. Choose between -1.0 and 1.0\n"); }
int rotate_left90() { mraa_pwm_enable(en1,1); mraa_pwm_enable(en2,1); mraa_pwm_write(en1,1.0f); mraa_pwm_write(en2,1.0f); mraa_gpio_write(in1,0); mraa_gpio_write(in2,1); mraa_gpio_write(in3,0); mraa_gpio_write(in4,1); printf("left90"); usleep(2300000); mraa_gpio_write(in1,0); mraa_gpio_write(in2,0); mraa_gpio_write(in3,0); mraa_gpio_write(in4,0); mraa_pwm_enable(en1,0); mraa_pwm_enable(en2,0); //usleep(2300000); return 0; }
void BuzzerAlarm::start(note tone, float vol, int delay) { mraa_pwm_period_us(myAlarm, tone); float volume; // éviter les valeurs impossibles if (vol > 1.0) volume = 1.0; else if (vol < 0.0) volume = 0.0; else volume = vol; // la volume correspond au rapport cyclique, 50% sera donc la volume max mraa_pwm_write(myAlarm, volume*0.5); usleep(delay*1000); BuzzerAlarm::stop(); }
int move_front (int i) { while(i!=0) { mraa_pwm_enable(en1,1); mraa_pwm_enable(en2,1); mraa_pwm_write(en1,1.0f); mraa_pwm_write(en2,1.0f); mraa_gpio_write(in1,0); mraa_gpio_write(in2,1); mraa_gpio_write(in3,0); mraa_gpio_write(in4,1); usleep(50000); printf("front"); mraa_gpio_write(in1,0); mraa_gpio_write(in2,0); mraa_gpio_write(in3,0); mraa_gpio_write(in4,0); mraa_pwm_enable(en1,0); mraa_pwm_enable(en2,0); i--; } return 0; }
void PWM_Run(void) { mraa_pwm_period_ms(pwm, TIME_PERIOD); //printf("Time Period set to %dms\n", TIME_PERIOD); mraa_pwm_enable(pwm,1); //printf("PWM enabled\n"); mraa_pwm_write(pwm, DUTY_CYCLE); //printf("Duty Cycle set to %f%%\n", (100 * DUTY_CYCLE)); //printf("PWM running\n"); }
void initialize() { pwm1 = mraa_pwm_init(20); mraa_pwm_period_us(pwm1, 870); mraa_pwm_enable(pwm1, 1); pwm2 = mraa_pwm_init(14); mraa_pwm_period_us(pwm2, 870); mraa_pwm_enable(pwm2, 1); mraa_pwm_write(pwm1, 0.01); mraa_pwm_write(pwm2, 0.01); mraa_pwm_write(pwm1, 0.0); mraa_pwm_write(pwm2, 0.0); A1 = mraa_gpio_init(48); A2 = mraa_gpio_init(36); mraa_gpio_dir(A1, MRAA_GPIO_OUT); mraa_gpio_mode(A1, MRAA_GPIO_STRONG); mraa_gpio_write(A1, 1); mraa_gpio_dir(A2, MRAA_GPIO_OUT); mraa_gpio_mode(A2, MRAA_GPIO_STRONG); mraa_gpio_write(A2, 1); B1 = mraa_gpio_init(33); B2 = mraa_gpio_init(46); mraa_gpio_dir(B1, MRAA_GPIO_OUT); mraa_gpio_mode(B1, MRAA_GPIO_STRONG); mraa_gpio_write(B1, 1); mraa_gpio_dir(B2, MRAA_GPIO_OUT); mraa_gpio_mode(B2, MRAA_GPIO_STRONG); mraa_gpio_write(B2, 1); standbyPin = mraa_gpio_init(47); mraa_gpio_dir(standbyPin, MRAA_GPIO_OUT); mraa_gpio_mode(standbyPin, MRAA_GPIO_STRONG); mraa_gpio_write(standbyPin, 1); }
FskErr mraaPWMSetDutyCycle(FskPinPWM pin, double value) { mraaPWM mpwm = (mraaPWM)pin; int result = mraa_pwm_write(mpwm->context, value); if (MRAA_SUCCESS != result) return kFskErrOperationFailed; if (!mpwm->enabled) { mraa_pwm_enable(mpwm->context, 1); mpwm->enabled = 1; } return kFskErrNone; }
void send_high_bit() { int i = 0; printf("1"); mraa_pwm_write(pwm1,DUTY); mraa_pwm_write(pwm2,DUTY); mraa_pwm_write(pwm3,DUTY); mraa_pwm_write(pwm4,DUTY); for(i = LONG_DELAY; i > 0; i--); //20 ms mraa_pwm_write(pwm1,0); mraa_pwm_write(pwm2,0); mraa_pwm_write(pwm3,0); mraa_pwm_write(pwm4,0); for(i = SHORT_DELAY; i > 0; i--); // 5 ms }
upm_result_t speaker_set_frequency(const speaker_context dev, unsigned int freq) { assert(dev != NULL); if (!dev->is_pwm) return UPM_ERROR_NO_RESOURCES; if (freq < 50 || freq > 32000) { printf("%s: freq must be between 50 and 32000.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } float period = 1.0 / (float)freq; // printf("PERIOD: %f (freq %d)\n", period, freq); if (period >= 0.001) // ms range { if (mraa_pwm_period(dev->pwm, period)) { printf("%s: mraa_pwm_period() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } } else // us range. With a max of 32KHz, no less than 3.125 us. { if (mraa_pwm_period_us(dev->pwm, (int)(period * 1000000))) { printf("%s: mraa_pwm_period_us() failed.\n", __FUNCTION__); return UPM_ERROR_OPERATION_FAILED; } } // save it for later if needed dev->default_freq = freq; // A 10% duty cycle enables better results at high frequencies mraa_pwm_write(dev->pwm, 0.1); return UPM_SUCCESS; }
void send_preamble_sequence(int preamble_length) { int i = 0, j = 0; for(i = preamble_length; i > 0; i--){ //equal durations mraa_pwm_write(pwm1,DUTY); mraa_pwm_write(pwm2,DUTY); mraa_pwm_write(pwm3,DUTY); mraa_pwm_write(pwm4,DUTY); for(j = PREAMBLE_DELAY; j > 0; j--); mraa_pwm_write(pwm1,0); mraa_pwm_write(pwm2,0); mraa_pwm_write(pwm3,0); mraa_pwm_write(pwm4,0); for(j = PREAMBLE_DELAY; j > 0; j--); } }
FskErr mraaPWMNew(FskPinPWM *pin, SInt32 number, const char *name) { FskErr err; mraaPWM mpwm; err = FskMemPtrNewClear(sizeof(mraaPWMRecord), &mpwm); if (err) return err; mpwm->context = mraa_pwm_init(number); if (!mpwm->context) { FskMemPtrDispose(mpwm); return kFskErrOperationFailed; } mraa_pwm_period_ms(mpwm->context, PWMDEFAULTPERIOD); mpwm->period = PWMDEFAULTPERIOD; mraa_pwm_write(mpwm->context, 0.0); *pin = (FskPinPWM)mpwm; return err; }
int main() { int sockfd; float brightness; struct sockaddr_in server_addr; char response[MAX_SIZE]; char cmd[MAX_SIZE]; char cmd2[MAX_SIZE]; FILE *fp, *fp2; char component_string[MAX_SIZE]; char value_string[MAX_SIZE]; int value_int; mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm; speed_pwm_in1 = mraa_pwm_init(3); speed_pwm_in2 = mraa_pwm_init(5); turn_pwm = mraa_pwm_init(6); if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) { fprintf(stderr, "Failed to initialized.\n"); return 1; } mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz mraa_pwm_enable(speed_pwm_in1, 1); mraa_pwm_period_us(speed_pwm_in2,870); mraa_pwm_enable(speed_pwm_in2, 1); mraa_pwm_period_ms(turn_pwm,20); mraa_pwm_enable(turn_pwm, 1); mraa_pwm_write(speed_pwm_in1, 1.0f); mraa_pwm_write(speed_pwm_in2, 1.0f); mraa_pwm_write(turn_pwm, CENTER); //Set the protocol to mqtt and //restart iotkit-agent to ensure that it is running. system("iotkit-admin protocol mqtt"); system("systemctl restart iotkit-agent"); //create a socket with the following attributes: socklen_t len = sizeof(server_addr); sockfd = socket(AF_INET, SOCK_DGRAM, 0); bzero(&server_addr,sizeof(server_addr)); //Name of the socket as agreed with the server server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(localhost); server_addr.sin_port = htons(41235); if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { fprintf(stderr, "Failed to bind"); return 1; } while(1) { //Use recvfrom function to send JSON message to iotkit-agent if(recvfrom(sockfd, response, MAX_SIZE, 0, (struct sockaddr *)&server_addr, &len) < 0) { fprintf(stderr, "Failed to receive actuation\n"); return 1; } //printf("Received JSON message: %s\n", response); //---This part extracts the component name---------------- snprintf(cmd, sizeof(cmd), "echo '%s' | sed 's/\"/ /g' | tr ' ' '\n' | awk '/component/{getline; getline; print}'", response); fp = popen(cmd, "r"); if (fp == NULL) { fprintf(stderr, "Failed to run command\n" ); exit(1); } while (fgets(component_string, sizeof(component_string)-1, fp) != NULL) { } pclose(fp); //---This part extracts the value------------------------- snprintf(cmd2, sizeof(cmd2), "echo '%s' | sed 's/\"/ /g' | tr ' ' '\n' | awk '/value/{getline; getline; print}'", response); fp2 = popen(cmd2, "r"); if (fp2 == NULL) { fprintf(stderr, "Failed to run command\n" ); exit(1); } while (fgets(value_string, sizeof(value_string)-1, fp) != NULL) { } pclose(fp2); //printf(value_string); //---This part is for actuation if (!strcmp(component_string, "vehicleControl\n")) { if (!strcmp(value_string, "forward\n")) { printf("Going forward for 1 second.\n\n"); mraa_pwm_write(turn_pwm, CENTER); //straight usleep(100000); speed_control(speed_pwm_in1, speed_pwm_in2, 100); sleep(1); speed_control(speed_pwm_in1, speed_pwm_in2, 0); } else if (!strcmp(value_string, "reverse\n")) { printf("Going backward for 1 second.\n\n"); mraa_pwm_write(turn_pwm, CENTER); //reverse usleep(100000); speed_control(speed_pwm_in1, speed_pwm_in2, -100); sleep(1); speed_control(speed_pwm_in1, speed_pwm_in2, 0); } else if (!strcmp(value_string, "left\n")) { printf("Making a left turn.\n\n"); mraa_pwm_write(turn_pwm, CENTER - 0.015f); //left usleep(100000); speed_control(speed_pwm_in1, speed_pwm_in2, 100); sleep(1); speed_control(speed_pwm_in1, speed_pwm_in2, 0); } else if (!strcmp(value_string, "right\n")) { printf("Making a right turn.\n\n"); mraa_pwm_write(turn_pwm, CENTER + 0.015f); //right usleep(100000); speed_control(speed_pwm_in1, speed_pwm_in2, 100); sleep(1); speed_control(speed_pwm_in1, speed_pwm_in2, 0); } else fprintf(stderr, "Wrong actuation command received!\n"); } } close(sockfd); return 0; }
/** * Set the output duty-cycle percentage, as a float * * @param percentage A floating-point value representing percentage of * output. The value should lie between 0.0f (representing on 0%) and * 1.0f Values above or below this range will be set at either 0.0f or * 1.0f * @return Result of operation */ mraa_result_t write(float percentage) { return mraa_pwm_write(m_pwm, percentage); }
int main() { float speed, turn; char speed_user_input[MAXBUFSIZ]; char turn_user_input[MAXBUFSIZ]; char time_user_input[MAXBUFSIZ]; mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm; speed_pwm_in1 = mraa_pwm_init(3); speed_pwm_in2 = mraa_pwm_init(5); turn_pwm = mraa_pwm_init(6); if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) { fprintf(stderr, "Failed to initialized.\n"); return 1; } mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz mraa_pwm_enable(speed_pwm_in1, 1); mraa_pwm_period_us(speed_pwm_in2,870); mraa_pwm_enable(speed_pwm_in2, 1); mraa_pwm_period_ms(turn_pwm,20); mraa_pwm_enable(turn_pwm, 1); mraa_pwm_write(turn_pwm, CENTER); mraa_pwm_write(speed_pwm_in1, 1.0f); mraa_pwm_write(speed_pwm_in2, 1.0f); RearWheel *rw = new RearWheel(); //go for the curve thread t1; while(1) { t1 = rw->threading(); printf("Turn (L, C, R): "); scanf("%s", turn_user_input); if (!strcmp(turn_user_input, "L") || !strcmp(turn_user_input, "l")) turn = CENTER - 0.015f; else if (!strcmp(turn_user_input, "C") || !strcmp(turn_user_input, "c")) turn = CENTER; else if (!strcmp(turn_user_input, "R") || !strcmp(turn_user_input, "r")) turn = CENTER + 0.018f; else { printf("Wrong turn type!\n"); return 1; } printf("Speed value (-100-100): "); scanf("%s", speed_user_input); speed = atof(speed_user_input); printf("time: "); scanf("%s", time_user_input); int time = atoi(time_user_input); if (speed > 100 || speed < -100) printf("Error: Choose between -100 and 100\n"); else { mraa_pwm_write(turn_pwm, turn); usleep(100000); speed_control(speed_pwm_in1, speed_pwm_in2, speed); } sleep(time); speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f); float count = rw->getCount(); printf("%.2f \n", count); rw->clear(); t1.join(); } return 0; }
int main(int argc, char *argv[]) { printf("\n---------------------ir_transmit.c---------------------\n"); //const struct sched_param priority={1}; //sched_setscheduler(0,SCHED_FIFO,&priority); int transmit_counter = 0; int i = 0; int temp = -1; // GPIO Initialization - Edison has 4 PWM pins pwm1 = mraa_pwm_init(3); mraa_pwm_period_us(pwm1, 26); mraa_pwm_enable(pwm1, 1); pwm2 = mraa_pwm_init(5); mraa_pwm_period_us(pwm2, 26); mraa_pwm_enable(pwm2, 1); pwm3 = mraa_pwm_init(6); mraa_pwm_period_us(pwm3, 26); mraa_pwm_enable(pwm3, 1); pwm4 = mraa_pwm_init(9); mraa_pwm_period_us(pwm4, 26); mraa_pwm_enable(pwm4, 1); printf("\n---------------------Part 0: Apply Settings from Config Files---------------------\n"); temp = check_preamble_length(); if (temp != -1) { printf("\nGot Preamble Length from /etc/IR/preamble_length.txt\n"); printf("ID I got was %d\n", temp); printf("But we are still going to use 5\n", temp); //preamble_length = temp; } temp = -1; // check file for edisonID temp = check_edison_id(); if (temp != -1) { printf("Got Edison ID from /etc/IR/edisonID.txt\n"); printf("ID I got was %d\n", temp); edisonID = temp; } temp = -1; printf("\n---------------------Part 0: Apply Settings from Passed in Arguments---------------------\n"); // if we pass in an argument, use it for the preamble_length, takes // priority over what's in the file. please pass in an integer if (argc == 2) { preamble_length = atoi(argv[1]); printf("Argument(1) Passed In! Preamble Length set to: %d\n", preamble_length); } else { printf("No Argument(1), preamble Length set to default value: %d\n", preamble_length); } // if we pass in an argument, use it for the edisonID, takes // priority over what's in the file. please pass in an integer if (argc == 3) { preamble_length = atoi(argv[1]); printf("Argument(1) Passed In! Preamble Length set to: %d\n", preamble_length); edisonID = atoi(argv[2]); printf("Argument(2) Passed In! edisonID set to: %d\n", edisonID); } else { printf("No Argument(2), edisonID set to default value: %d\n", edisonID); } // This will transmit IR data on all 4 pins at once // on a single Edison board // Continuously Transmit IR Signal when the program is running while(1) { printf("\n---------------------Transmission---------------------\n"); // Updates the preamble_length variable based on info from server temp = check_preamble_length(); if (temp != -1) { printf("ID from /etc/IR_conf/preamble_length.txt was %d\n", temp); preamble_length = temp; } // Preamble - Signals the Receiver Message Incoming printf("Sending preamble_length = %d, ", preamble_length); send_preamble_sequence(preamble_length); // Sending Edison Board ID # - 2 bits, MSB then LSB printf("EdisonID: %d - ", edisonID); switch (edisonID) { case 0: send_low_bit(); // Send lsb bit 0 = LOW send_low_bit(); // Send msb bit 1 = LOW break; case 1: send_low_bit(); // Send msb bit 1 = LOW send_high_bit(); // Send lsb bit 0 = HIGH break; case 2: send_high_bit(); // Send msb bit 1 = HIGH send_low_bit(); // Send lsb bit 0 = LOW break; case 3: send_high_bit(); // Send lsb bit 0 = HIGH send_high_bit(); // Send msb bit 1 = HIGH break; default: send_low_bit(); // Send lsb bit 0 = LOW send_low_bit(); // Send msb bit 1 = LOW } // Sending Edison IR Emitter ID # - 2 bits, MSB then LSB // pwm1,DUTY = 00 = short-long/short-long = 5-20/5-20 // pwm2,DUTY = 01 = short-long/long-short = 5-20/20-5 // pwm3,DUTY = 10 = long-short/short-long = 20-5/5-20 // pwm4,DUTY = 11 = long-short/long-short = 20-5/20-5 printf(", EmitterIDs 0-3\n"); // First Bit mraa_pwm_write(pwm1,DUTY); mraa_pwm_write(pwm2,DUTY); mraa_pwm_write(pwm3,DUTY); mraa_pwm_write(pwm4,DUTY); for(i = SHORT_DELAY; i > 0; i--); // 5 ms mraa_pwm_write(pwm1,0); mraa_pwm_write(pwm2,0); //mraa_pwm_write(pwm3,DUTY, DUTY); //mraa_pwm_write(pwm4,DUTY, DUTY); for(i = MID_DELAY; i > 0; i--); // 15 ms //mraa_pwm_write(pwm1, 0); //mraa_pwm_write(pwm2, 0); mraa_pwm_write(pwm3,0); mraa_pwm_write(pwm4,0); for(i = SHORT_DELAY; i > 0; i--); // 5 ms // Second Bit mraa_pwm_write(pwm1,DUTY); mraa_pwm_write(pwm2,DUTY); mraa_pwm_write(pwm3,DUTY); mraa_pwm_write(pwm4,DUTY); for(i = SHORT_DELAY; i > 0; i--); // 20 ms mraa_pwm_write(pwm1,0); //mraa_pwm_write(pwm2,DUTY, DUTY); mraa_pwm_write(pwm3,0); //mraa_pwm_write(pwm4,DUTY, DUTY); for(i = MID_DELAY; i > 0; i--); // 15 ms //mraa_pwm_write(pwm1, 0); mraa_pwm_write(pwm2,0); //mraa_pwm_write(pwm3, 0); mraa_pwm_write(pwm4,0); for(i = SHORT_DELAY; i > 0; i--); // 5 ms /* send_message_bit(edisonID, 0, DUTY, pwm1,DUTY); send_message_bit(edisonID, 0, DUTY, pwm1,DUTY); send_message_bit(edisonID, 0, DUTY, pwm1,DUTY); send_message_bit(edisonID, 0, DUTY, pwm1,DUTY); for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) { send_message_bit(edisonID, 0, DUTY, pwm1,DUTY); } for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) { send_message_bit(edisonID, 1, DUTY, pwm2,DUTY); } for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) { send_message_bit(edisonID, 2, DUTY, pwm3,DUTY); } for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0 ;transmit_counter--) { send_message_bit(edisonID, 3, DUTY, pwm4,DUTY); } */ }// end while loop mraa_pwm_write (pwm1, 0); mraa_pwm_enable(pwm1, 0); mraa_pwm_write (pwm2, 0); mraa_pwm_enable(pwm2, 0); mraa_pwm_write (pwm3, 0); mraa_pwm_enable(pwm3, 0); mraa_pwm_write (pwm4, 0); mraa_pwm_enable(pwm4, 0); return 0; }//end main
void BuzzerAlarm::stop() { mraa_pwm_period_us(myAlarm, 1); mraa_pwm_write(myAlarm, 0); }
void set_speed(float speed) { mraa_pwm_write(pwm1, speed); mraa_pwm_write(pwm2, speed); }
int main(){ /**********************************************************************/ /**********************************************************************/ /* Begin. */ mraa_uart_context uart; uart = mraa_uart_init(0); mraa_uart_set_baudrate(uart, 115200); char buffer[] = "hhh"; char flush[]="xxxxxxxxxxxx"; char str[] = "HELLO"; if (uart == NULL) { fprintf(stderr, "UART failed to setup\n"); printf("UART failed"); return 1; } printf("firstavail:%d\n",mraa_uart_data_available(uart,0)); while (mraa_uart_data_available(uart,0)) { mraa_uart_read(uart, flush, sizeof(uart)); printf("Flush: %c %c %c %c %c %c %c %c",flush[0], flush[1], flush[2],flush[3],flush[4],flush[5],flush[6],flush[7]); usleep(150); } printf("available: %d",mraa_uart_data_available(uart,0)); char speed_user_input[MAXBUFSIZ]; char turn_user_input[MAXBUFSIZ]; mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm; speed_pwm_in1 = mraa_pwm_init(3); speed_pwm_in2 = mraa_pwm_init(5); turn_pwm = mraa_pwm_init(6); if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) { fprintf(stderr, "Failed to initialized.\n"); return 1; } mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz mraa_pwm_enable(speed_pwm_in1, 1); mraa_pwm_period_us(speed_pwm_in2,870); mraa_pwm_enable(speed_pwm_in2, 1); mraa_pwm_period_ms(turn_pwm,20); mraa_pwm_enable(turn_pwm, 1); mraa_pwm_write(turn_pwm, CENTER); mraa_pwm_write(speed_pwm_in1, 1.0f); mraa_pwm_write(speed_pwm_in2, 1.0f); int n = 5; int spd = 0; char direction = 'C'; char cross_clr = 'B'; float angle = 0.0f; while (1) { //sleep(2); //readCharAry(uart,flush); spd = 50; mraa_uart_read(uart, buffer, 1); if(buffer[0]=='\n') { printf("new line "); mraa_uart_read(uart, buffer, 1); if(buffer[0]=='\n') { printf("new line "); mraa_uart_read(uart, buffer, 1); } } mraa_uart_read(uart, buffer+1, 1); if(buffer[1]=='\n') { buffer[0] = 'h'; buffer[1] = 'h'; } int sign = 0; if(cross_clr == 'M') { speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f); sleep(1); char* nearestBeaconID = (char*)malloc(5); getStrongestBeacon(nearestBeaconID); printf("the nearestBeaconID is: %s\n", nearestBeaconID); sign = 1; angle = CENTER + 0.015f; mraa_pwm_write(turn_pwm, angle); usleep(15000); speed_control(speed_pwm_in1, speed_pwm_in2, 54); printf("speed: %d",spd); usleep(5500000); speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f); //sleep(1); mraa_uart_write(uart, str, 1); //while(!mraa_uart_data_available(uart, 10)){}; mraa_uart_read(uart, buffer, 1); while(buffer[0] == '\n') { mraa_uart_read(uart, buffer, 1); } if(buffer[0] == '\0') direction = 'C'; mraa_uart_read(uart, buffer + 1, 1); cross_clr = buffer[1]; } printf("buff:%c %c %c \n",buffer[0], buffer[1],buffer[2]); if(!sign){ if (direction == 'L') angle = CENTER - 0.005f; if (direction == 'R') angle = CENTER + 0.005f; if (direction == 'C') angle = CENTER;} else { if (direction == 'C') angle = CENTER +0.013f; if (direction == 'L') angle = CENTER +0.005f; if (direction == 'R') angle = CENTER + 0.019f; } mraa_pwm_write(turn_pwm, angle); speed_control(speed_pwm_in1, speed_pwm_in2, spd); printf("speed: %d",spd); usleep(250000); direction = buffer[0]; cross_clr = buffer[1]; } return 0; }