Part 1: How The Installation Of Raspberry Pi With GOBOT (Golang) Looks Like

This all started with developing a simple software to support the robotic project. We had initially planned with Arduino and Gobot (A golang framework for robotics). After we tested some initial sample programs we realised that we can’t run the program or a go binary as a service on Arduino. Because Arduino accepts only instructions and executes them. So the other option was to switch to Arduino sketch. But with Arduino sketch there is a limitation when we do parallel processing. Our aim was to run 3 motors and 5 IR sensors in parallel. After doing some more research we decided to move to Raspberry pi 3.

Once we received the hardware we installed the Raspbian OS on sd card (You can check the instructions here https://www.raspberrypi.org/downloads/raspbian/). After you download the image we can burn it on SD card using this software https://etcher.io/

Initially we started with EasyDriver(https://learn.sparkfun.com/tutorials/easy-driver-hook-up-guide) to control the stepper motor. But we realised as we increased the speed the motor starts jurking.  So after doing some testing with couple of other drivers Leadshine was the promising one (http://www.leadshine.com.ua/pdf/dm320c.pdf). It gives a lot of flexibility in terms of current and steps per rotation(to make motor more smoother).

So now the hardware was setup and ready. Now here comes the Gobot.

Run the Stepper motor at 45 rpm

func Start_motor(r *raspi.Adaptor, exit_channel chan int, distance int, pin string) {
  r := raspi.NewAdaptor()
  // Create the New Driver For Step Pin
  step := gpio.NewDirectPinDriver(r, pin)
  // Disable the Motor initially
  step.DigitalWrite(1)

  start_time := time.Now()
  step_counter := 0
  s := 1600 // Delay in Microseconds
  /* Run the stepper motor continuesly will updated 
     sleep_time based on IR sensor detection */
L:
  for {
   select {
    case s = <-exit_channel:
      if s == -1 {
        break L
      }
    default:
      step.DigitalWrite(0)
      time.Sleep(time.Microsecond * time.Duration(s))
      step.DigitalWrite(1)
      time.Sleep(time.Microsecond * time.Duration(s))

      step_counter++
      // Optionally we can run the motor for N number of steps
      // or keep running continuesly till will send signal on 
      // exit channel
      if distance != 0 && step_counter == distance {
        break L
      }
    }
  }
  // Print the total time taken to cover the given distance
  fmt.Println(time.Since(start_time).Seconds(), step_counter)
  step.DigitalWrite(1)
}

IR Sensor to Monitor RPM

func Start_sensor(r *raspi.Adaptor, c chan interface{}, pin string) {
 # Create the New Driver For IR Sensor
 b := gpio.NewDirectPinDriver(r, pin)
 state := 0
 for {
   select {
    case _, ok := <-c:
      if !ok {
        return
      }
    default:
      newValue, err := b.DigitalRead()
      if err != nil {
        c <- err
      } else if newValue != state && newValue != -1 {
        state = newValue
        c <- newValue
      }
   }
   time.Sleep(time.Millisecond * time.Duration(50))
 }
 return
}

Main Program

import (
 "fmt"
 "time"

 "gobot.io/x/gobot"
 "gobot.io/x/gobot/drivers/gpio"
 "gobot.io/x/gobot/drivers/i2c"
 "gobot.io/x/gobot/platforms/raspi"
)

func main() {
  r := raspi.NewAdaptor()
  sensor_channel := make(chan interface{})
  motor_channel := make(chan int)

  work := func() {
    go Start_sensor(r, sensor_channel, "38")
    go Start_motor(r, motor_channel, 0, "40")
  }

  robot := gobot.NewRobot("MyBot",
   []gobot.Connection{r},
   []gobot.Device{},
   work,
  )

  robot.Start()
}

This will Run both IR Sensor & Stepper motor in background. Ideally we can add exit_channel to Start_sensor function, so when the motor stops we can send the signal on this exit_channel to quit Start_sensor function.

So in this part we saw the basic go programs along with Raspberry PI installations. In Next part we will take a look at how we can use from Touchscreen.

This was much easier part compared to wiring the actual hardware. Have a look at this 🙂

Stepper motor

 

One thought on “Part 1: How The Installation Of Raspberry Pi With GOBOT (Golang) Looks Like

  1. Enabling your robots and drones globally accessible & control with multimedia streaming through sposync.com partnership

    Pion webRTC clients for Ubuntu desktop and Raspberry pi Arm, enabling robot and drone globally accessible & controllable with multimedia streaming

    Light-weight IoT devices and robots/drones not only stream multimedia but also can be controlled with your own custom commands. You can test Pion webRTC clients for Ubuntu Desktop or Raspberry Pi Arm by downloading files from Secure P2P Call&Access section of SPOSYNC.COM. In order for SPOSYNC.COM playing these roles, please contact support@sposync.com for connecting GPIOs or serial ports of your devices for your customized commands set.

    Thanks,
    support@sposync.om
    https://sposync.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.