在gobot中进行转换时传递了太多的参数。

huangapple go评论96阅读模式
英文:

Too many arguments to conversion in gobot

问题

我正在尝试使用gobot框架测试一个LDR。我使用了AnalogSensorDriver设备驱动程序,我的代码如下:

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()
}

当我执行这段代码时,我遇到了以下错误:

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

我对golang和gobot完全是新手。所以,任何帮助解决这个问题的指导都将不胜感激。

提前感谢。

英文:

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.

答案1

得分: 1

从对gobot的源代码的快速浏览中,我觉得你不能在raspi.Adaptor中使用aio.NewAnalogSensorDriveraio.NewAnalogSensorDriver期望它的第一个参数是一个类型为AnalogReader的接口,而raspi.NewAdaptor返回一个raspi.Adaptor,似乎没有实现所需的AnalogRead方法来实现AnalogReader接口。

这就是为什么你会得到这个错误./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)

更新
使你的代码工作取决于你想要做什么。如果你想使用raspi.Adaptor,你不能使用aio.NewAnalogSensorDriver,因为raspi.Adaptor没有模拟功能。如果你想使用aio.NewAnalogSensorDriver,你需要使用它的第一个参数,一个类型实现了AnalogRead方法的值,比如beaglebone.Adaptor

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")

    // ...
}

如果这个示例能让你解决初始错误,如果下面的代码引起其他问题,你应该考虑查阅Gogobot的文档。

英文:

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.

答案2

得分: 1

树莓派没有直接连接模拟设备的方法,所以无论使用哪种框架,你都不能直接将光敏电阻(LDR)连接到主板上。在这种情况下,有两种使用光敏电阻的方法:

  1. 你需要一个模数转换器(ADC),比如MCP3008、ADS1015、ADS1115。

截至2017年4月30日,Gobot.io在Dev分支中支持ADS1015/ADS1115。如果你将设备连接到ADS1015的通道0,然后将转换器连接到树莓派的I2C接口,代码将类似于以下内容:

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)
	// 调整增益以便能够读取至少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. 有一种解决方案可以在没有ADC转换器的情况下使用这些传感器,详细描述在这里

由于光敏电阻基本上是电阻器,即使你的微控制器上没有任何模拟引脚(或者说你想连接更多的光敏电阻器,而你没有足够的模拟输入引脚),你仍然可以使用它们。我们利用了电阻器和电容器的基本电子特性来实现这一点。事实证明,如果你拿一个初始没有电压的电容器,然后通过一个电阻器将其连接到电源(如5V),它将缓慢地充电到电源电压。电阻器越大,充电速度越慢。

你可以在互联网上找到许多关于如何在这种情况下连接光敏电阻的示例。我不确定目前是否有使用Gobot.io的可行示例。

英文:

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.

huangapple
  • 本文由 发表于 2017年3月30日 17:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/43113841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定