func adafruitDCMotorRunner(a *i2c.AdafruitMotorHatDriver, dcMotor int) (err error) {
	log.Printf("DC Motor Run Loop...\n")
	// set the speed:
	var speed int32 = 255 // 255 = full speed!
	if err = a.SetDCMotorSpeed(dcMotor, speed); err != nil {
		return
	}
	// run FORWARD
	if err = a.RunDCMotor(dcMotor, i2c.AdafruitForward); err != nil {
		return
	}
	// Sleep and RELEASE
	time.Sleep(2000 * time.Millisecond)
	if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
		return
	}
	// run BACKWARD
	if err = a.RunDCMotor(dcMotor, i2c.AdafruitBackward); err != nil {
		return
	}
	// Sleep and RELEASE
	time.Sleep(2000 * time.Millisecond)
	if err = a.RunDCMotor(dcMotor, i2c.AdafruitRelease); err != nil {
		return
	}
	return
}
示例#2
0
func adafruitServoMotorRunner(a *i2c.AdafruitMotorHatDriver) (err error) {
	log.Printf("Servo Motor Run Loop...\n")
	// Changing from the default 0x40 address because this configuration involves
	// a Servo HAT stacked on top of a DC/Stepper Motor HAT on top of the Pi.
	stackedHatAddr := 0x41
	var channel byte = 1
	deg := 90

	// update the I2C address state
	a.SetServoHatAddress(stackedHatAddr)

	// Do not need to set this every run loop
	freq := 60.0
	if err = a.SetServoMotorFreq(freq); err != nil {
		log.Printf("%s", err.Error())
		return
	}
	// start in the middle of the 180-deg range
	pulse := degree2pulse(deg)
	if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
		log.Printf(err.Error())
		return
	}
	// INCR
	pulse = degree2pulse(deg + degIncrease)
	if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
		log.Printf(err.Error())
		return
	}
	time.Sleep(2000 * time.Millisecond)
	// DECR
	pulse = degree2pulse(deg - degIncrease)
	if err = a.SetServoMotorPulse(channel, 0, pulse); err != nil {
		log.Printf(err.Error())
		return
	}
	return
}
func adafruitStepperMotorRunner(a *i2c.AdafruitMotorHatDriver, motor int) (err error) {
	log.Printf("Stepper Motor Run Loop...\n")
	// set the speed state:
	speed := 30 // rpm
	style := i2c.AdafruitDouble
	steps := 20

	a.SetStepperMotorSpeed(motor, speed)

	if err = a.Step(motor, steps, i2c.AdafruitForward, style); err != nil {
		log.Printf(err.Error())
		return
	}
	if err = a.Step(motor, steps, i2c.AdafruitBackward, style); err != nil {
		log.Printf(err.Error())
		return
	}
	return
}