func main() { flag.Parse() sb := servoblaster.New() defer sb.Close() pwm := sb.Channel(0) servo := servo.New(pwm) c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) turnTimer := time.Tick(500 * time.Millisecond) left := true servo.SetAngle(90) defer func() { servo.SetAngle(90) }() for { select { case <-turnTimer: left = !left var err error switch left { case true: err = servo.SetAngle(45) case false: err = servo.SetAngle(135) } if err != nil { panic(err) } case <-c: return } } }
func main() { glog.Info("main: starting up") flag.Parse() var car Car = NullCar if !*fakeCar { if err := embd.InitI2C(); err != nil { panic(err) } defer embd.CloseI2C() bus := embd.NewI2CBus(byte(*i2cBusNo)) var cam Camera = NullCamera if !*fakeCam { cam = NewCamera(*camWidth, *camHeight, *camTurnImage, *camFps) } defer cam.Close() cam.Run() var comp Compass = NullCompass if !*fakeCompass { comp = NewCompass(bus) } defer comp.Close() var rf RangeFinder = NullRangeFinder if !*fakeRangeFinder { thermometer := bmp180.New(bus) defer thermometer.Close() if err := embd.InitGPIO(); err != nil { panic(err) } defer embd.CloseGPIO() echoPin, err := embd.NewDigitalPin(*echoPinNumber) if err != nil { panic(err) } triggerPin, err := embd.NewDigitalPin(*triggerPinNumber) if err != nil { panic(err) } rf = NewRangeFinder(echoPin, triggerPin, thermometer) } defer rf.Close() var fw FrontWheel = NullFrontWheel if !*fakeFrontWheel { sb := servoblaster.New() defer sb.Close() pwm := sb.Channel(*sbChannel) servo := servo.New(pwm) fw = &frontWheel{servo} } defer fw.Turn(0) var engine Engine = NullEngine if !*fakeEngine { ctrl := pca9685.New(bus, 0x41) defer ctrl.Close() pwm := ctrl.AnalogChannel(15) engine = NewEngine(pwm) } defer engine.Stop() var gyro Gyroscope = NullGyroscope if !*fakeGyro { gyro = NewGyroscope(bus, l3gd20.R250DPS) } defer gyro.Close() car = NewCar(bus, cam, comp, rf, gyro, fw, engine) } defer car.Close() ws := NewWebServer(car) ws.Run() quit := make(chan os.Signal, 1) signal.Notify(quit, os.Interrupt, os.Kill) <-quit glog.Info("main: all done") }