main() { // Open i2c: int fh = open("/dev/i2c-3", O_RDWR); printf("Opening i2c port...\n"); if(fh!=3) printf("ERROR opening i2c port.\n"); else printf("The i2c port is open.\n"); // Reset Device printf("Reseting device...\n"); if(resetDev(fh)!=1) printf("ERROR: Device not reset.\n"); else printf("Device is reset and ready for use.\n"); // Set pwm frequency to 60 Hz (different depending on servo): float freq = 60; if(pwmFreq(fh, freq)!=1) printf("ERROR: setting pwm frequency\n"); else printf("Frequency set to %f Hz.\n", freq); // Make loop that rotates servos up and down simultaneously, lapping 10 times uint16_t pl1 = ServoMin;// small pulse length (may need editind depending on servo) uint16_t pl2 = ServoMax;// large pulse length (may need editind depending on servo) int switcher = 0;// Switches between 0 and 1 to change servo directions int loops = 1;// For loops 1-10 int servo, servo1, servo2;// for calling every other servo inside while() loop int servonum = 0; uint16_t pl; while(loops<=10){ printf("Pulse %d:\n", loops); for(servo = 0; servo < 8; servo++) { servo1 = 2*servo;// 0, 2, 4,...14 servo2 = 2*servo+1;// 1, 3, 5,...15 if(switcher == 0) { pwmPulse(fh, servo1, 0, pl1); pwmPulse(fh, servo2, 0, pl2); } else { pwmPulse(fh, servo1, 0, pl2); pwmPulse(fh, servo2, 0, pl1); } } if(switcher == 0) switcher = 1; else switcher = 0; usleep(1000000);// 0.5 sec delay loops++; } resetDev(fh);// Stop pulses }
bool LSM303DHLC_Mag::init( uint8_t odr, uint8_t sens ) { resetDev(); if( send_reg1_8bit( reg_cra, odr ) != 1 ) { return false; } if( send_reg1_8bit( reg_crb, sens ) != 1 ) { return false; } if( send_reg1_8bit( reg_mode, 0 ) != 1 ) { // 0 = continous 1=single_shot return false; } return true; }
void L6470::init(int k_value) { // This is the generic initialization function to set up the Arduino to // communicate with the dSPIN chip. // set up the input/output pins for the application. pinMode(SLAVE_SELECT_PIN, OUTPUT); // The SPI peripheral REQUIRES the hardware SS pin- // pin 10- to be an output. This is in here just // in case some future user makes something other // than pin 10 the SS pin. pinMode(_SSPin, OUTPUT); digitalWrite(_SSPin, HIGH); pinMode(MOSI, OUTPUT); pinMode(MISO, INPUT); pinMode(SCK, OUTPUT); pinMode(BUSYN, INPUT); #if (ENABLE_RESET_PIN == 1) pinMode(RESET, OUTPUT); // reset the dSPIN chip. This could also be accomplished by // calling the "L6470::ResetDev()" function after SPI is initialized. digitalWrite(RESET, HIGH); delay(10); digitalWrite(RESET, LOW); delay(10); digitalWrite(RESET, HIGH); delay(10); #endif // initialize SPI for the dSPIN chip's needs: // most significant bit first, // SPI clock not to exceed 5MHz, // SPI_MODE3 (clock idle high, latch data on rising edge of clock) SPI.begin(); SPI.setBitOrder(MSBFIRST); SPI.setClockDivider(SPI_CLOCK_DIV16); // or 2, 8, 16, 32, 64 SPI.setDataMode(SPI_MODE3); // First things first: let's check communications. The CONFIG register should // power up to 0x2E88, so we can use that to check the communications. if (GetParam(CONFIG) == 0x2E88) { //Serial.println('good to go'); } else { //Serial.println('Comm issue'); } #if (ENABLE_RESET_PIN == 0) resetDev(); #endif // First, let's set the step mode register: // - SYNC_EN controls whether the BUSY/SYNC pin reflects the step // frequency or the BUSY status of the chip. We want it to be the BUSY // status. // - STEP_SEL_x is the microstepping rate- we'll go full step. // - SYNC_SEL_x is the ratio of (micro)steps to toggles on the // BUSY/SYNC pin (when that pin is used for SYNC). Make it 1:1, despite // not using that pin. //SetParam(STEP_MODE, !SYNC_EN | STEP_SEL_1 | SYNC_SEL_1); SetParam(KVAL_RUN, k_value); SetParam(KVAL_ACC, k_value); SetParam(KVAL_DEC, k_value); SetParam(KVAL_HOLD, k_value); // Set up the CONFIG register as follows: // PWM frequency divisor = 1 // PWM frequency multiplier = 2 (62.5kHz PWM frequency) // Slew rate is 290V/us // Do NOT shut down bridges on overcurrent // Disable motor voltage compensation // Hard stop on switch low // 16MHz internal oscillator, nothing on output SetParam(CONFIG, CONFIG_PWM_DIV_1 | CONFIG_PWM_MUL_2 | CONFIG_SR_290V_us| CONFIG_OC_SD_DISABLE | CONFIG_VS_COMP_DISABLE | CONFIG_SW_HARD_STOP | CONFIG_INT_16MHZ); // Configure the RUN KVAL. This defines the duty cycle of the PWM of the bridges // during running. 0xFF means that they are essentially NOT PWMed during run; this // MAY result in more power being dissipated than you actually need for the task. // Setting this value too low may result in failure to turn. // There are ACC, DEC, and HOLD KVAL registers as well; you may need to play with // those values to get acceptable performance for a given application. //SetParam(KVAL_RUN, 0xFF); // Calling GetStatus() clears the UVLO bit in the status register, which is set by // default on power-up. The driver may not run without that bit cleared by this // read operation. getStatus(); hardStop(); //engage motors }