Add getJoint function & Share ip/port declaration

This commit is contained in:
AntoineH 2024-09-04 11:35:29 +02:00
parent 01514ad6d2
commit d950ba6d1d
4 changed files with 49 additions and 14 deletions

View file

@ -8,15 +8,26 @@ import (
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"context"
"log"
"time"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
pb "github.com/AntoineHX/multi-motors-controller/src/proto"
) )
//Cobra CLI
// getJointsCmd represents the getJoints command // getJointsCmd represents the getJoints command
var getJointsCmd = &cobra.Command{ var getJointsCmd = &cobra.Command{
Use: "getJoints", Use: "getJoints",
Short: "getJoints command descritpion", Short: "getJoints command descritpion",
Long: `getJoints command descritpion`, Long: `getJoints command descritpion`,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println("getJoints called") // fmt.Println("getJoints called")
GetJoints()
}, },
} }
@ -33,3 +44,24 @@ func init() {
// is called directly, e.g.: // is called directly, e.g.:
// getJointsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // getJointsCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
//gRPC Client
func GetJoints(){
// Set up a connection to the server.
var addr = fmt.Sprintf("%s:%d", ip, port) //Defined in controller/serve
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.GetJoints(ctx, &pb.Empty{})
if err != nil {
log.Fatalf("could not send: %v", err)
}
log.Printf("Angles (°): %v", r.GetAngles()) //TODO: Format with °
}

View file

@ -17,7 +17,8 @@ import (
) )
var ( var (
port uint16 ip string = "localhost" //localhost=127.0.0.1
port uint16 = 8080 //TODO: Share with config file or env var
) )
// Cobra CLI // Cobra CLI
// serveCmd represents the serve command // serveCmd represents the serve command
@ -58,12 +59,12 @@ func (s *server) SetJoints(ctx context.Context, in *pb.Angles) (*pb.Angles, erro
return &pb.Angles{Angles: in.GetAngles()}, nil return &pb.Angles{Angles: in.GetAngles()}, nil
} }
//TODO: Fix compiling issue with Empty message //TODO: Fix compiling issue with google.protobuf.Empty message
// func (s *server) GetJoints(ctx context.Context, in *pb.emptypb.Empty) (*pb.Angles, error) { func (s *server) GetJoints(ctx context.Context, in *pb.Empty) (*pb.Angles, error) {
// var angles = []float64{0, 0, 0} var angles = []float64{0, 0, 0}
// log.Printf("Sending: %v", angles) log.Printf("Sending: %v", angles)
// return &pb.Angles{Angles: angles}, nil return &pb.Angles{Angles: angles}, nil
// } }
func serve() { func serve() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))

View file

@ -19,7 +19,6 @@ import (
) )
var( var(
addr = "localhost:8080" //TODO: Add to cobra config
tgt_angles = []float64{} //TODO: Add to cobra config tgt_angles = []float64{} //TODO: Add to cobra config
) )
@ -55,6 +54,7 @@ func init() {
//gRPC Client //gRPC Client
func SetJoints(){ func SetJoints(){
// Set up a connection to the server. // Set up a connection to the server.
var addr = fmt.Sprintf("%s:%d", ip, port) //Defined in controller/serve
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil { if err != nil {
log.Fatalf("did not connect: %v", err) log.Fatalf("did not connect: %v", err)

View file

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
import "google/protobuf/empty.proto"; //import "google/protobuf/empty.proto"; //google.protobuf.Empty
option go_package = ".;motorprotos"; option go_package = ".;motorprotos";
@ -9,12 +9,12 @@ package motorprotos;
// Server converting target angles to velocity profile for mo // Server converting target angles to velocity profile for mo
service MotorsController { service MotorsController {
rpc SetJoints(Angles) returns (Angles) {} rpc SetJoints(Angles) returns (Angles) {}
rpc GetJoints(google.protobuf.Empty) returns (Angles) {} rpc GetJoints(Empty) returns (Angles) {}
} }
// Server converting target angles to velocity profile for mo // Server converting target angles to velocity profile for mo
//service Motor { //service Motor {
// rpc SetVolicty(Velocity) returns (google.protobuf.Empty) {} // rpc SetVolicty(Velocity) returns (Empty) {}
// rpc ReceiveDataStream(google.protobuf.Empty) returns (stream // rpc ReceiveDataStream(Empty) returns (stream
//} //}
message Angles { message Angles {
@ -30,3 +30,5 @@ message MotorData {
double velocity = 2; double velocity = 2;
string error = 3; // Will be filled if motor encounter an error string error = 3; // Will be filled if motor encounter an error
} }
message Empty {}