gobot.io/x/gobot/v2@v2.1.0/examples/jetson-nano_servo.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  package main
     8  
     9  //
    10  // before to run
    11  // 1. check Jetson io pwm pin configure `sudo /opt/nvidia/jetson-io/jetson-io.py`
    12  // 2. if end pin configure, reboot Jetson nano.
    13  // 3. run gobot
    14  import (
    15  	"log"
    16  	"time"
    17  
    18  	"gobot.io/x/gobot/v2"
    19  	"gobot.io/x/gobot/v2/drivers/gpio"
    20  	"gobot.io/x/gobot/v2/platforms/jetson"
    21  )
    22  
    23  func main() {
    24  
    25  	jetsonAdaptor := jetson.NewAdaptor()
    26  	servo := gpio.NewServoDriver(jetsonAdaptor, "32")
    27  
    28  	counter := 0
    29  	flg := true
    30  	work := func() {
    31  		gobot.Every(100*time.Millisecond, func() {
    32  
    33  			log.Println("Turning", counter)
    34  			servo.Move(uint8(counter))
    35  			if counter == 140 {
    36  				flg = false
    37  			} else if counter == 30 {
    38  				flg = true
    39  			}
    40  
    41  			if flg {
    42  				counter = counter + 1
    43  			} else {
    44  				counter = counter - 1
    45  
    46  			}
    47  		})
    48  
    49  	}
    50  
    51  	robot := gobot.NewRobot("Jetsonservo",
    52  		[]gobot.Connection{jetsonAdaptor},
    53  		[]gobot.Device{servo},
    54  		work,
    55  	)
    56  
    57  	robot.Start()
    58  }