func setSpeed(folder string, speed int16) { regulationMode := utilities.ReadStringValue(folder, regulationModeFD) switch regulationMode { case "on": utilities.WriteIntValue(folder, speedSetterFD, int64(speed)) case "off": if speed > 100 || speed < -100 { log.Fatal("The speed must be in range [-100, 100]") } utilities.WriteIntValue(folder, powerSetterFD, int64(speed)) } }
func findSensor(port InPort, t Type) string { sensors, _ := ioutil.ReadDir(baseSensorPath) for _, item := range sensors { if strings.HasPrefix(item.Name(), "sensor") { sensorPath := fmt.Sprintf("%s/%s", baseSensorPath, item.Name()) portr := utilities.ReadStringValue(sensorPath, "address") if InPort(portr) == port { typer := utilities.ReadStringValue(sensorPath, "driver_name") if Type(typer) == t { return item.Name() } } } } log.Fatalf("Could not find %v sensor on port %v\n", t, port) return "" }
// Runs the motor at the given port. // The meaning of `speed` parameter depends on whether the regulation mode is turned on or off. // // When the regulation mode is off (by default) `speed` ranges from -100 to 100 and // it's absolute value indicates the percent of motor's power usage. It can be roughly interpreted as // a motor speed, but deepending on the environment, the actual speed of the motor // may be lower than the target speed. // // When the regulation mode is on (has to be enabled by EnableRegulationMode function) the motor // driver attempts to keep the motor speed at the `speed` value you've specified // which ranges from about -1000 to 1000. The actual range depends on the type of the motor - see ev3dev docs. // // Negative values indicate reverse motion regardless of the regulation mode. func Run(port OutPort, speed int16) { folder := findFolder(port) regulationMode := utilities.ReadStringValue(folder, regulationModeFD) switch regulationMode { case "on": utilities.WriteIntValue(folder, speedSetterFD, int64(speed)) utilities.WriteStringValue(folder, runFD, "run-forever") case "off": if speed > 100 || speed < -100 { log.Fatal("The speed must be in range [-100, 100]") } utilities.WriteIntValue(folder, powerSetterFD, int64(speed)) utilities.WriteStringValue(folder, runFD, "run-forever") } }
func findFolder(port OutPort) string { if _, err := os.Stat(rootMotorPath); os.IsNotExist(err) { log.Fatal("There are no motors connected") } rootMotorFolder, _ := os.Open(rootMotorPath) motorFolders, _ := rootMotorFolder.Readdir(-1) if len(motorFolders) == 0 { log.Fatal("There are no motors connected") } for _, folderInfo := range motorFolders { folder := folderInfo.Name() motorPort := utilities.ReadStringValue(path.Join(rootMotorPath, folder), portFD) if motorPort == "out"+string(port) { return path.Join(rootMotorPath, folder) } } log.Fatal("No motor is connected to port ", port) return "" }
// Read motor state func ReadState(port OutPort) string { return utilities.ReadStringValue(findFolder(port), stateFD) }