func main() { firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0") servo1 := gpio.NewServoDriver(firmataAdaptor, "5") servo2 := gpio.NewServoDriver(firmataAdaptor, "3") leapAdaptor := leap.NewAdaptor("127.0.0.1:6437") leapDriver := leap.NewDriver(leapAdaptor) work := func() { x := 90.0 z := 90.0 leapDriver.On(leap.MessageEvent, func(data interface{}) { if len(data.(leap.Frame).Hands) > 0 { hand := data.(leap.Frame).Hands[0] x = gobot.ToScale(gobot.FromScale(hand.X(), -300, 300), 30, 150) z = gobot.ToScale(gobot.FromScale(hand.Z(), -300, 300), 30, 150) } }) gobot.Every(10*time.Millisecond, func() { servo1.Move(uint8(x)) servo2.Move(uint8(z)) fmt.Println("Current Angle: ", servo1.CurrentAngle, ",", servo2.CurrentAngle) }) } robot := gobot.NewRobot("pwmBot", []gobot.Connection{firmataAdaptor, leapAdaptor}, []gobot.Device{servo1, servo2, leapDriver}, work, ) robot.Start() }
// PwmWrite writes a PWM signal to the specified pin func (r *Adaptor) PwmWrite(pin string, val byte) (err error) { sysfsPin, err := r.pwmPin(pin) if err != nil { return err } return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, gobot.FromScale(float64(val), 0, 255))) }
// PwmWrite writes the 0-254 value to the specified pin func (e *Adaptor) PwmWrite(pin string, val byte) (err error) { sysPin := sysfsPinMap[pin] if sysPin.pwmPin != -1 { if e.pwmPins[sysPin.pwmPin] == nil { if err = e.DigitalWrite(pin, 1); err != nil { return } e.pwmPins[sysPin.pwmPin] = newPwmPin(sysPin.pwmPin) if err = e.pwmPins[sysPin.pwmPin].export(); err != nil { return } if err = e.pwmPins[sysPin.pwmPin].enable("1"); err != nil { return } } p, err := e.pwmPins[sysPin.pwmPin].period() if err != nil { return err } period, err := strconv.Atoi(p) if err != nil { return err } duty := gobot.FromScale(float64(val), 0, 255.0) return e.pwmPins[sysPin.pwmPin].writeDuty(strconv.Itoa(int(float64(period) * duty))) } return errors.New("Not a PWM pin") }
// pwmWrite writes pwm value to specified pin func (b *Adaptor) pwmWrite(pin string, val byte) (err error) { i, err := b.pwmPin(pin) if err != nil { return } period := 500000.0 duty := gobot.FromScale(float64(val), 0, 255.0) return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty))) }
// ServoWrite writes the 0-180 degree val to the specified pin. func (b *Adaptor) ServoWrite(pin string, val byte) (err error) { i, err := b.pwmPin(pin) if err != nil { return err } period := 16666666.0 duty := (gobot.FromScale(float64(val), 0, 180.0) * 0.115) + 0.05 return b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty))) }
// ServoWrite writes a servo signal to the specified pin func (r *Adaptor) ServoWrite(pin string, angle byte) (err error) { sysfsPin, err := r.pwmPin(pin) if err != nil { return err } val := (gobot.ToScale(gobot.FromScale(float64(angle), 0, 180), 0, 200) / 1000.0) + 0.05 return r.piBlaster(fmt.Sprintf("%v=%v\n", sysfsPin, val)) }
func main() { beagleboneAdaptor := beaglebone.NewAdaptor() sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor, "P9_33") led := gpio.NewLedDriver(beagleboneAdaptor, "P9_14") work := func() { sensor.On(sensor.Event("data"), func(data interface{}) { brightness := uint8( gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), ) fmt.Println("sensor", data) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{sensor, led}, work, ) robot.Start() }
func main() { e := edison.NewAdaptor() sensor := aio.NewAnalogSensorDriver(e, "0") led := gpio.NewLedDriver(e, "3") work := func() { sensor.On(aio.Data, func(data interface{}) { brightness := uint8( gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 4096), 0, 255), ) fmt.Println("sensor", data) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{e}, []gobot.Device{sensor, led}, work, ) master.Start() }
func main() { firmataAdaptor := firmata.NewAdaptor("/dev/ttyACM0") sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "0") led := gpio.NewLedDriver(firmataAdaptor, "3") work := func() { sensor.On(gpio.Data, func(data interface{}) { brightness := uint8( gobot.ToScale(gobot.FromScale(float64(data.(int)), 0, 1024), 0, 255), ) fmt.Println("sensor", data) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{firmataAdaptor}, []gobot.Device{sensor, led}, work, ) robot.Start() }
func ExampleFromScale() { fmt.Println(gobot.FromScale(5, 0, 10)) // Output: // 0.5 }
func scale(position float64) uint8 { return uint8(gobot.ToScale(gobot.FromScale(position, 0, 1), 0, 255)) }