0

I was trying to test a LDR using gobot framework. I used AnalogSensorDriver device driver and my code is

    package main

import (
    "time"

    "gobot.io/x/gobot"
    "gobot.io/x/gobot/drivers/aio"
    "gobot.io/x/gobot/platforms/raspi"
)

func main() {
    r := raspi.NewAdaptor()
    ldr := aio.AnalogSensorDriver(r, "7")

    work := func() {
        gobot.Every(1*time.Second, func() {
            ldr.Read()
        })
    }

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

    robot.Start()
}

When I execute this, I am getting this error.

./ldrtest.go:13: too many arguments to conversion to aio.AnalogSensorDriver: aio.AnalogSensorDriver(w, "7") ./ldrtest.go:22: undefined: w

I am totally new for golang and gobot. So any kind of help to solve this problem would be greatly appreciated.

Thanks in advance.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sachith Muhandiram
  • 2,819
  • 10
  • 45
  • 94
  • You need to define the `w` variable before you try to use it on line 22. – mkopriva Mar 30 '17 at 09:37
  • can you tell me how to define it? – Sachith Muhandiram Mar 30 '17 at 09:38
  • And `AnalogSensorDriver` seems to be a `struct`, you should use the `NewAnalogSensorDriver` func to initialize one. – mkopriva Mar 30 '17 at 09:39
  • I don't know what you want `w` to be, but you define variables in Go like this `w := "string value"` or like this `var w = "string value"`. But as i mentioned in the previous comment `AnalogSensorDriver` is a type and using it like this `AnalogSensorDriver(w, "7")` is never gonna work, whether `w` is intialized or not. – mkopriva Mar 30 '17 at 09:42
  • now I am getting this after correcting mistakes in my code >./ldrtest.go:13: cannot use r (type *raspi.Adaptor) as type aio.AnalogReader in argument to aio.NewAnalogSensorDriver: *raspi.Adaptor does not implement aio.AnalogReader (missing AnalogRead method) – Sachith Muhandiram Mar 30 '17 at 09:53

2 Answers2

1

From a quick glance at gobot's source it seems to me that you cannot use aio.NewAnalogSensorDriver with the raspi.Adaptor. The aio.NewAnalogSensorDriver expects it's first argument to be an interface of type AnalogReader while the raspi.NewAdaptor returns a raspi.Adaptor that, seemingly, does not implement the AnalogRead method required for it to implement the AnalogReader interface.

That's why you get that error ./ldrtest.go:13: cannot use r (type *raspi.Adaptor) as type aio.AnalogReader in argument to aio.NewAnalogSensorDriver: *raspi.Adaptor does not implement aio.AnalogReader (missing AnalogRead method).

Update: Making your code work depends on what you want to do. If you want to use raspi.Adaptor you cannot use aio.NewAnalogSensorDriver because the raspi.Adaptor does not have analog capabilities. If you want to use the aio.NewAnalogSensorDriver you'll need to use, as its first argument, a value whose type implements the AnalogRead method, like for example the beaglebone.Adaptor does.

package main

import (
    "time"

    "gobot.io/x/gobot"
    "gobot.io/x/gobot/drivers/aio"
    "gobot.io/x/gobot/platforms/beaglebone"
)

func main() {
    r := beaglebone.NewAdaptor()
    ldr := aio.NewAnalogSensorDriver(r, "7")

    // ...
}

This example should get you past that initial error, if the code below causes other issues you should consider consulting the documentation for both Go and gobot.

mkopriva
  • 35,176
  • 4
  • 57
  • 71
  • so how can I solve this problem? I am totally new for both golang and gobot. – Sachith Muhandiram Mar 30 '17 at 10:12
  • thanks, but the thing is I want to execute this on raspberry pi board – Sachith Muhandiram Mar 30 '17 at 10:28
  • panic: runtime error: index out of range goroutine 1 [running]: panic(0xf6188, 0x1040a030) /usr/local/go/src/runtime/panic.go:500 +0x33c – Sachith Muhandiram Mar 30 '17 at 10:33
  • In that case you need to use a different driver. Try copy pasting the example from here https://github.com/hybridgroup/gobot/tree/master/platforms/raspi#how-to-use, does that work? – mkopriva Mar 30 '17 at 10:39
  • Yes, I did this test and led was working. http://raspberrypi.stackexchange.com/questions/64006/raspberry-led-python-blinking-does-not-work/64009?noredirect=1#comment100186_64009 – Sachith Muhandiram Mar 30 '17 at 10:46
  • **I could be wrong** but it seems to me that at this moment [gpio](https://github.com/hybridgroup/gobot/tree/master/drivers/gpio#hardware-support) doesn't support a light sensor driver, which i believe is what you need, so you might be out of luck using Go+gobot for now? Again, I could be wrong about this. – mkopriva Mar 30 '17 at 11:00
1

The raspberry pi does not have a way to connect analog devices directly, so, whatever the framework, you cannot simply attach the photoresistor (LDR) directly to the board. There are 2 ways to use photoresistors in that case:

  1. You need an analog-digital converter (ADC), such as the MCP3008, ADS1015, ADS1115.

As of April, 30th, 2017, Gobot.io supports the ADS1015/ADS1115 in the Dev branch. If you connect your device to an ADS1015 on the channel 0, then the converter to the I2C interface of the raspberry pi, the code would look like that:

package main

import (
    "fmt"
    "time"

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

func main() {
    a := raspi.NewAdaptor()
    ads1015 := i2c.NewADS1015Driver(a)
    // Adjust the gain to be able to read values of at least 5V
    ads1015.DefaultGain, _ = ads1015.BestGainForVoltage(5.0)

    work := func() {
        gobot.Every(100*time.Millisecond, func() {
            v, _ := ads1015.ReadWithDefaults(0)
            fmt.Println("A0", v)
        })
    }

    robot := gobot.NewRobot("ads1015bot",
        []gobot.Connection{a},
        []gobot.Device{ads1015},
        work,
    )

    robot.Start()
}
  1. There is a solution to use such sensors without ADC converters, described here:

Because photocells are basically resistors, its possible to use them even if you don't have any analog pins on your microcontroller (or if say you want to connect more than you have analog input pins). The way we do this is by taking advantage of a basic electronic property of resistors and capacitors. It turns out that if you take a capacitor that is initially storing no voltage, and then connect it to power (like 5V) through a resistor, it will charge up to the power voltage slowly. The bigger the resistor, the slower it is.

You can find many examples on the internet on how to connect the LDR in such case. I'm not sure there is a working example with Gobot.io right now.

Beg2k1
  • 61
  • 5