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"
"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
var getJointsCmd = &cobra.Command{
Use: "getJoints",
Short: "getJoints command descritpion",
Long: `getJoints command descritpion`,
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.:
// 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 (
port uint16
ip string = "localhost" //localhost=127.0.0.1
port uint16 = 8080 //TODO: Share with config file or env var
)
// Cobra CLI
// 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
}
//TODO: Fix compiling issue with Empty message
// func (s *server) GetJoints(ctx context.Context, in *pb.emptypb.Empty) (*pb.Angles, error) {
// var angles = []float64{0, 0, 0}
// log.Printf("Sending: %v", angles)
// return &pb.Angles{Angles: angles}, nil
// }
//TODO: Fix compiling issue with google.protobuf.Empty message
func (s *server) GetJoints(ctx context.Context, in *pb.Empty) (*pb.Angles, error) {
var angles = []float64{0, 0, 0}
log.Printf("Sending: %v", angles)
return &pb.Angles{Angles: angles}, nil
}
func serve() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))

View file

@ -19,7 +19,6 @@ import (
)
var(
addr = "localhost:8080" //TODO: Add to cobra config
tgt_angles = []float64{} //TODO: Add to cobra config
)
@ -55,6 +54,7 @@ func init() {
//gRPC Client
func SetJoints(){
// 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)

View file

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