Prevent motor startup if config extraction failled

This commit is contained in:
AntoineH 2024-09-04 17:31:33 +02:00
parent d3ba2c1134
commit 73ee02f1c7
3 changed files with 22 additions and 13 deletions

View file

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

View file

@ -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
}

View file

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