diff --git a/src/cmd/motor/listen.go b/src/cmd/motor/listen.go index 6db3db1..601e24e 100644 --- a/src/cmd/motor/listen.go +++ b/src/cmd/motor/listen.go @@ -48,7 +48,7 @@ func init() { // Could have been done with a coroutine and a range on a channel if launched from the server thread func listen(){ // Set up a connection to the server. - var addr = fmt.Sprintf("%s:%d", ip, curr_config.Port) //Defined in controller/serve + var addr = fmt.Sprintf("%s:%d", ip, curr_config.Port) //Defined in motor conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err) diff --git a/src/cmd/motor/motor.go b/src/cmd/motor/motor.go index b1325b0..ee5a8eb 100644 --- a/src/cmd/motor/motor.go +++ b/src/cmd/motor/motor.go @@ -62,7 +62,7 @@ func init() { } // Update curr_config from config file -func updateConfig(){ +func updateConfig()(bool){ //Find correct ID in config file i := 0 config_id := fmt.Sprintf("motors.%d.id",i) @@ -70,15 +70,9 @@ func updateConfig(){ // log.Printf("%v / %v", viper.GetInt(config_id), int(motorID)) if viper.GetInt(config_id) == int(motorID) { //Extract config for this motor - err := viper.UnmarshalKey(fmt.Sprintf("motors.%d",i), &curr_config) - if err != nil { - log.Fatalf("unable to decode into struct, %v", err) - break - } - - //Sanity check - if curr_config.Id != motorID { - log.Fatalf("Failed to update config. Requested ID: %d. Got: %d.", motorID, curr_config.Id) + if !ExtractConfig(i, &curr_config) { + //Extraction failed->stop + return false } break } @@ -86,6 +80,21 @@ func updateConfig(){ i++ config_id = fmt.Sprintf("motors.%d.id",i) } - + //Sanity check + if curr_config.Id != motorID { + log.Fatalf("Failed to update config. Requested ID: %d. Got: %d.", motorID, curr_config.Id) + return false + } log.Printf("Using config: %+v", curr_config) + return true +} + +func ExtractConfig(idx int, config *Config)(bool){ + //Extract config for this motor + err := viper.UnmarshalKey(fmt.Sprintf("motors.%d",idx), config) //& + if err != nil { + log.Fatalf("unable to decode into struct, %v", err) + return false + } + return true } \ No newline at end of file diff --git a/src/cmd/motor/moveVel.go b/src/cmd/motor/moveVel.go index 2be8a93..40ec378 100644 --- a/src/cmd/motor/moveVel.go +++ b/src/cmd/motor/moveVel.go @@ -54,7 +54,7 @@ func init() { //gRPC Client func moveVel(cmd_vel float64){ // Set up a connection to the server. - var addr = fmt.Sprintf("%s:%d", ip, curr_config.Port) //Defined in controller/serve + var addr = fmt.Sprintf("%s:%d", ip, curr_config.Port) //Defined in motor conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err)