Intial server/client setup of controller

This commit is contained in:
AntoineH 2024-09-03 18:05:41 +02:00
parent 0f5d2df8a5
commit 01514ad6d2
7 changed files with 120 additions and 12 deletions

View file

@ -6,11 +6,24 @@ package controller
import (
"fmt"
// "strings"
"github.com/spf13/cobra"
"context"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "github.com/AntoineHX/multi-motors-controller/src/proto"
)
var(
addr = "localhost:8080" //TODO: Add to cobra config
tgt_angles = []float64{} //TODO: Add to cobra config
)
//Cobra CLI
// setJointsCmd represents the setJoints command
var setJointsCmd = &cobra.Command{
Use: "setJoints",
@ -18,6 +31,7 @@ var setJointsCmd = &cobra.Command{
Long: `setJoints command descritpion`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("setJoints called with target :", cmd.Flag("vel").Value)
SetJoints()
},
}
@ -34,6 +48,26 @@ func init() {
// is called directly, e.g.:
//TODO: Anonymous flag (with anonymous flag group ?)
//TODO: Fix input of multiple values as slice
setJointsCmd.Flags().Float32Slice("vel", nil, "Target joint values") //or IntSlice ?
setJointsCmd.Flags().Float64SliceVar(&tgt_angles, "vel", nil, "Target joint values") //or IntSlice ?
setJointsCmd.MarkFlagRequired("vel")
}
//gRPC Client
func SetJoints(){
// Set up a connection to the server.
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewMotorsControllerClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SetJoints(ctx, &pb.Angles{Angles: tgt_angles})
if err != nil {
log.Fatalf("could not send: %v", err)
}
log.Printf("Received: %v", r.GetAngles())
}