|
| 1 | +package megapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "log" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | + |
| 10 | + "gobot.io/x/gobot/v2" |
| 11 | + "gobot.io/x/gobot/v2/drivers/serial" |
| 12 | +) |
| 13 | + |
| 14 | +var _ gobot.Driver = (*MotorDriver)(nil) |
| 15 | + |
| 16 | +type megapiMotorSerialAdaptor interface { |
| 17 | + gobot.Adaptor |
| 18 | + serial.SerialWriter |
| 19 | +} |
| 20 | + |
| 21 | +// MotorDriver represents a motor |
| 22 | +type MotorDriver struct { |
| 23 | + *serial.Driver |
| 24 | + port byte |
| 25 | + halted bool |
| 26 | + writeBytesChannel chan []byte |
| 27 | + finalizeChannel chan struct{} |
| 28 | + syncRoot *sync.Mutex |
| 29 | +} |
| 30 | + |
| 31 | +// NewMotorDriver creates a new MotorDriver at the given port |
| 32 | +func NewMotorDriver(a megapiMotorSerialAdaptor, port byte, opts ...serial.OptionApplier) *MotorDriver { |
| 33 | + d := &MotorDriver{ |
| 34 | + port: port, |
| 35 | + halted: true, |
| 36 | + syncRoot: &sync.Mutex{}, |
| 37 | + writeBytesChannel: make(chan []byte), |
| 38 | + finalizeChannel: make(chan struct{}), |
| 39 | + } |
| 40 | + d.Driver = serial.NewDriver(a, "MegaPiMotor", d.initialize, d.shutdown, opts...) |
| 41 | + |
| 42 | + return d |
| 43 | +} |
| 44 | + |
| 45 | +// Speed sets the motors speed to the specified value |
| 46 | +func (d *MotorDriver) Speed(speed int16) error { |
| 47 | + d.syncRoot.Lock() |
| 48 | + defer d.syncRoot.Unlock() |
| 49 | + |
| 50 | + if d.halted { |
| 51 | + return nil |
| 52 | + } |
| 53 | + return d.speedHelper(speed) |
| 54 | +} |
| 55 | + |
| 56 | +// initialize implements the Driver interface |
| 57 | +func (d *MotorDriver) initialize() error { |
| 58 | + d.syncRoot.Lock() |
| 59 | + defer d.syncRoot.Unlock() |
| 60 | + |
| 61 | + // sleeping is required to give the board a chance to reset after connection is done |
| 62 | + time.Sleep(2 * time.Second) |
| 63 | + |
| 64 | + // kick off thread to send bytes to the board |
| 65 | + go func() { |
| 66 | + for { |
| 67 | + select { |
| 68 | + case bytes := <-d.writeBytesChannel: |
| 69 | + if _, err := d.adaptor().SerialWrite(bytes); err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + time.Sleep(10 * time.Millisecond) |
| 73 | + case <-d.finalizeChannel: |
| 74 | + d.finalizeChannel <- struct{}{} |
| 75 | + return |
| 76 | + default: |
| 77 | + time.Sleep(10 * time.Millisecond) |
| 78 | + } |
| 79 | + } |
| 80 | + }() |
| 81 | + |
| 82 | + d.halted = false |
| 83 | + return d.speedHelper(0) |
| 84 | +} |
| 85 | + |
| 86 | +// Halt terminates the Driver interface |
| 87 | +func (d *MotorDriver) shutdown() error { |
| 88 | + d.syncRoot.Lock() |
| 89 | + defer d.syncRoot.Unlock() |
| 90 | + |
| 91 | + d.finalizeChannel <- struct{}{} |
| 92 | + <-d.finalizeChannel |
| 93 | + |
| 94 | + d.halted = true |
| 95 | + return d.speedHelper(0) |
| 96 | +} |
| 97 | + |
| 98 | +// there is some sort of bug on the hardware such that you cannot |
| 99 | +// send the exact same speed to 2 different motors consecutively |
| 100 | +// hence we ensure we always alternate speeds |
| 101 | +func (d *MotorDriver) speedHelper(speed int16) error { |
| 102 | + if err := d.sendSpeed(speed - 1); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + return d.sendSpeed(speed) |
| 106 | +} |
| 107 | + |
| 108 | +// sendSpeed sets the motors speed to the specified value |
| 109 | +func (d *MotorDriver) sendSpeed(speed int16) error { |
| 110 | + bufOut := new(bytes.Buffer) |
| 111 | + |
| 112 | + // byte sequence: 0xff, 0x55, id, action, device, port |
| 113 | + bufOut.Write([]byte{0xff, 0x55, 0x6, 0x0, 0x2, 0xa, d.port}) |
| 114 | + if err := binary.Write(bufOut, binary.LittleEndian, speed); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + bufOut.Write([]byte{0xa}) |
| 118 | + d.writeBytesChannel <- bufOut.Bytes() |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func (d *MotorDriver) adaptor() megapiMotorSerialAdaptor { |
| 124 | + if a, ok := d.Connection().(megapiMotorSerialAdaptor); ok { |
| 125 | + return a |
| 126 | + } |
| 127 | + |
| 128 | + log.Printf("%s has no MegaPi serial connector\n", d.Name()) |
| 129 | + return nil |
| 130 | +} |
0 commit comments