-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2783a29
commit 1b0614e
Showing
15 changed files
with
623 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// From https://en.wikipedia.org/wiki/SocketCAN | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <string.h> | ||
#include <net/if.h> | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <sys/ioctl.h> | ||
#include <linux/can.h> | ||
#include <linux/can/raw.h> | ||
|
||
int main(void) { | ||
int s; | ||
int nbytes; | ||
struct sockaddr_can addr; | ||
struct can_frame frame; | ||
struct ifreq ifr; | ||
const char *ifname = "can0"; | ||
if((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { | ||
perror("Error while opening socket"); | ||
return -1; | ||
} | ||
strcpy(ifr.ifr_name, ifname); | ||
ioctl(s, SIOCGIFINDEX, &ifr); | ||
addr.can_family = AF_CAN; | ||
addr.can_ifindex = ifr.ifr_ifindex; | ||
printf("%s at index %d\n", ifname, ifr.ifr_ifindex); | ||
if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) { | ||
perror("Error in socket bind"); | ||
return -2; | ||
} | ||
frame.can_id = 0x123; | ||
frame.can_dlc = 3; | ||
frame.data[0] = 0x11; | ||
frame.data[1] = 0x22; | ||
frame.data[2] = 0x33; | ||
nbytes = write(s, &frame, sizeof(struct can_frame)); | ||
printf("Wrote %d bytes\n", nbytes); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// This code comes from pololu.com directly | ||
// Uses POSIX functions to send and receive data from the virtual serial | ||
// port of a Pololu Simple Motor Controller. | ||
// NOTE: The Simple Motor Controller's Input Mode must be set to Serial/USB. | ||
// NOTE: You must change the 'const char * device' line below. | ||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <termios.h> | ||
#define SERIAL_ERROR -9999 | ||
|
||
// Reads a variable from the SMC and returns it as number between 0 and 65535. | ||
// Returns SERIAL_ERROR if there was an error. | ||
// The 'variableId' argument must be one of IDs listed in the | ||
// "Controller Variables" section of the user's guide. | ||
// For variables that are actually signed, additional processing is required | ||
// (see smcGetTargetSpeed for an example). | ||
int smcGetVariable(int fd, unsigned char variableId) { | ||
unsigned char command[] = {0xA1, variableId}; | ||
if(write(fd, &command, sizeof(command)) == -1) | ||
{ | ||
perror("error writing"); | ||
return SERIAL_ERROR; | ||
} | ||
unsigned char response[2]; | ||
if(read(fd,response,2) != 2) | ||
{ | ||
perror("error reading"); | ||
return SERIAL_ERROR; | ||
} | ||
return response[0] + 256*response[1]; | ||
} | ||
|
||
// Returns the target speed (-3200 to 3200). | ||
// Returns SERIAL_ERROR if there is an error. | ||
int smcGetTargetSpeed(int fd) { | ||
int val = smcGetVariable(fd, 20); | ||
return val == SERIAL_ERROR ? SERIAL_ERROR : (signed short)val; | ||
} | ||
|
||
// Returns a number where each bit represents a different error, and the | ||
// bit is 1 if the error is currently active. | ||
// See the user's guide for definitions of the different error bits. | ||
// Returns SERIAL_ERROR if there is an error. | ||
int smcGetErrorStatus(int fd) { | ||
return smcGetVariable(fd,0); | ||
} | ||
|
||
// Sends the Exit Safe Start command, which is required to drive the motor. | ||
// Returns 0 if successful, SERIAL_ERROR if there was an error sending. | ||
int smcExitSafeStart(int fd) { | ||
const unsigned char command = 0x83; | ||
if (write(fd, &command, 1) == -1) | ||
{ | ||
perror("error writing"); | ||
return SERIAL_ERROR; | ||
} | ||
return 0; | ||
} | ||
|
||
// Sets the SMC's target speed (-3200 to 3200). | ||
// Returns 0 if successful, SERIAL_ERROR if there was an error sending. | ||
int smcSetTargetSpeed(int fd, int speed) | ||
{ | ||
unsigned char command[3]; | ||
if (speed < 0) { | ||
command[0] = 0x86; // Motor Reverse | ||
speed = -speed; | ||
} | ||
else { | ||
command[0] = 0x85; // Motor Forward | ||
} | ||
command[1] = speed & 0x1F; | ||
command[2] = speed >> 5 & 0x7F; | ||
if (write(fd, command, sizeof(command)) == -1) | ||
{ | ||
perror("error writing"); | ||
return SERIAL_ERROR; | ||
} | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
printf("Starting the motor controller example\n"); | ||
const char * device = "/dev/ttyO4"; // Linux | ||
int fd = open(device, O_RDWR | O_NOCTTY); | ||
if (fd == -1) { | ||
perror(device); | ||
return 1; | ||
} | ||
struct termios options; | ||
tcgetattr(fd, &options); | ||
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); | ||
options.c_oflag &= ~(ONLCR | OCRNL); | ||
tcsetattr(fd, TCSANOW, &options); | ||
smcExitSafeStart(fd); | ||
printf("Error status: 0x%04x\n", smcGetErrorStatus(fd)); | ||
int speed = smcGetTargetSpeed(fd); | ||
printf("Current Target Speed is %d.\n", speed); | ||
int newSpeed = (speed <= 0) ? 3200 : -3200; | ||
printf("Setting Target Speed to %d.\n", newSpeed); | ||
smcSetTargetSpeed(fd, newSpeed); | ||
close(fd); | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
g++ -o tmp36 tmp36.cpp bus/SPIDevice.cpp bus/BusDevice.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* BusDevice.cpp Created on: 23 May 2014 | ||
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie) | ||
* Made available for the book "Exploring BeagleBone" | ||
* See: www.exploringbeaglebone.com | ||
* Licensed under the EUPL V.1.1 | ||
* | ||
* This Software is provided to You under the terms of the European | ||
* Union Public License (the "EUPL") version 1.1 as published by the | ||
* European Union. Any use of this Software, other than as authorized | ||
* under this License is strictly prohibited (to the extent such use | ||
* is covered by a right of the copyright holder of this Software). | ||
* | ||
* This Software is provided under the License on an "AS IS" basis and | ||
* without warranties of any kind concerning the Software, including | ||
* without limitation merchantability, fitness for a particular purpose, | ||
* absence of defects or errors, accuracy, and non-infringement of | ||
* intellectual property rights other than copyright. This disclaimer | ||
* of warranty is an essential part of the License and a condition for | ||
* the grant of any rights to this Software. | ||
* | ||
* For more details, see http://www.derekmolloy.ie/ | ||
*/ | ||
|
||
#include "BusDevice.h" | ||
|
||
namespace exploringBB { | ||
|
||
/** | ||
* Constructor for a generic bus device | ||
* @param bus the bus number | ||
* @param device the device number | ||
*/ | ||
BusDevice::BusDevice(unsigned int bus, unsigned int device) { | ||
this->bus = bus; | ||
this->device = device; | ||
this->file=-1; | ||
} | ||
|
||
/** | ||
* Destructor is unused | ||
*/ | ||
BusDevice::~BusDevice() {} | ||
|
||
} /* namespace exploringBB */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* BusDevice.h Created on: 23 May 2014 | ||
* Copyright (c) 2014 Derek Molloy (www.derekmolloy.ie) | ||
* Made available for the book "Exploring BeagleBone" | ||
* See: www.exploringbeaglebone.com | ||
* Licensed under the EUPL V.1.1 | ||
* | ||
* This Software is provided to You under the terms of the European | ||
* Union Public License (the "EUPL") version 1.1 as published by the | ||
* European Union. Any use of this Software, other than as authorized | ||
* under this License is strictly prohibited (to the extent such use | ||
* is covered by a right of the copyright holder of this Software). | ||
* | ||
* This Software is provided under the License on an "AS IS" basis and | ||
* without warranties of any kind concerning the Software, including | ||
* without limitation merchantability, fitness for a particular purpose, | ||
* absence of defects or errors, accuracy, and non-infringement of | ||
* intellectual property rights other than copyright. This disclaimer | ||
* of warranty is an essential part of the License and a condition for | ||
* the grant of any rights to this Software. | ||
* | ||
* For more details, see http://www.derekmolloy.ie/ | ||
*/ | ||
|
||
#ifndef BUSDEVICE_H_ | ||
#define BUSDEVICE_H_ | ||
|
||
namespace exploringBB { | ||
|
||
/** | ||
* @class BusDevice | ||
* @brief This class is the parent of I2C and SPI devices, so that devices that use both | ||
* SPI and I2C interfaces can use those interfaces interchangeably. Because it contains | ||
* abstract methods, the child classes MUST implement the methods that are listed in this | ||
* class. | ||
*/ | ||
class BusDevice { | ||
protected: | ||
unsigned int bus; /**< the bus number */ | ||
unsigned int device; /**< the device number on the bus */ | ||
int file; /**< the file handle to the device */ | ||
public: | ||
BusDevice(unsigned int bus, unsigned int device); | ||
virtual int open()=0; | ||
virtual unsigned char readRegister(unsigned int registerAddress)=0; | ||
virtual unsigned char* readRegisters(unsigned int number, unsigned int fromAddress=0)=0; | ||
virtual int write(unsigned char value)=0; | ||
virtual int writeRegister(unsigned int registerAddress, unsigned char value)=0; | ||
virtual void debugDumpRegisters(unsigned int number = 0xff)=0; | ||
virtual void close()=0; | ||
virtual ~BusDevice(); | ||
}; | ||
|
||
} /* namespace exploringBB */ | ||
|
||
#endif /* BUSDEVICE_H_ */ |
Oops, something went wrong.