Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change CAN interface from a SPI breakout to direct CAN. #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

## Summary ##

This is a C++ library that provides a convenient API for controlling and monitoring an mjbots moteus brushless servo controller. It uses the ACAN2517FD library to perform CAN-FD communications, but could be easily modified to work with other libraries.
This is a C++ library that provides a convenient API for controlling and monitoring an mjbots moteus brushless servo controller. It uses the ACAN4 library to perform CAN-FD communications, but could be easily modified to work with other libraries.

## Getting started ##

Note that this library can only communicate and operate a moteus controller which has already been calibrated. Calibration currently can not be performed via an Arduino and requires `moteus_tool` executing on a desktop computer with some CAN-FD adapter. See the moteus [getting started guide](https://github.com/mjbots/moteus/blob/main/docs/getting_started.md#calibration) for details. The only controllers sold by mjbots.com which are pre-calibrated are those included in development kits.

### Hardware ###
For Teensy 4.x support, you will need a 3.3v compatible CAN transceiver such as the Adafruit TJA1051T/3. Only CAN3 (pins 30/31) support FD mode. Connected the CRX to RX and CTX to TX on the transceiver. From the transceiver to the Moteus CAN-H, CAN-L, and GND should be connected in standard way. The CAN-FD bus needs to be connected to moteus, typically this would be at least the CANL and CANH wires, and likely the ground as well. For more than 2 or 3 controllers, separate 120 ohm termination resistors will be required on each end of the CAN bus. Some Arduino CAN-FD shields, like the CANBed FD, have one termination resistor built in.

You will need to have hardware that is compatible with the [ACAN2517FD library](https://github.com/pierremolinaro/acan2517FD), this will be something that has the MCP2517FD or MCP2518FD CAN-FD controller onboard.

The CAN-FD bus needs to be connected to moteus, typically this would be at least the CANL and CANH wires, and likely the ground as well. For more than 2 or 3 controllers, separate 120 ohm termination resistors will be required on each end of the CAN bus. Some Arduino CAN-FD shields, like the CANBed FD, have one termination resistor built in.

### Software ###

Expand All @@ -25,7 +23,7 @@ If you are using version 1.6.x or later of the Arduino software (IDE) you can us

If this does not work, you can manually install the library:

1. Download the [latest release archive from GitHub](https://github.com/mjbots/moteus-arduino/releases) and decompress it
1. Download the [latest release archive from GitHub](https://github.com/kylevernyi/moteus-teensy) and decompress it
2. Rename the folder "moteus-arduino" to "moteus"
3. Drag the "moteus" folder into the "libraries" directory inside your Arduino sketchbook directory. You can view your sketchbook location by opening the "File" menu and selecting "Preferences" in the Arduino IDE. If there is not already a "libraries" folder in that location, you should make the folder yourself.
4. After installing the library, restart the Arduino IDE.
Expand Down
94 changes: 23 additions & 71 deletions examples/BasicControl/BasicControl.ino
Original file line number Diff line number Diff line change
@@ -1,90 +1,46 @@
//——————————————————————————————————————————————————————————————————————————————
// Demonstration of control and monitoring of 2 moteus controllers
// running on a CANBed FD from longan labs.
// * https://mjbots.com/products/moteus-r4-11
// * https://www.longan-labs.cc/1030009.html
//
// Controller ID 1 is moved through a sine wave pattern, while
// controller ID 2 just has a brake command sent.
// ——————————————————————————————————————————————————————————————————————————————

#include <ACAN2517FD.h>
#include <Moteus.h>

//——————————————————————————————————————————————————————————————————————————————
// The following pins are selected for the CANBed FD board.
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2517_SCK = 9 ; // SCK input of MCP2517
static const byte MCP2517_SDI = 10 ; // SDI input of MCP2517
static const byte MCP2517_SDO = 11 ; // SDO output of MCP2517

static const byte MCP2517_CS = 17 ; // CS input of MCP2517
static const byte MCP2517_INT = 7 ; // INT output of MCP2517
#ifndef __IMXRT1062__
#error "This sketch should be compiled for Teensy 4.x"
#endif

static uint32_t gNextSendMillis = 0;

//——————————————————————————————————————————————————————————————————————————————
// ACAN2517FD Driver object
//——————————————————————————————————————————————————————————————————————————————

ACAN2517FD can (MCP2517_CS, SPI, MCP2517_INT) ;
#include <ACAN_T4.h>
#include "Moteus.h"

Moteus moteus1(can, []() {
Moteus moteus1(
[]() { // lambda function which initializes options
Moteus::Options options;
options.id = 1;
return options;
}());
Moteus moteus2(can, []() {
Moteus::Options options;
options.id = 2;
return options;
}());
}
());

Moteus::PositionMode::Command position_cmd;

void setup() {
pinMode (LED_BUILTIN, OUTPUT);

// Let the world know we have begun!
void setup () {
// Let the world know we have begun!
Serial.begin(115200);
while (!Serial) {}
Serial.println(F("started"));

SPI.begin();

// This operates the CAN-FD bus at 1Mbit for both the arbitration
// and data rate. Most arduino shields cannot operate at 5Mbps
// correctly, so the moteus Arduino library permanently disables
// BRS.
ACAN2517FDSettings settings(
ACAN2517FDSettings::OSC_20MHz, 1000ll * 1000ll, DataBitRateFactor::x1);
ACAN_T4FD_Settings settings (1000000, DataBitRateFactor::x1) ;

// The atmega32u4 on the CANbed has only a tiny amount of memory.
// The ACAN2517FD driver needs custom settings so as to not exhaust
// all of SRAM just with its buffers.
settings.mArbitrationSJW = 2;
settings.mDriverTransmitFIFOSize = 1;
settings.mDriverReceiveFIFOSize = 2;

const uint32_t errorCode = can.begin(settings, [] { can.isr(); });

while (errorCode != 0) {
Serial.print(F("CAN error 0x"));
Serial.println(errorCode, HEX);
delay(1000);
const uint32_t errorCode = ACAN_T4::can3.beginFD (settings) ;
if (0 == errorCode) {
Serial.println ("can3 ok") ;
}else{
Serial.print ("Error can3: 0x") ;
Serial.println (errorCode, HEX) ;
}

// To clear any faults the controllers may have, we start by sending
// a stop command to each.
moteus1.SetStop();
moteus2.SetStop();
Serial.println(F("all stopped"));
}


static uint32_t gNextSendMillis = 0;
uint16_t gLoopCount = 0;

void loop() {
void loop () {
// We intend to send control frames every 20ms.
const auto time = millis();
if (gNextSendMillis >= time) { return; }
Expand All @@ -94,15 +50,12 @@ void loop() {

Moteus::PositionMode::Command cmd;
cmd.position = NaN;
cmd.velocity = 0.2 * ::sin(time / 1000.0);

cmd.velocity = 5*::sin(time / 1000.0);
moteus1.SetPosition(cmd);
moteus2.SetBrake();

if (gLoopCount % 5 != 0) { return; }

// Only print our status every 5th cycle, so every 1s.

Serial.print(F("time "));
Serial.print(gNextSendMillis);

Expand All @@ -115,7 +68,6 @@ void loop() {
};

print_moteus(moteus1.last_result().values);
Serial.print(F(" / "));
print_moteus(moteus2.last_result().values);
Serial.println();

}
147 changes: 0 additions & 147 deletions examples/DiagnosticProtocol/DiagnosticProtocol.ino

This file was deleted.

Loading