Skip to content

Commit

Permalink
updating adc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmolloy committed Jun 21, 2018
1 parent 2783a29 commit 1b0614e
Show file tree
Hide file tree
Showing 15 changed files with 623 additions and 10 deletions.
Binary file added chp08/can/simple
Binary file not shown.
42 changes: 42 additions & 0 deletions chp08/can/simpleCAN.c
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 added chp09/simple/motor
Binary file not shown.
106 changes: 106 additions & 0 deletions chp09/simple/motor.c
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 modified chp09/stepper/StepperApp
Binary file not shown.
17 changes: 7 additions & 10 deletions chp09/stepper/StepperMotorApp.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
/* A Stepper Motor Application
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014
* ISBN 9781118935125. Please see the file README.md in the repository root
* directory for copyright and GNU GPLv3 license information.
* The pins for this example are wired as follows:
* MS1 - BBB P8_12 = GPIO 1_12 = GPIO 44
* MS2 - BBB P8_18 = GPIO 2_01 = GPIO 65
* STEP- BBB P8_14 = GPIO 0_26 = GPIO 26
* SLP - BBB P8_11 = GPIO 1_13 = GPIO 45
* DIR - BBB P8_16 = GPIO 1_14 = GPIO 46
* MS1 - GPIO 1_20 = GPIO 52
* MS2 - GPIO 1_12 = GPIO 44
* STEP- GPIO 2_00 = GPIO 64
* SLP - GPIO 1_28 = GPIO 60
* DIR - GPIO 1_14 = GPIO 46
*/

#include <iostream>
#include <unistd.h>
#include "motor/StepperMotor.h"
using namespace std;
using namespace exploringBB;

int main(){
cout << "Starting EBB Stepper Motor Example:" << endl;
//Using 5 GPIOs, RPM=60 and 200 steps per revolution
StepperMotor m(44,65,26,45,46,60,200);
StepperMotor m(52,44,64,60,46,60,200);
m.setDirection(StepperMotor::ANTICLOCKWISE);
m.setStepMode(StepperMotor::STEP_FULL);
m.setSpeed(100); //rpm
Expand Down
Binary file modified chp09/testADC/testADC
Binary file not shown.
1 change: 1 addition & 0 deletions chp09/testADC/testADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include<string>
#include<sstream>
#include<cmath>
#include<unistd.h>
using namespace std;

#define LDR_PATH "/sys/bus/iio/devices/iio:device0/in_voltage"
Expand Down
2 changes: 2 additions & 0 deletions chp09/tmp36/build
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
45 changes: 45 additions & 0 deletions chp09/tmp36/bus/BusDevice.cpp
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 */
56 changes: 56 additions & 0 deletions chp09/tmp36/bus/BusDevice.h
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_ */
Loading

0 comments on commit 1b0614e

Please sign in to comment.