From 2de5220071f515ac59261d1e40b076701f40923a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 21 Aug 2017 23:20:18 +0200 Subject: Add trim --- car/car.ino | 30 +++++++++++------------------- car/elegoo.pb.c | 9 +++++---- car/elegoo.pb.h | 21 ++++++++++++--------- car/elegoo/elegoo.pb.go | 48 ++++++++++++++++++++++++++++-------------------- car/elegoo/elegoo.proto | 5 +++-- car/elegoo/main.go | 8 +------- car/pb/pb_stream.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ car/pb/pb_stream.h | 12 ++++++++++++ car/pb_stream.cpp | 48 ------------------------------------------------ car/pb_stream.h | 12 ------------ 10 files changed, 120 insertions(+), 121 deletions(-) create mode 100644 car/pb/pb_stream.cpp create mode 100644 car/pb/pb_stream.h delete mode 100644 car/pb_stream.cpp delete mode 100644 car/pb_stream.h diff --git a/car/car.ino b/car/car.ino index ee5c84f..474d543 100644 --- a/car/car.ino +++ b/car/car.ino @@ -14,6 +14,7 @@ PacketSerial serial; Servo servo; +int trim; IRrecv irrecv(IR); decode_results ir; @@ -39,17 +40,6 @@ void motorL(int v) { motor(ENB, IN3, IN4, v); } -void move(int l, int r) { - motor(ENA, IN1, IN2, l); - motor(ENB, IN3, IN4, r); -} - -#define forward(v) do move( v, v); while (0) -#define backward(v) do move(-v, -v); while (0) -#define toleft(v) do move( v, -v); while (0) -#define toright(v) do move(-v, v); while (0) -#define stop() do move( 0, 0); while (0) - int distance() { digitalWrite(Trig, LOW); delayMicroseconds(2); @@ -59,14 +49,12 @@ int distance() { return pulseIn(Echo, HIGH, TimeOut) / ToCM; } +// + left +// - right void look(int deg) { - servo.write(90 + deg); + servo.write(90 + deg + trim); } -#define lookahead() do look( 0); while (0) -#define lookleft(deg) do look( deg); while (0) -#define lookright(deg) do look(-deg); while (0) - void onPacket(const uint8_t* buf, size_t size) { Command cmd = Command_init_zero; @@ -79,16 +67,20 @@ void onPacket(const uint8_t* buf, size_t size) { if (cmd.has_SpeedL) { motorL(cmd.SpeedL); } - if (cmd.has_Stop && cmd.Stop) { + if (cmd.has_Stop) { motorR(0); motorL(0); } + if (cmd.has_Trim) { + trim = cmd.Trim; + } if (cmd.has_TurnHead) { - servo.write(90 + cmd.TurnHead); + look(cmd.TurnHead); } if (cmd.has_Center) { - servo.write(90); + look(0); } + } void env() { diff --git a/car/elegoo.pb.c b/car/elegoo.pb.c index 5af0f71..e9a762d 100644 --- a/car/elegoo.pb.c +++ b/car/elegoo.pb.c @@ -1,5 +1,5 @@ /* Automatically generated nanopb constant definitions */ -/* Generated by nanopb-0.3.6 at Mon Aug 21 22:55:02 2017. */ +/* Generated by nanopb-0.3.6 at Mon Aug 21 23:14:33 2017. */ #include "elegoo.pb.h" @@ -10,12 +10,13 @@ -const pb_field_t Command_fields[6] = { +const pb_field_t Command_fields[7] = { PB_FIELD( 1, SINT32 , OPTIONAL, STATIC , FIRST, Command, SpeedR, SpeedR, 0), PB_FIELD( 2, SINT32 , OPTIONAL, STATIC , OTHER, Command, SpeedL, SpeedR, 0), PB_FIELD( 3, BOOL , OPTIONAL, STATIC , OTHER, Command, Stop, SpeedL, 0), - PB_FIELD( 4, SINT32 , OPTIONAL, STATIC , OTHER, Command, TurnHead, Stop, 0), - PB_FIELD( 5, BOOL , OPTIONAL, STATIC , OTHER, Command, Center, TurnHead, 0), + PB_FIELD( 4, BOOL , OPTIONAL, STATIC , OTHER, Command, Center, Stop, 0), + PB_FIELD( 5, SINT32 , OPTIONAL, STATIC , OTHER, Command, TurnHead, Center, 0), + PB_FIELD( 6, SINT32 , OPTIONAL, STATIC , OTHER, Command, Trim, TurnHead, 0), PB_LAST_FIELD }; diff --git a/car/elegoo.pb.h b/car/elegoo.pb.h index 4a0e918..0518f9f 100644 --- a/car/elegoo.pb.h +++ b/car/elegoo.pb.h @@ -1,5 +1,5 @@ /* Automatically generated nanopb header */ -/* Generated by nanopb-0.3.6 at Mon Aug 21 22:55:02 2017. */ +/* Generated by nanopb-0.3.6 at Mon Aug 21 23:14:33 2017. */ #ifndef PB_ELEGOO_PB_H_INCLUDED #define PB_ELEGOO_PB_H_INCLUDED @@ -22,10 +22,12 @@ typedef struct _Command { int32_t SpeedL; bool has_Stop; bool Stop; - bool has_TurnHead; - int32_t TurnHead; bool has_Center; bool Center; + bool has_TurnHead; + int32_t TurnHead; + bool has_Trim; + int32_t Trim; /* @@protoc_insertion_point(struct:Command) */ } Command; @@ -46,17 +48,18 @@ typedef struct _Event { /* Default values for struct fields */ /* Initializer values for message structs */ -#define Command_init_default {false, 0, false, 0, false, 0, false, 0, false, 0} +#define Command_init_default {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0} #define Event_init_default {false, 0, false, 0, false, 0, false, 0, false, 0} -#define Command_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0} +#define Command_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0, false, 0} #define Event_init_zero {false, 0, false, 0, false, 0, false, 0, false, 0} /* Field tags (for use in manual encoding/decoding) */ #define Command_SpeedR_tag 1 #define Command_SpeedL_tag 2 #define Command_Stop_tag 3 -#define Command_TurnHead_tag 4 -#define Command_Center_tag 5 +#define Command_Center_tag 4 +#define Command_TurnHead_tag 5 +#define Command_Trim_tag 6 #define Event_Distance_tag 1 #define Event_SensorR_tag 2 #define Event_SensorC_tag 3 @@ -64,11 +67,11 @@ typedef struct _Event { #define Event_KeyPress_tag 5 /* Struct field encoding specification for nanopb */ -extern const pb_field_t Command_fields[6]; +extern const pb_field_t Command_fields[7]; extern const pb_field_t Event_fields[6]; /* Maximum encoded size of messages (where known) */ -#define Command_size 22 +#define Command_size 28 #define Event_size 18 /* Message IDs (where set with "msgid" option) */ diff --git a/car/elegoo/elegoo.pb.go b/car/elegoo/elegoo.pb.go index a0e9704..b20c09c 100644 --- a/car/elegoo/elegoo.pb.go +++ b/car/elegoo/elegoo.pb.go @@ -32,8 +32,9 @@ type Command struct { SpeedR int32 `protobuf:"zigzag32,1,opt,name=SpeedR" json:"SpeedR,omitempty"` SpeedL int32 `protobuf:"zigzag32,2,opt,name=SpeedL" json:"SpeedL,omitempty"` Stop bool `protobuf:"varint,3,opt,name=Stop" json:"Stop,omitempty"` - TurnHead int32 `protobuf:"zigzag32,4,opt,name=TurnHead" json:"TurnHead,omitempty"` - Center bool `protobuf:"varint,5,opt,name=Center" json:"Center,omitempty"` + Center bool `protobuf:"varint,4,opt,name=Center" json:"Center,omitempty"` + TurnHead int32 `protobuf:"zigzag32,5,opt,name=TurnHead" json:"TurnHead,omitempty"` + Trim int32 `protobuf:"zigzag32,6,opt,name=Trim" json:"Trim,omitempty"` } func (m *Command) Reset() { *m = Command{} } @@ -62,6 +63,13 @@ func (m *Command) GetStop() bool { return false } +func (m *Command) GetCenter() bool { + if m != nil { + return m.Center + } + return false +} + func (m *Command) GetTurnHead() int32 { if m != nil { return m.TurnHead @@ -69,11 +77,11 @@ func (m *Command) GetTurnHead() int32 { return 0 } -func (m *Command) GetCenter() bool { +func (m *Command) GetTrim() int32 { if m != nil { - return m.Center + return m.Trim } - return false + return 0 } type Event struct { @@ -132,19 +140,19 @@ func init() { func init() { proto.RegisterFile("elegoo.proto", fileDescriptor0) } var fileDescriptor0 = []byte{ - // 212 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x49, 0xcd, 0x49, 0x4d, - 0xcf, 0xcf, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x57, 0x6a, 0x64, 0xe4, 0x62, 0x77, 0xce, 0xcf, - 0xcd, 0x4d, 0xcc, 0x4b, 0x11, 0x12, 0xe3, 0x62, 0x0b, 0x2e, 0x48, 0x4d, 0x4d, 0x09, 0x92, 0x60, - 0x54, 0x60, 0xd4, 0x10, 0x0c, 0x82, 0xf2, 0xe0, 0xe2, 0x3e, 0x12, 0x4c, 0x48, 0xe2, 0x3e, 0x42, - 0x42, 0x5c, 0x2c, 0xc1, 0x25, 0xf9, 0x05, 0x12, 0xcc, 0x0a, 0x8c, 0x1a, 0x1c, 0x41, 0x60, 0xb6, - 0x90, 0x14, 0x17, 0x47, 0x48, 0x69, 0x51, 0x9e, 0x47, 0x6a, 0x62, 0x8a, 0x04, 0x0b, 0x58, 0x35, - 0x9c, 0x0f, 0x32, 0xc7, 0x39, 0x35, 0xaf, 0x24, 0xb5, 0x48, 0x82, 0x15, 0xac, 0x03, 0xca, 0x53, - 0xea, 0x65, 0xe4, 0x62, 0x75, 0x2d, 0x4b, 0xcd, 0x2b, 0x01, 0xe9, 0x76, 0xc9, 0x2c, 0x2e, 0x49, - 0xcc, 0x4b, 0x4e, 0x05, 0xbb, 0x81, 0x37, 0x08, 0xce, 0x17, 0x92, 0xe0, 0x62, 0x0f, 0x4e, 0xcd, - 0x2b, 0xce, 0x2f, 0x0a, 0x02, 0x3b, 0x83, 0x23, 0x08, 0xc6, 0x45, 0xc8, 0x38, 0x43, 0x9d, 0x02, - 0xe3, 0x22, 0x64, 0x7c, 0xc0, 0x8e, 0x81, 0xcb, 0xf8, 0x80, 0x6c, 0xf2, 0x4e, 0xad, 0x0c, 0x28, - 0x4a, 0x2d, 0x2e, 0x06, 0xbb, 0x86, 0x37, 0x08, 0xce, 0x77, 0x62, 0x8b, 0x62, 0xc9, 0x4d, 0xcc, - 0xcc, 0x4b, 0x62, 0x03, 0x07, 0x91, 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x8f, 0x9d, 0x88, 0x7d, - 0x32, 0x01, 0x00, 0x00, + // 219 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0xb1, 0x4a, 0xc5, 0x30, + 0x14, 0x86, 0x89, 0xb6, 0x69, 0x09, 0x76, 0x30, 0x83, 0x04, 0xa7, 0xd2, 0xa9, 0x93, 0x8b, 0x6f, + 0x60, 0x15, 0x04, 0x3b, 0x48, 0xda, 0xc9, 0x2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o/elegoo.proto b/car/elegoo/elegoo.proto index f310b62..bdf1b95 100644 --- a/car/elegoo/elegoo.proto +++ b/car/elegoo/elegoo.proto @@ -6,8 +6,9 @@ message Command { sint32 SpeedR = 1; sint32 SpeedL = 2; bool Stop = 3; - sint32 TurnHead = 4; // ±90 deg - bool Center = 5; + bool Center = 4; + sint32 TurnHead = 5; // ±90 deg + sint32 Trim = 6; } message Event { diff --git a/car/elegoo/main.go b/car/elegoo/main.go index 900b584..df5dabc 100644 --- a/car/elegoo/main.go +++ b/car/elegoo/main.go @@ -7,7 +7,6 @@ import ( "bufio" "io" "log" - "time" "github.com/dim13/cobs" "github.com/golang/protobuf/proto" @@ -51,12 +50,7 @@ func main() { go func() { cmd := &Command{} - for i := -45; i < 45; i += 5 { - cmd.TurnHead = int32(i) - Write(s, cmd) - time.Sleep(time.Second / 2) - } - cmd.TurnHead = 0 + cmd.Trim = -10 cmd.Center = true Write(s, cmd) diff --git a/car/pb/pb_stream.cpp b/car/pb/pb_stream.cpp new file mode 100644 index 0000000..41100ed --- /dev/null +++ b/car/pb/pb_stream.cpp @@ -0,0 +1,48 @@ +// From https://github.com/amorellgarcia/arduino-nanopb + +#include "pb_stream.h" + +#define MAXSZ (size_t)-1 + +bool +os_read(pb_istream_t *stream, uint8_t *buf, size_t count) +{ + Stream *s = static_cast(stream->state); + while (s->available() > 0 && count > 0) { + count -= s->readBytes((char *)buf, count); + } + return count == 0; +} + + +void +pb_istream_from_stream(Stream &stream, pb_istream_t &istream) +{ + istream.callback = &os_read; + istream.state = &stream; + istream.bytes_left = MAXSZ; +#ifndef PB_NO_ERRMSG + istream.errmsg = NULL; +#endif +} + +bool +os_write(pb_ostream_t *stream, const uint8_t *buf, size_t count) +{ + if (stream == NULL || buf == NULL) { + return false; + } + Print *s = static_cast(stream->state); + return (s->write(buf, count) == count); +} + +void +pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream) { + ostream.callback = &os_write; + ostream.state = &stream; + ostream.max_size = MAXSZ; + ostream.bytes_written = 0; +#ifndef PB_NO_ERRMSG + ostream.errmsg = NULL; +#endif +} diff --git a/car/pb/pb_stream.h b/car/pb/pb_stream.h new file mode 100644 index 0000000..00be77c --- /dev/null +++ b/car/pb/pb_stream.h @@ -0,0 +1,12 @@ +#ifndef PB_STREAM +#define PB_STREAM + +#include +#include +#include "pb_encode.h" +#include "pb_decode.h" + +void pb_istream_from_stream(Stream &stream, pb_istream_t &istream); +void pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream); + +#endif diff --git a/car/pb_stream.cpp b/car/pb_stream.cpp deleted file mode 100644 index 41100ed..0000000 --- a/car/pb_stream.cpp +++ /dev/null @@ -1,48 +0,0 @@ -// From https://github.com/amorellgarcia/arduino-nanopb - -#include "pb_stream.h" - -#define MAXSZ (size_t)-1 - -bool -os_read(pb_istream_t *stream, uint8_t *buf, size_t count) -{ - Stream *s = static_cast(stream->state); - while (s->available() > 0 && count > 0) { - count -= s->readBytes((char *)buf, count); - } - return count == 0; -} - - -void -pb_istream_from_stream(Stream &stream, pb_istream_t &istream) -{ - istream.callback = &os_read; - istream.state = &stream; - istream.bytes_left = MAXSZ; -#ifndef PB_NO_ERRMSG - istream.errmsg = NULL; -#endif -} - -bool -os_write(pb_ostream_t *stream, const uint8_t *buf, size_t count) -{ - if (stream == NULL || buf == NULL) { - return false; - } - Print *s = static_cast(stream->state); - return (s->write(buf, count) == count); -} - -void -pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream) { - ostream.callback = &os_write; - ostream.state = &stream; - ostream.max_size = MAXSZ; - ostream.bytes_written = 0; -#ifndef PB_NO_ERRMSG - ostream.errmsg = NULL; -#endif -} diff --git a/car/pb_stream.h b/car/pb_stream.h deleted file mode 100644 index 00be77c..0000000 --- a/car/pb_stream.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef PB_STREAM -#define PB_STREAM - -#include -#include -#include "pb_encode.h" -#include "pb_decode.h" - -void pb_istream_from_stream(Stream &stream, pb_istream_t &istream); -void pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream); - -#endif -- cgit v1.2.3