func main() { gbot := gobot.NewGobot() firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0") servo1 := gpio.NewServoDriver(firmataAdaptor, "servo", "5") servo2 := gpio.NewServoDriver(firmataAdaptor, "servo", "3") leapAdaptor := leap.NewLeapMotionAdaptor("leap", "127.0.0.1:6437") leapDriver := leap.NewLeapMotionDriver(leapAdaptor, "leap") work := func() { x := 90.0 z := 90.0 gobot.On(leapDriver.Events["Message"], 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) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("pwmBot", []gobot.Connection{firmataAdaptor, leapAdaptor}, []gobot.Device{servo1, servo2, leapDriver}, work)) gbot.Start() }
func (m *Motion) moveCam(p *rocproto.Packet) error { if p.Cam == nil { return errors.New(fmt.Sprintf("Mouvement error: moving camera ==> %v", p)) } x := uint8(gobot.ToScale(gobot.FromScale(p.Cam.X, -90, 90), 0, 180)) y := uint8(gobot.ToScale(gobot.FromScale(p.Cam.Y, -35, 35), 90, 180)) m.X = float64(x) m.Y = float64(y) m.servoX.Move(x) m.servoY.Move(y) return m.getCamPos(p) }
// PwmWrite writes the 0-254 value to the specified pin func (e *EdisonAdaptor) 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 } if err = changePinMode(strconv.Itoa(int(sysPin.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 the 0-254 value to the specified pin func (e *EdisonAdaptor) PwmWrite(pin string, val byte) (err error) { sysPin := e.sysfsPinMap[pin] if sysPin.pwmPin != -1 { if e.pwmPins[sysPin.pwmPin] == nil { if err = e.DigitalWrite(pin, 1); err != nil { return } // FIXME: is this really the GPIO pin or the pwmPin we need to tweak ?? // seems like an oversight, doesn't it? if err = changePinMode(strconv.Itoa(int(sysPin.gpioPin)), "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") }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor, "sensor", "P9_33") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "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, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() edisonAdaptor := edison.NewEdisonAdaptor("edison") blinkm := i2c.NewBlinkMDriver(edisonAdaptor, "blinkm") sensor := gpio.NewAnalogSensorDriver(edisonAdaptor, "sensor", "2") work := func() { gobot.On(sensor.Event("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) blinkm.Rgb(0, brightness, 0) }) } robot := gobot.NewRobot("sensorBot", []gobot.Connection{edisonAdaptor}, []gobot.Device{blinkm, sensor}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() e := edison.NewEdisonAdaptor("edison") sensor := gpio.NewAnalogSensorDriver(e, "sensor", "0") led := gpio.NewLedDriver(e, "led", "3") work := func() { sensor.On(gpio.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, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() firmataAdaptor := firmata.NewFirmataAdaptor("firmata", "/dev/ttyACM0") sensor := gpio.NewAnalogSensorDriver(firmataAdaptor, "sensor", "0") led := gpio.NewLedDriver(firmataAdaptor, "led", "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, ) gbot.AddRobot(robot) gbot.Start() }
func (r *RaspiAdaptor) 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 pwm value to specified pin func (b *BeagleboneAdaptor) 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 *BeagleboneAdaptor) 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))) }
func (r *RaspiAdaptor) 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 (m *Motion) move(p *rocproto.Packet) error { var r int64 = 50 if p.Mv == nil { return errors.New(fmt.Sprintf("Mouvement error, using motors ==> %v", p)) } theta := int64(p.Mv.Angle * 180 / math.Pi) theta = ((theta + 180) % 360) - 180 v_a := r * (45 - theta%90) / 45 // falloff of main motor v_b := misc.Min(100, 2*r+v_a, 2*r-v_a) // compensation of other motor lR, rR := thrust(theta, v_a, v_b) lS := gobot.ToScale(gobot.FromScale(CALCSPEED*(float64(lR)/100), -90, 90), 0, 180) rS := gobot.ToScale(gobot.FromScale(CALCSPEED*(float64(rR)/100), -90, 90), 0, 180) p.Mv.Speed = float64(lR+rR) / 2 gobot.Publish(m.Event("move"), *p.Mv) m.resetTime <- true m.motorL.Move(uint8(lS)) m.motorR.Move(uint8(90 - (rS - 90))) fmt.Println(lS, 90-(rS-90)) return nil }
/* This function is used if only the rotary port is in play. func mapValueToColor(i int) (int, int, int) { // Masking to account for very sensitive rotary red := ((i & (1 << 1)) << 2) | ((i & (1 << 4)) << 0) | ((i & (1 << 7)) >> 2) green := ((i & (1 << 2)) << 1) | ((i & (1 << 5)) >> 1) | ((i & (1 << 8)) >> 3) blue := ((i & (1 << 3)) << 0) | ((i & (1 << 6)) >> 2) | ((i & (1 << 9)) >> 4) return red, green, blue } */ func main() { gbot := gobot.NewGobot() board := edison.NewEdisonAdaptor("edison") screen := i2c.NewGroveLcdDriver(board, "screen") sensorRotary := gpio.NewGroveRotaryDriver(board, "sensor", "0") sensorLight := gpio.NewGroveLightSensorDriver(board, "light", "1") sensorTemp := gpio.NewGroveTemperatureSensorDriver(board, "temp", "2") work := func() { var r, g, b int gobot.On(sensorRotary.Event("data"), func(data interface{}) { r = data.(int) >> 2 }) gobot.On(sensorLight.Event("data"), func(data interface{}) { fmt.Printf("%d\n", data) g = data.(int) * 255 / 790 }) gobot.Every(time.Millisecond*500, func() { b = int(gobot.ToScale(gobot.FromScale(sensorTemp.Temperature(), 25, 35), 0, 255)) screen.Clear() screen.Home() screen.SetRGB(r, g, b) screen.Write(fmt.Sprintf("#%02X%02X%02X", r, g, b)) }) } robot := gobot.NewRobot("screenBot", []gobot.Connection{board}, []gobot.Device{screen, sensorRotary, sensorLight, sensorTemp}, work, ) gbot.AddRobot(robot) gbot.Start() }
func main() { gbot := gobot.NewGobot() beagleboneAdaptor := beaglebone.NewBeagleboneAdaptor("beaglebone") sensor := gpio.NewAnalogSensorDriver(beagleboneAdaptor, "sensor", "P9_33") led := gpio.NewLedDriver(beagleboneAdaptor, "led", "P9_14") work := func() { gobot.Every(100*time.Millisecond, func() { val := sensor.Read() brightness := uint8(gobot.ToScale(gobot.FromScale(float64(val), 0, 1024), 0, 255)) fmt.Println("sensor", val) fmt.Println("brightness", brightness) led.Brightness(brightness) }) } gbot.Robots = append(gbot.Robots, gobot.NewRobot("sensorBot", []gobot.Connection{beagleboneAdaptor}, []gobot.Device{sensor, led}, work)) gbot.Start() }
func (ia *AI) light() error { var err error d := &DataLux{iter: 0, iterMax: 0, lux: -1, iterMaxLux: 0} timeout := time.After(3 * time.Second) tick := time.NewTicker(100 * time.Millisecond) ia.toggle(true) defer ia.toggle(false) for { select { case <-timeout: d.iterMax = d.iter log.Println("Max lux is at: ", d.getAngle(), " degrees") coord := ia.getPos(nil).(rocproto.Coord) coord.Lat = coord.Lat + math.Cos(d.getAngle()) coord.Long = coord.Long + math.Sin(d.getAngle()) p := goPack.Prepare(uint32(rocproto.AiCodes_DLIGHT), rocproto.Packet_DATA, rocproto.Packet_CONTROL_SERVER, rocproto.Packet_VIDEO_CLIENT) p.Coord = &coord if err != nil { return err } tick.Stop() return ia.Send(p) case <-tick.C: v, err := ia.sensorLight.Read() if misc.CheckError(err, "reading light sensor in photoresistor.go", false) != nil { return err } d.iter += 1 temp := gobot.ToScale(gobot.FromScale(float64(v), 0, 1024), 0, 255) //fmt.Println("Sensor data", temp) if diffIsCorrect(d.lux, temp) && temp > d.lux { d.lux = temp d.iterMaxLux = d.iter } } } }
func (b *BeagleboneAdaptor) ServoWrite(pin string, val byte) { i := b.pwmPin(pin) period := 20000000.0 duty := gobot.FromScale(float64(^val), 0, 180.0) b.pwmPins[i].pwmWrite(strconv.Itoa(int(period)), strconv.Itoa(int(period*duty))) }
func scale(position float64) uint8 { return uint8(gobot.ToScale(gobot.FromScale(position, 0, 1), 0, 255)) }
func ExampleFromScale() { fmt.Println(gobot.FromScale(5, 0, 10)) // Output: // 0.5 }